void Student(struct student stu[N]) { int i, n = 0; struct student *p; p=stu; char num1[] = "2021110300236"; char pass1[] = "111111"; char num[13], pass[10]; input:printf("请输入学号:"); scanf("%s",&num); printf("请输入密码:"); scanf("%s",&pass); for(i=0;i<sum;i++) { if(strcmp(num,num1)==0 && strcmp(pass,pass1)==0) { Sid+i; break; } } if(i==sum){ printf("你输入错误,请重新输入!\n"); goto input; } while (n < 4) { 优化代码
时间: 2024-04-07 13:28:48 浏览: 16
这段代码可以进行一些优化,例如:
1. 使用fgets代替scanf,可以避免输入缓冲区溢出的问题。
2. 使用strcmp的返回值,可以避免使用for循环和break语句。
3. 不要使用goto语句,可以使用while循环代替。
下面是优化后的代码:
void Student(struct student stu[N]) {
int i, n = 0;
struct student *p;
p = stu;
char num1[] = "2021110300236";
char pass1[] = "111111";
char num[13], pass[10];
while (n < 4) {
printf("请输入学号:");
fgets(num, 13, stdin);
printf("请输入密码:");
fgets(pass, 10, stdin);
if (strcmp(num, num1) == 0 && strcmp(pass, pass1) == 0) {
Sid + i;
return;
} else {
printf("你输入错误,请重新输入!\n");
n++;
}
}
printf("已达到最大输入次数,程序退出。\n");
}
相关问题
void Student(struct student stu[N]) { int i, n = 0; struct student *p; p=stu; char num1[] = "2021110300236"; char pass1[] = "111111"; char num[13], pass[10]; input:printf("请输入学号:"); scanf("%s",&num); printf("请输入密码:"); scanf("%s",&pass); for(i=0;i<sum;i++) { if(strcmp(num,num1)==0 && strcmp(pass,pass1)==0) { Sid+i; break; } } if(i==sum){ printf("你输入错误,请重新输入!\n"); goto input; } 优化代码
这段代码可以进行一些优化,例如:
1. 使用fgets代替scanf,可以避免输入缓冲区溢出的问题。
2. 在for循环中使用return语句代替break语句。
3. 不要使用goto语句,可以使用while循环代替。
下面是优化后的代码:
void Student(struct student stu[N]) {
int i, n = 0;
struct student *p;
p = stu;
char num1[] = "2021110300236";
char pass1[] = "111111";
char num[13], pass[10];
while (n < 4) {
printf("请输入学号:");
fgets(num, 13, stdin);
printf("请输入密码:");
fgets(pass, 10, stdin);
for (i = 0; i < sum; i++) {
if (strcmp(num, num1) == 0 && strcmp(pass, pass1) == 0) {
Sid + i;
return;
}
}
printf("你输入错误,请重新输入!\n");
n++;
}
printf("已达到最大输入次数,程序退出。\n");
}
#include<stdio.h> #define MAXN 100 struct student{ char name[20]; int score; }; void cnt_score( struct student *p, int n ); int main() { int i, n; struct student stu[MAXN]; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%s%d", stu[i].name, &stu[i].score); } cnt_score(stu, n); return 0; } void cnt_score(struct student *p, int n) { int cnt_a = 0, cnt_p = 0, cnt_f = 0; struct student stu[q];struct student *p=stu while ( p <= q ){ if ( (*p).score>=80&&(*p).score<=100 ) cnt_a++; else if ( (*p).score>=60&&(*p).score<=79 ) cnt_p++; else cnt_f++; p++; } printf("%d %d %d\n", cnt_a, cnt_p, cnt_f); }
这段代码存在以下问题:
1. 在 `cnt_score()` 函数中,定义了一个 `struct student` 类型的指针 `p`,但并没有给它赋值,也没有使用传入的指针参数 `p`,因此会导致编译时出现错误。
2. 在 `cnt_score()` 函数中,定义了一个 `struct student` 类型的数组 `stu`,但是并没有使用,也没有给它赋值,同样会导致编译时出现错误。
3. 在 `cnt_score()` 函数中,没有使用传入的指针参数 `p` 来统计成绩的数量,而是使用了未定义的变量 `q`,这也会导致编译时出现错误。
下面是修改后的代码:
```c
#include<stdio.h>
#define MAXN 100
struct student{
char name[20];
int score;
};
void cnt_score(struct student *p, int n);
int main() {
int i, n;
struct student stu[MAXN];
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%s%d", stu[i].name, &stu[i].score);
}
cnt_score(stu, n);
return 0;
}
void cnt_score(struct student *p, int n) {
int cnt_a = 0, cnt_p = 0, cnt_f = 0;
while (p < p + n) { // 使用传入的指针参数 p 来统计成绩的数量
if ((*p).score >= 80 && (*p).score <= 100)
cnt_a++;
else if ((*p).score >= 60 && (*p).score <= 79)
cnt_p++;
else
cnt_f++;
p++;
}
printf("%d %d %d\n", cnt_a, cnt_p, cnt_f);
}
```
修改后的代码中,使用传入的指针参数 `p` 来统计成绩的数量,同时删除了无用的变量和语句。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)