我想建立一个学生成绩管理系统。 其他功能是正确的。但是排序功能总是有一点问题。第一行全部为0.排序功能将显示全部为0的第一行
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class student
{
int num;
char name[20];
char class1[20];
float Chinese;
float math;
float English;
float aver;//平均成绩
public:
void set()
{
cout << "\t学生学号:";
cin >> num;
cout << "\t学生姓名:";
cin >> name;
cout << "\t学生班级:";
cin >> class1;
cout << "\t语文成绩:";
cin >> Chinese;
cout << "\t英语成绩:";
cin >> English;
cout << "\t数学成绩:";
cin >> math;
}
void show()
{
cout << "该学生的学号:" << num << endl;
cout << "该学生的姓名:" << name << endl;
cout << "该学生的班级:" << class1 << endl;
cout << "该学生的语文成绩:" << Chinese << endl;
cout << "该学生的数学成绩:" << math << endl;
cout << "该学生的英语成绩:" << English << endl;
}
friend void show();
friend void search();
friend void change();
friend void add();
friend void sort();
friend void del();
};
void input();
void show();
void search();
void change();
void add();
void sort();
void del();
student s[100];
int n = 0;
int main()
{
int select;
while (1)
{
system("cls");
cout << "\t ******************欢迎使用******************\n";
cout << "\t **************学生成绩管理系统**************\n";
cout << "\t *------------------------------------------*\n";
cout << "\t * 1——录入学生信息 *\n";
cout << "\t * 2——显示学生信息 *\n";
cout << "\t * 3——查询学生信息 *\n";
cout << "\t * 4——修改学生信息 *\n";
cout << "\t * 5——添加学生信息 *\n";
cout << "\t * 6——排序学生信息 *\n";
cout << "\t * 7——删除学生信息 *\n";
cout << "\t * 0——退出 *\n";
cout << "\t *------------------------------------------*\n";
cout << "\t 你要输入的编号是(0--7):";
cin >> select;
if (select == 0) break;
switch (select) {
case 1:
input(); //调用input函数录入数据
system("pause");
break;
case 2:
show(); //调用show函数显示学生信息
system("pause");
break;
case 3:
search(); //调用search函数查询学生信息
system("pause");
break;
case 4:
change(); //调用change函数修改学生信息
system("pause");
break;
case 5:
add(); //调用add函数添加学生信息
system("pause");
break;
case 6:
paixu(); //调用add函数添加学生信息
system("pause");
break;
case 7:
del(); // 调用del函数删除学生信息
system("pause");
break;
system("pause");
default:
cout << "没有此选项,请重新选择!" << endl;
}
}
return 0;
}
void input() //录入学生信息的函数
{
char sel;
do
{
n++;
cout << "\t请输入第" << n << "个学生信息:\t" << endl;
s[n].set();
cout << "是否要继续录入学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
}
void show()
{
cout << left; //设置输出左对齐
cout << setw(10) << "学生学号"
<< setw(10) << "学生姓名"
<< setw(13) << "学生班级"
<< setw(12) << "语文成绩"
<< setw(12) << "数学成绩"
<< setw(12) << "英语成绩"
<< endl;
for (int i = 1; i <= n; i++)
{
cout << setw(10) << s[i].num
<< setw(10) << s[i].name
<< setw(13) << s[i].class1
<< setw(12) << s[i].Chinese
<< setw(12) << s[i].math
<< setw(12) << s[i].English << endl;
}
cout << endl;
}
void search()//查询
{
int a, b, i; char c[20];
cout << "请选择查询方式:" << endl;
cout << "按学生姓名查询请按1" << endl;
cout << "按学生学号查询请按2" << endl;
cin >> a;
switch (a)
{
case 1:
{char sel;
do {
cout << "请输入你要查的学生的姓名:" << endl;
cin >> c;
for (i = 1; i <= n; i++)
if (strcmp(s[i].name, c) == 0)
{
cout << "你要查的学生信息为:" << endl;
cout << "学生学号:" << s[i].num << endl << "学生姓名:"
<< s[i].name << endl << "学生班级:"
<< s[i].class1 << endl << "语文成绩:"
<< s[i].Chinese << endl << "数学成绩:"
<< s[i].math << endl << "英语成绩:"
<< s[i].English << endl;
break;
}
else if (strcmp(s[i].name, c) != 0 && i == n)
{
cout << "没有这个学生!" << endl; break;
}
cout << "是否要继续查询学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
break;
}
case 2:
{ char sel;
do {
cout << "请输入你要查的学生的学号:" << endl;
cin >> b;
for (i = 1; i <= n; i++)
if (s[i].num == b)
{
cout << "你要查的学生信息为:" << endl;
cout << "学生学号:" << s[i].num << endl << "学生姓名:"
<< s[i].name << endl << "学生班级:"
<< s[i].class1 << endl << "语文成绩:"
<< s[i].Chinese << endl << "数学成绩:"
<< s[i].math << endl << "英语成绩:"
<< s[i].English << endl;
break;
}
else if (s[i].num != b&&i == n)
{
cout << "没有这个学生!请核对后再输入!" << endl; break;
}
cout << "是否要继续查询学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
break; }
default:
cout << "您的输入有误!" << endl;
}
}
void change()
{
{ char sel;
int d, i, e, f, g = 0;
do {
cout << "请输入你要修改的学生的学号:" << endl;
cin >> d;
for (i = 1; i <= n; i++)
if (s[i].num == d)
{
cout << "你要修改的学生信息为:" << endl;
cout << "学生学号:" << s[i].num << endl << "学生姓名:"
<< s[i].name << endl << "学生班级:"
<< s[i].class1 << endl << "语文成绩:"
<< s[i].Chinese << endl << "数学成绩:"
<< s[i].math << endl << "英语成绩:"
<< s[i].English << endl;
e = 1;
break;
}
else if (s[i].num != d&&i == n)
{
cout << "没有这个学生!请核对后再输入!" << endl;
e = 0; break;
}
while (e == 1)
{
{cout << "请输入该学生要修改的信息对应的数字:" << endl;
cout << "学生学号:1"
<< '\t' << "学生姓名:2"
<< '\t' << "学生班级:3"
<< '\t' << "语文成绩:4"
<< '\t' << "数学成绩:5"
<< '\t' << "英语成绩:6" << endl;
}
cin >> f;
switch (f)
{
case 1:cout << "\t新的学生学号为:";
cin >> s[i].num; break;
case 2:cout << "\t新的学生姓名为:";
cin >> s[i].name; break;
case 3:cout << "\t新的学生班级为:";
cin >> s[i].class1; break;
case 4:cout << "\t新的语文成绩为:";
cin >> s[i].Chinese; break;
case 5:cout << "\t新的数学成绩为:";
cin >> s[i].math; break;
case 6:cout << "\t新的英语成绩为:";
cin >> s[i].English; break;
default:cout << "您的输入有误,请重新输入!";
cin >> f;
}
e = 0;
g = 1;
}
if (g == 1)
{
cout << "修改成功!";
}
cout << "是否要继续修改学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
}
}
void add()
{
char sel;
do
{
n++;
cout << "\n请输入第" << n << "个学生信息:\n";
cout << "\t学生学号:";
cin >> s[n].num;
cout << "\t学生姓名:";
cin >> s[n].name;
cout << "\t学生班级:";
cin >> s[n].class1;
cout << "\t语文成绩:";
cin >> s[n].Chinese;
cout << "\t数学成绩:";
cin >> s[n].math;
cout << "\t英语成绩:";
cin >> s[n].English;
cout << "是否要继续添加学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
}
void sort()
{
student w;
for (int i = 0; i<n - 1; i++)
for (int j = 0; j<n - 1-i; j++)
if (s[j].aver < s[j + 1].aver)
{
w = s[j]; s[j] = s[j + 1]; s[j + 1] = w;
}
cout << "按平均成绩排序结果:" << endl;
cout << left; //设置输出左对齐
cout << setw(10) << "学生学号"
<< setw(10) << "学生姓名"
<< setw(13) << "学生班级"
<< setw(12) << "语文成绩"
<< setw(12) << "数学成绩"
<< setw(12) << "英语成绩"
<< setw(12) << "平均成绩"
<< endl;
for (int k = 0; k <n ; k++)
{
s[k].aver = (s[k].math + s[k].Chinese + s[k].English) / 3;
cout << setw(10) << s[k].num
<< setw(10) << s[k].name
<< setw(13) << s[k].class1
<< setw(12) << s[k].Chinese
<< setw(12) << s[k].math
<< setw(12) << s[k].English
<< setw(12) << s[k].aver << endl;
}
}
void del()
{
int j, i, k; char c[10], sel, y;
do {
cout << "请输入你要删除的学生的姓名:" << endl;
cin >> c;
for (i = 1; i <= n; i++)
if (strcmp(s[i].name, c) == 0)
{
cout << "你要删除的学生信息为:" << endl;
cout << "学生学号:" << s[i].num << endl
<< "学生姓名:" << s[i].name << endl
<< "学生班级:" << s[i].class1 << endl
<< "语文成绩:" << s[i].Chinese << endl
<< "数学成绩:" << s[i].math << endl
<< "英语成绩:" << s[i].English << endl;
cout << "确认删除?(y/n):";
cin >> y;
if (y == 'Y' || y == 'y')
{
for (j = i; j <= n + 1; j++)
{
s[j] = s[j + 1];
} k = 1;
}
else
cout << "删除失败!" << endl;
break;
}
else if (strcmp(s[i].name, c) != 0 && i == n)
{
cout << "没有这个学生!请核对后再输入!" << endl;
k = 0; break;
}
while (k == 1)
{
if (y == 'Y' || y == 'y')
cout << "删除成功!";
n--;
k = 0;
}
cout << "是否要继续删除学生信息(Y/N):";
cin >> sel;
} while (sel == 'Y' || sel == 'y');
}
答案 0 :(得分:0)
在add
函数中,在将其用作n
数组的索引之前,请增加s
。这会使s[0]
为空(因此您有一行全零),s[n]
包含最后一个条目(因此它不会包含在排序中)。
将所有输入存储在add
,然后增加n
。