scanf("%6.3d")
时间: 2023-12-15 14:32:19 浏览: 36
scanf("%6.3d")是一个格式化输入函数,用于读取整数。其中6表示字段宽度,3表示小数点后的位数,d表示读取整数。如果读取的整数位数超过6,则会被截断;如果小数点后的位数超过3,则会四舍五入。例如,如果输入为12345,则读取的整数为123.000。如果输入为12.345,则读取的整数为012.000。
范例:请问如何使用scanf读取字符串?
```c
char str[100];
scanf("%s", str);
printf("输入的字符串为:%s\n", str);
```
相关问题
优化此程序段:void gradelist(struct student *list,int amount) { int i=0; printf("等级 人数 百分比 \n"); int j=0; for(i=0;i<amount;i++) { if(list[i].grade=='A') j++; } printf("\t\t A %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='B') j++; } printf("\t\t B %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='C') j++; } printf("\t\t C %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='D') j++; } printf("\t\t D %d %6.3lf \n",j,(double)j/amount); j=0; for(i=0;i<amount;i++) { if(list[i].grade=='E') j++; } printf("\t\t E %d %6.3lf \n",j,(double)j/amount); printf("\n\n\n ****** 请摁任意键继续! ******"); getch(); } void find(struct student *list,int amount) { int i=0; int flag=1; char temp[10]; dispinfo(); int flag2; do { flag2=1; printf(" 请输入学号:"); scanf("%s",temp); for(i=0;i<amount;i++) { flag=strcmp(temp,list[i].code); if(flag==0) { flag2=0; break; } } if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n"); }while(flag2); printf("\t\t 学号 总评 等级 \n"); printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade); printf("\n\n\n ****** 按任意键继续! ****** \n"); getch(); } void save(struct student *list,int amount) { int i=0; FILE *fp; fp=fopen("C:\Users\dell\Desktop\软件设计-源文件","w"); fprintf(fp,"\t\t 学号 总评 等级 \n"); for(i=0;i<amount;i++) { fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade); } printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n"); fclose(fp); getch(); } int main() { system("color F4"); struct student list[100]; int choice; int amount; while(1) { dispinfo(); set(&choice); switch(choice) { case '1':amount=duqu(list);break; case '2':calculate(list,amount);break; case '3':findgrade(list,amount);break; case '4':paixu(list,amount);break; case '5':find(list,amount);break; case '6':gradelist(list,amount);break; case '7':save(list,amount);break; case '0': system("cls"); printf("\n\n\n\n\n\n\n\n\n\n\n ******* 谢谢使用!欢迎再次光临! ******\n"); getch(); break; } } return 0; }
void gradelist(struct student *list,int amount) {
int i=0;
printf("等级 人数 百分比 \n");
int grades[5] = {0}; // 用数组记录每个等级的人数
for(i=0;i<amount;i++) {
switch(list[i].grade) {
case 'A': grades[0]++; break;
case 'B': grades[1]++; break;
case 'C': grades[2]++; break;
case 'D': grades[3]++; break;
case 'E': grades[4]++; break;
}
}
char grade_labels[5] = {'A', 'B', 'C', 'D', 'E'}; // 存放等级标签的数组
for(i=0;i<5;i++) {
printf("\t\t %c %d %6.3lf \n", grade_labels[i], grades[i], (double)grades[i]/amount);
}
printf("\n\n\n ****** 请摁任意键继续! ******");
getch();
}
void find(struct student *list,int amount) {
int i=0;
int flag=1;
char temp[10];
dispinfo();
int flag2;
do {
flag2=1;
printf(" 请输入学号:");
scanf("%s",temp);
for(i=0;i<amount;i++) {
flag=strcmp(temp,list[i].code);
if(flag==0) {
flag2=0;
break;
}
}
if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n");
}while(flag2);
printf("\t\t 学号 总评 等级 \n");
printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade);
printf("\n\n\n ****** 按任意键继续! ****** \n");
getch();
}
void save(struct student *list,int amount) {
int i=0;
FILE *fp;
fp=fopen("C:\Users\dell\Desktop\软件设计-源文件","w");
fprintf(fp,"\t\t 学号 总评 等级 \n");
for(i=0;i<amount;i++) {
fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade);
}
printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n");
fclose(fp);
getch();
}
int main() {
system("color F4");
struct student list[100];
int choice;
int amount;
while(1) {
dispinfo();
set(&choice);
switch(choice) {
case '1':amount=duqu(list);break;
case '2':calculate(list,amount);break;
case '3':findgrade(list,amount);break; // 函数名与代码段中不一致,这里修改为正确的函数名
case '4':paixu(list,amount);break;
case '5':find(list,amount);break;
case '6':gradelist(list,amount);break;
case '7':save(list,amount);break;
case '0':
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n ******* 谢谢使用!欢迎再次光临! ******\n");
getch();
return 0; // 程序结束,直接返回
default:
printf(" 无效的选项,请重新输入!\n");
break;
}
}
}
给这段代码添加中文注释:void findgrade(struct student list[], int amount) { dispinfo(); printf("\n\n\n 请选择您需要查询的等级(输入大写A(优)/B(良)C(中)/D(及格)/E(不及格)): "); char g = getchar(); int count = 0; printf("\t\t 学号 总评 等级 \n"); for(int i = 0; i < amount; i++) { if(list[i].grade == g) { count++; printf("\t\t%10s %6.1f %c \n", list[i].code, list[i].score, list[i].grade); } } printf("\n\n 等级为%c总共有%d人, 总人数%d ,占总人数的%.2lf\n", g, count, amount, (double)count/amount); printf("\n\n\n ****** 按任意键继续! ****** \n"); getch(); } void gradelist(struct student *list,int amount) { int i=0; printf("等级 人数 百分比 \n"); int grades[5] = {0}; // 用数组记录每个等级的人数 for(i=0;i<amount;i++) { switch(list[i].grade) { case 'A': grades[0]++; break; case 'B': grades[1]++; break; case 'C': grades[2]++; break; case 'D': grades[3]++; break; case 'E': grades[4]++; break; } } char grade_labels[5] = {'A', 'B', 'C', 'D', 'E'}; // 存放等级标签的数组 for(i=0;i<5;i++) { printf("\t\t %c %d %6.3lf \n", grade_labels[i], grades[i], (double)grades[i]/amount); } printf("\n\n\n ****** 请摁任意键继续! ******"); getch(); } void find(struct student *list,int amount) { int i=0; int flag=1; char temp[10]; dispinfo(); int flag2; do { flag2=1; printf(" 请输入学号:"); scanf("%s",temp); for(i=0;i<amount;i++) { flag=strcmp(temp,list[i].code); if(flag==0) { flag2=0; break; } } if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n"); }while(flag2); printf("\t\t 学号 总评 等级 \n"); printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade); printf("\n\n\n ****** 按任意键继续! ****** \n"); getch(); } void save(struct student *list,int amount) { int i=0; FILE *fp; fp=fopen("C:\Users\dell\Desktop\软件设计-源文件\A3\out.txt","w"); fprintf(fp,"\t\t 学号 总评 等级 \n"); for(i=0;i<amount;i++) { fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade); } printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n"); fclose(fp); getch(); }
void findgrade(struct student list[], int amount) {
dispinfo(); // 显示菜单信息
printf("\n\n\n 请选择您需要查询的等级(输入大写A(优)/B(良)C(中)/D(及格)/E(不及格)): ");
char g = getchar(); // 获取用户输入等级
int count = 0; // 计数等级为g的学生人数
printf("\t\t 学号 总评 等级 \n");
for(int i = 0; i < amount; i++) {
if(list[i].grade == g) { // 如果学生等级为g
count++; // 计数器加一
printf("\t\t%10s %6.1f %c \n", list[i].code, list[i].score, list[i].grade); // 输出该学生的信息
}
}
printf("\n\n 等级为%c总共有%d人, 总人数%d ,占总人数的%.2lf\n", g, count, amount, (double)count/amount); // 输出等级为g的学生人数和占比
printf("\n\n\n ****** 按任意键继续! ****** \n");
getch(); // 等待用户按键
}
void gradelist(struct student *list,int amount) {
int i=0;
printf("等级 人数 百分比 \n");
int grades[5] = {0}; // 用数组记录每个等级的人数
for(i=0;i<amount;i++) {
switch(list[i].grade) {
case 'A': grades[0]++; break;
case 'B': grades[1]++; break;
case 'C': grades[2]++; break;
case 'D': grades[3]++; break;
case 'E': grades[4]++; break;
} // 统计每个等级的人数
}
char grade_labels[5] = {'A', 'B', 'C', 'D', 'E'}; // 存放等级标签的数组
for(i=0;i<5;i++) {
printf("\t\t %c %d %6.3lf \n", grade_labels[i], grades[i], (double)grades[i]/amount); // 输出每个等级的人数和占比
}
printf("\n\n\n ****** 请摁任意键继续! ******");
getch(); // 等待用户按键
}
void find(struct student *list,int amount) {
int i=0; int flag=1; char temp[10];
dispinfo(); // 显示菜单信息
int flag2;
do {
flag2=1;
printf(" 请输入学号:");
scanf("%s",temp); // 获取用户输入的学号
for(i=0;i<amount;i++) {
flag=strcmp(temp,list[i].code); // 比较用户输入的学号和学生信息中的学号
if(flag==0) { // 如果学号匹配成功
flag2=0;
break;
}
}
if(flag2==1) printf(" 您所输入的学号不存在,请重新输入!\n"); // 如果没有匹配成功,提示用户重新输入
}while(flag2);
printf("\t\t 学号 总评 等级 \n");
printf("\t\t%10s %6.1f%6c \n",list[i].code,list[i].score,list[i].grade); // 输出匹配成功的学生信息
printf("\n\n\n ****** 按任意键继续! ****** \n");
getch(); // 等待用户按键
}
void save(struct student *list,int amount) {
int i=0;
FILE *fp;
fp=fopen("C:\Users\dell\Desktop\软件设计-源文件\A3\out.txt","w"); // 打开文件
fprintf(fp,"\t\t 学号 总评 等级 \n");
for(i=0;i<amount;i++) {
fprintf(fp,"\t\t %10s %6.1f %6c \n",list[i].code,list[i].score,list[i].grade); // 将学生信息写入文件中
}
printf("\n\n ****** 输出成功!请查看!摁任意键继续 ****** \n");
fclose(fp); // 关闭文件
getch(); // 等待用户按键
}
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)