#include<stdio.h> char f(int n){ char result; int k=0; k=(n>=93)+(n>=85)+(n>=80)+(n>=75)+(n>=70)+(n>=65)+(n>=60); switch(k){ case 0:result='F';break; case 1:result='D';break; case 2:result='C';break; case 3:result='C+';break; case 4:result='B';break; case 5:result='B+';break; case 6:result='A';break; case 7:result='A+';break; } return result; } int main(){ int N; printf("Name_f Name_l stu_id score retake GPA rank\n"); for(N=0; N<100; N++){ char lname,fname; int n,m,f1,f2; f1=n/100000000; scanf("%c",&lname); getchar(); scanf("%c",&fname); scanf("%d %d",&n,&m); if(n<202000000000 || n>202300000000) continue; if(f1=2022) f2=0; else f2=1; printf("%c %c %d %d %d %c\n",lname,fname,n,m,f2,f(n)); } return 0; }
时间: 2023-12-09 20:02:49 浏览: 128
这段代码有几个问题?
1. 函数f中,返回值类型为char,但是在 case 3 中返回了字符串 "C+",这是不正确的。
2. 在 main 函数中,f1 的值没有被初始化,应该在读入学号 n 后进行初始化,避免出现未定义行为。
3. 在判断 f1 是否等于 2022 的时候,应该使用等于号 "==" 而不是赋值运算符 "="。
4. 在输出 GPA 的时候,应该进行计算,而不是直接输出 f2 的值。
5. 在输出 GPA 和 rank 的时候,没有进行换行,这样输出的结果会比较混乱。
6. 在读入 lname 和 fname 的时候,应该使用 scanf("%c %c", &lname, &fname),这样可以避免在输入时出现空格。
7. 程序中没有进行错误处理,如果输入的数据格式不符合要求,程序会出现异常终止。应该在输入的时候进行错误处理,避免程序崩溃。
8. 在函数 f 中,变量 k 的初始值没有被赋值。虽然在后面的语句中会对 k 进行赋值,但是这种写法会让代码难以理解。应该在定义变量 k 的时候就对其进行初始化,避免出现问题。
9. 在函数 f 中,变量 result 的初始值没有被赋值。虽然在后面的语句中会对 result 进行赋值,但是这种写法会让代码难以理解。应该在定义变量 result 的时候就对其进行初始化,避免出现问题。
相关问题
学习-编写函数将两个字符串连接起来#include<stdio.h> #define N 80 char * mystrcat(char ch1[],char ch2[]); int mystrlen(const char str[ ])
好的,这是一个关于字符串操作的问题。对于第一个问题,可以使用以下代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 80
char* mystrcat(char ch1[], char ch2[]) {
char* result = (char*)malloc(sizeof(char) * (strlen(ch1) + strlen(ch2) + 1));
strcpy(result, ch1);
strcat(result, ch2);
return result;
}
int mystrlen(const char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int main() {
char ch1[N], ch2[N];
printf("请输入两个字符串:\n");
scanf("%s%s", ch1, ch2);
char* result = mystrcat(ch1, ch2);
printf("合并后的字符串为:%s\n", result);
free(result);
printf("第一个字符串的长度为:%d\n", mystrlen(ch1));
printf("第二个字符串的长度为:%d\n", mystrlen(ch2));
return 0;
}
```
其中,`mystrcat` 函数接受两个字符串参数,使用 `malloc` 分配新的内存空间来存储合并后的字符串,并使用 `strcpy` 和 `strcat` 函数拼接两个字符串。最后,返回合并后的字符串指针。
`mystrlen` 函数接受一个字符串参数,通过循环计算字符串的长度,并返回字符串的长度值。
改正#include <stdio.h> #include <iostream> int main() char fat_char_from_modem() { char ch; ch=read_modem(); return(ch&127); }
这段代码仍然存在一些问题:
1. main 函数中没有调用 fat_char_from_modem 函数,需要在 main 函数中调用该函数。
2. 函数的返回类型是 char,但是函数中没有明确的 return 语句,需要添加一个 return 语句。
3. 函数中使用了一个未定义的函数 read_modem(),需要在代码中定义该函数或者引入该函数的头文件。
4. 函数中的 return 语句返回了 ch & 127,这个操作的意义是将 ch 的最高位清零,但是没有明确说明这个操作的目的和意义,需要根据实际需求进行判断是否需要这个操作。
综上所述,这段代码需要进行修正和完善。以下是可能的修正版本:
```
#include <stdio.h>
char read_modem(); // 假设这是一个定义在其他文件中的函数,需要在这里声明一下
char fat_char_from_modem() {
char ch;
ch = read_modem();
return (ch & 127); // 这个操作的目的是将 ch 转换为 ASCII 码范围内的字符,如果不需要这个操作,可以直接返回 ch
}
int main() {
char result;
result = fat_char_from_modem();
printf("Result: %c\n", result); // 输出结果
return 0;
}
```
阅读全文
相关推荐

















