代码如下所示
#include<stdio.h>
int main()
{
char s, q, o;
s = q = o = 0;
float k;
int i, j, l, m, n;
i = j = l = m = n = 0;
k = 0.0;
float e;
int a, b, c, d, f, g, h;
float s1, s2, s3, s4, s5, s6, s7;
float score[10] = { s };
char name[10] = { s };
s3 = 0;
menu:
printf("欢迎使用大学评价系统,请根据提示输入相应的信息\n");
printf("\n1.开启评价系统 2.对已输入的评分进行排名(从小到大)\n");
scanf_s("%d", &g);
if (g == 1)
goto judge;
else
goto sort;
judge:
printf("\n请输入该大学的名称");
scanf_s("%10c", &s, 10);
if (s != 0)
name[i] = s;
else
goto judge;
printf("\n请输入该大学的校友会排名");
scanf_s("%d", &a);
if (a <= 300)
s1 = 0.5;
else if (a > 300)
s1 = 0.25;
printf("\n请输入该大学的软科排名");
scanf_s("%d", &b);
if (b <= 300)
s2 = 0.5;
else if (b > 300)
s2 = 0.25;
printf("\n请输入该大学的US世界排名,如无请输0");
scanf_s("%d", &c);
if (c == 0)
s3 = 0;
else if (c >= 0 && c <= 1600)
s3 = 1;
else if (c >= 1600)
s3 = 0.5;
printf("\n请输入该大学是否坐落于省会城市");
printf("\n1.是 2.否");
scanf_s("%d", &d);
if (d == 1)
s4 = 2;
else s4 = 1;
printf("\n请输入该大学的考研率,请以小数的形式输入");
scanf_s("%f", &e);
if (e <= 0.1)
s5 = 0.5;
else s5 = 1;
printf("\n请输入该大学是一本还是二本");
printf("\n1.一本 2.二本");
scanf_s("%d", &f);
if (f == 1)
s6 = 5;
else s6 = 3;
s7 = s1 + s2 + s3 + s4 + s5 + s6;
printf("\n该大学是的综合评分是%f", s7);
score[i] = s7;
i++;
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf_s("%d", &h);
if (h == 1)
goto menu;
else
goto judge;
sort:
for(i=0;i<9;i++)
for(j=9;j>i;j--)
if (score[i]>score[j])
{
q = name[i];
name[i] = name[j];
name[j] = q;
k = score[i];
score[i] = score[j];
score[j] = k;
}
for (i = 0; i < 10; i++)
printf("\n%c %f",name[i],score[i]);
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf_s("%d", &h);
if (h == 1)
goto menu;
else
goto judge;
return 0;
}
```c++
```c++
最终无法成功输出汉字,求各位学长帮我看看哪里有问题
这个程序存在一些问题,主要是输入和输出方面的问题。具体来说:
scanf_s()
函数的使用不正确,应该指定要读取的字符数,而不是缓冲区大小。例如,在输入大学名称时,可以使用以下代码:scanf_s("%10s", name[i], 10);
这里 %10s
表示最多读取 10 个字符,name[i]
表示读取的字符串存储在 name
数组中的第 i
个元素中,10
表示最多读取的字符数。
%s
格式化字符串来输出名称,例如:printf("\n%s %f", name[i], score[i]);
这里 %s
表示输出一个字符串,name[i]
表示要输出的字符串,%f
表示输出一个浮点数,score[i]
表示要输出的浮点数。
%s
格式化字符串,而不是 %c
,因为 %c
只能读取一个字符,而 %s
可以读取一整个字符串。除此之外,还有一些其他的问题,比如:
s
、q
、o
三个变量没有用到,可以直接省略。
在输入大学名称时,应该使用一个单独的变量来记录字符串的长度,以便在剩余部分填充空格。
在输入大学名称时,应该检查输入的字符是否为换行符,如果是,则表示输入结束。
在输入大学名称时,应该检查输入的字符是否为有效字符,如果不是,则需要清空输入缓冲区,重新读取。
下面是修改后的代码,供你参考:
#include <stdio.h>
int main()
{
float score[10] = {0};
char name[10][11] = {{0}};
int i = 0, j = 0, h = 0;
while (1) {
printf("\n欢迎使用大学评价系统,请根据提示输入相应的信息\n");
printf("\n1.开启评价系统 2.对已输入的评分进行排名(从小到大)\n");
scanf_s("%d", &h);
if (h == 1) {
printf("\n请输入该大学的名称(最多10个字符):");
int len = 0;
while (1) {
char c = getchar();
if (c == '\n') { // 输入结束
break;
}
if (c < ' ' || c > '~') { // 非法字符,清空输入缓冲区
while (getchar() != '\n');
continue;
}
if (len < 10) {
name[i][len++] = c;
}
}
name[i][len] = '\0'; // 填充空格
printf("\n请输入该大学的校友会排名:");
int a;
scanf_s("%d", &a);
float s1 = (a <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的软科排名:");
int b;
scanf_s("%d", &b);
float s2 = (b <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的US世界排名,如无请输0:");
int c;
scanf_s("%d", &c);
float s3 = (c == 0) ? 0 : (c >= 0 && c <= 1600) ? 1 : 0.5;
printf("\n请输入该大学是否坐落于省会城市:");
printf("\n1.是 2.否");
int d;
scanf_s("%d", &d);
float s4 = (d == 1) ? 2 : 1;
printf("\n请输入该大学的考研率,以小数形式输入:");
float e;
scanf_s("%f", &e);
float s5 = (e <= 0.1) ? 0.5 : 1;
printf("\n请输入该大学是一本还是二本:");
printf("\n1.一本 2.二本");
int f;
scanf_s("%d", &f);
float s6 = (f == 1) ? 5 : 3;
float s7 = s1 + s2 + s3 + s4 + s5 + s6;
printf("\n该大学的综合评分是%f", s7);
score[i] = s7;
i++;
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf_s("%d", &h);
if (h == 1) {
continue;
}
} else if (h == 2) {
for (i = 0; i < 9; i++) {
for (j = 9; j > i; j--) {
if (score[i] > score[j]) {
float tmp = score[i];
score[i] = score[j];
score[j] = tmp;
char name_tmp[11];
strcpy_s(name_tmp, 11, name[i]);
strcpy_s(name[i], 11, name[j]);
strcpy_s(name[j], 11, name_tmp);
}
}
}
printf("\n排名结果(从小到大):\n");
for (i = 0; i < 10; i++) {
printf("%s %f\n", name[i], score[i]);
}
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf_s("%d", &h);
if (h == 1) {
continue;
}
}
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: