代码如下所示
#include <stdio.h>
#include <cstring>
int main()
{
double 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);
double s1 = (a <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的软科排名:");
int b;
scanf_s("%d", &b);
double s2 = (b <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的US世界排名,如无请输0:");
int c;
scanf_s("%d", &c);
double s3 = (c == 0) ? 0.0 : (c >= 0 && c <= 1600) ? 1.0 : 0.5;
printf("\n请输入该大学是否坐落于省会城市:");
printf("\n1.是 2.否");
int d;
scanf_s("%d", &d);
double s4 = (d == 1) ? 2.0 : 1.0;
printf("\n请输入该大学的考研率,以小数形式输入:");
float e;
scanf_s("%f", &e);
double s5 = (e <= 0.1) ? 0.5 : 1.0;
printf("\n请输入该大学是一本还是二本:");
printf("\n1.一本 2.二本");
int f;
scanf_s("%d", &f);
double s6 = (f == 1) ? 5.0 : 3.0;
double 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])
{
double 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;
}
整体修改完善如下,改动处见注释,供参考:
#include <stdio.h>
#include <cstring>
int main()
{
double score[10] = { 0 };
char name[10][11] = { {0} };
int i = 0, j = 0, h = 0, k = 0; // 修改
while (1)
{
if (i >= 10) break; // 修改 跳出整个系统
printf("\n欢迎使用大学评价系统,请根据提示输入相应的信息\n");
printf("\n1.开启评价系统 2.对已输入的评分进行排名(从小到大)\n");
scanf_s("%d", &h);
getchar(); // 修改
if (h == 1)
{
printf("输入该大学的名称(最多10个字符):");
int len = 0;
while (1)
{
char c = getchar();
if (c == '\n')
{
break; // 输入结束
}
if (c < ' ' || c > '~')
{
while ((getchar()) != '\n'); //while (getchar() != '\n'); // 修改
printf("非法字符,请重新输入学校名称:"); // 修改
len = 0; // 修改
continue; // 非法字符,清空输入缓冲区
}
if (len < 10)
{
name[i][len++] = c;
}
}
name[i][len] = '\0'; // 填充 结束符 修改
printf("\n请输入该大学的校友会排名:");
int a;
scanf_s("%d", &a);
double s1 = (a <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的软科排名:");
int b;
scanf_s("%d", &b);
double s2 = (b <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的US世界排名,如无请输0:");
int c;
scanf_s("%d", &c);
double s3 = (c == 0) ? 0.0 : (c > 0 && c <= 1600) ? 1.0 : 0.5; //(c >= 0 && c <= 1600) 修改
printf("\n请输入该大学是否坐落于省会城市:");
printf("\n1.是 2.否");
int d;
scanf_s("%d", &d);
double s4 = (d == 1) ? 2.0 : 1.0;
printf("\n请输入该大学的考研率,以小数形式输入:");
float e;
scanf_s("%f", &e);
double s5 = (e <= 0.1) ? 0.5 : 1.0;
printf("\n请输入该大学是一本还是二本:");
printf("\n1.一本 2.二本");
int f;
scanf_s("%d", &f);
double s6 = (f == 1) ? 5.0 : 3.0;
double 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 (k = 0; k < i; k++) //for (i = 0; i < 9; i++) 修改
{
for (j = i - 1; j > k; j--) //for (j = 9; j > i; j--) 修改
{
if (score[k] > score[j]) // 修改
{
double tmp = score[k]; // 修改
score[k] = score[j]; // 修改
score[j] = tmp;
char name_tmp[11];
strcpy_s(name_tmp, 11, name[k]); // 修改
strcpy_s(name[k], 11, name[j]); // 修改
strcpy_s(name[j], 11, name_tmp);
}
}
}
printf("\n排名结果(从小到大):\n");
for (k = 0; k < i; k++) // for (i = 0; i < 10; i++) 修改
{
printf("%s %f\n", name[k], score[k]); // 修改
}
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf_s("%d", &h);
if (h == 1)
{
continue;
}
}
}
return 0;
}
效果如图, 我用的是c11, 如有帮助给个采纳点赞加关注 ,下个问题不迷路 , 我先不说问题在哪, 你先看, 如果看懂了,对你帮助是最大的, 看不懂在叫我,我告诉你 :
#include <stdio.h>
#include <string.h>
int main()
{
double 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("%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("%d", &a);
double s1 = (a <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的软科排名:");
int b;
scanf("%d", &b);
double s2 = (b <= 300) ? 0.5 : 0.25;
printf("\n请输入该大学的US世界排名,如无请输0:");
int c;
scanf("%d", &c);
double s3 = (c == 0) ? 0.0 : (c >= 0 && c <= 1600) ? 1.0 : 0.5;
printf("\n请输入该大学是否坐落于省会城市:");
printf("\n1.是 2.否");
int d;
scanf("%d", &d);
double s4 = (d == 1) ? 2.0 : 1.0;
printf("\n请输入该大学的考研率,以小数形式输入:");
float e;
scanf("%f", &e);
double s5 = (e <= 0.1) ? 0.5 : 1.0;
printf("\n请输入该大学是一本还是二本:");
printf("\n1.一本 2.二本");
int f;
scanf("%d", &f);
double s6 = (f == 1) ? 5.0 : 3.0;
double s7 = s1 + s2 + s3 + s4 + s5 + s6;
printf("\n该大学的综合评分是%f", s7);
score[i] = s7;
i++;
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf("%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])
{
double tmp = score[i];
score[i] = score[j];
score[j] = tmp;
char name_tmp[11];
strcpy(name_tmp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], name_tmp);
}
}
}
printf("\n排名结果(从小到大):\n");
for (i = 0; i < 10; i++)
{
printf("%s %f\n", name[i], score[i]);
}
printf("\n1.回到主菜单 2.继续录入新大学的数据");
scanf("%d", &h);
if (h == 1)
{
continue;
}
}
}
return 0;
}
定义
它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。