题目内容:
下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。
#include<stdio.h>
int main()
{
char input[100];
int score = 0;
char grade;
while(1){
printf("Please input score:\n");
gets(input);
for(int tmp=0;input[tmp]!='\0';tmp++){
if(input[tmp]>='0'&input[tmp]<='9'){
score*=10;
score+=(int)(input[tmp]-'0');
} else{
score=-1;
break;
}
}
if (score < 0 || score > 100){
score=0;
printf("Input error!\n");
}
else{
break;
}
}
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}