#include<stdio.h> #include<string.h> int main() { char xuehao[46];int p[46]={0};char xingming[46],compare[46]; printf("please write down the student's number!\n"); for(int i=1;i<=45;i++) { scanf("%s",xuehao+i); } printf("please write down the student's name\n"); for(int i=1;i<=45;i++) { scanf("%s",xingming+i); } //读取录入姓名学号,未重复的为p==0 //读取做完的学生信息,姓名和学号串 int n=0; printf("please write down the finished student's name or number or name and number!\n"); while(scanf("%s",compare+n)!=EOF) { n++; } int s=45-n;//s为未完成的人数量 printf("print down the unfinished number %d",s); int i; for(i=1;i<=n;i++) { if(11==strlen(compare+i)) { for(int j=1;j<=45;j++) { if(strcmp(compare+i,xuehao+j)==0){p[j]=1;} } } else if(11>strlen(compare+i)) { for(int j=1;j<=45;j++) { if(strcmp(compare+i,xingming+j)==0){p[j]=1;} } } else{ char cc[100]; for(int j=1;j<=45;j++) { strncpy(cc, compare+i,strlen(xingming+j)); if(strcmp(cc,xingming+j)==0){p[j]=1;} strncpy(cc, compare+i,strlen(xuehao+j)); if(strcmp(cc,xuehao+j)==0){p[j]=1;} } } printf("print out the data!\n"); for(i=1;i<=45;i++) { if(p[i]==0)printf("%s",xuehao[i]); } } return 0; }
时间: 2023-12-03 16:02:56 浏览: 35
这段代码的作用是从输入中读取学生姓名和学号信息,并记录每个学生的完成情况。它包括以下步骤:
1. 读取45个学生的学号信息,存储在字符数组xuehao中。
2. 读取45个学生的姓名信息,存储在字符数组xingming中。
3. 从输入中读取已完成学生的信息,存储在字符数组compare中。
4. 根据compare中的信息,更新每个学生的完成情况,将完成的学生对应的p数组元素设置为1。
5. 打印未完成的学生的学号信息。
该代码存在一些问题,以下是一些需要注意的点:
1. 学号和姓名的长度都是46,但是在循环中却只读取了45个元素。这可能会导致访问越界的问题。
2. 在每次循环中,都会重新从头遍历xuehao和xingming数组,这样会导致效率较低。
3. 在更新每个学生的完成情况时,应该使用break语句跳出循环,避免不必要的遍历。
4. 在打印未完成学生信息时,应该使用空格或换行符进行分隔,否则所有学生的学号信息会连在一起输出。
5. 在比较姓名和学号信息时,应该使用strncmp函数而不是strcmp函数,以避免截断字符串的问题。
6. 当输入的已完成学生信息中既包含学号又包含姓名时,该代码的比较方法可能存在问题。
相关问题
只使用#include <stdio.h> #include <string.h>
这两个头文件都是C语言标准库中的头文件,其中<stdio.h>包含了一些输入输出函数的声明,如printf和scanf;<string.h>包含了一些字符串操作函数的声明,如strcpy和strlen。使用这两个头文件可以完成很多基本的C语言程序设计,例如输出、输入、字符串操作等等。下面给出一个简单的例子:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "hello";
char str2[10] = "world";
printf("%s ", str1);
printf("%s\n", str2);
strcat(str1, str2);
printf("%s\n", str1);
printf("The length of str1 is %d.\n", strlen(str1));
return 0;
}
```
这个程序输出了两个字符串"hello"和"world",然后将它们拼接在一起并输出结果"helloworld",最后输出了拼接后的字符串长度11。
include <stdio.h>#include <stdlib.h>
这是一个C语言的标准头文件的引用,其中<stdio.h>包含了C语言的输入输出函数,而<stdlib.h>包含了一些通用的工具函数和内存分配函数。这些头文件在C语言中非常常见,几乎每个C程序都会用到它们。下面是一个简单的例子,展示了如何使用这两个头文件。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
char str[100];
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered %d.\n", num);
printf("Enter a string: ");
scanf("%s", str);
printf("You entered %s.\n", str);
return 0;
}
```
上面的程序使用了<stdio.h>中的printf和scanf函数,以及<stdlib.h>中的exit函数。输入一个整数和一个字符串,然后将它们输出到控制台上。