题目提示
M个同学N门课成绩处理,字符数组存放姓名
基本要求
用for或while或do-while循环语句实现,能对数据进行排序的程序,能运用二维数组能熟练对学生成绩进行输入,查找,排序,输出等功能并提交程序
基本思路
这是一个较为基础的小代码,小编便直接将输入、输出、排序、查找一气呵成了,有需要的话可以自行添加菜单栏,设置选项,在输入错误时给出合理的返回值,可以编写通过查询姓名等选项查看成绩,还可以设置学生数、科目数从键盘获取,再改写数组成指针或者给个宏定义都欧克的,这样可以进一步进行插入、删除的操作,嫌输入麻烦还可以直接使用文件,这里只给最简单的代码
程序代码
#include <iostream>
#include <cstring>
using namespace std;
struct student//定义结构体
{
int num;
char name[5];
int score[3];
double ave;
}stu[5];
int main()
{
int i, j, sum;
struct student temp;
//输入
for (i = 0; i < 5; i++)
{
cout<<"输入第"<< i + 1 <<"个学生学号及姓名: ";
cin >> stu[i].num;
cin >> stu[i].name;
cout<<"输入三门课成绩: ";
cin >> stu[i].score[0] >> stu[i].score[1] >> stu[i].score[2];
sum = 0;
sum = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];
stu[i].ave = sum / 3.0;
}
//输出
for (i = 0; i < 5; i++)
{
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score[0] << " "
<< stu[i].score[1] << " " << stu[i].score[2] << " " << stu[i].ave << endl;
}
cout << endl;
//根据平均分冒泡排序
for (i = 5; i > 0; i--)
{
for (j = 0; j < i - 1; j++)
{
if (stu[j].ave>stu[j + 1].ave)
{
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
//排序后输出
for (i = 0; i < 5; i++)
{
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score[0] << " "
<< stu[i].score[1] << " " << stu[i].score[2] << " " << stu[i].ave;
cout << endl;
}
cout << endl;
//查找
int flag;
cout << "输入要查找的学号: ";
cin >> flag;
for (i = 0; i < 5; i++)
{
if (stu[i].num == flag)
{
cout << stu[i].num << " " << stu[i].name << " " << stu[i].score[0] << " "
<< stu[i].score[1] << " " << stu[i].score[2] << " " << stu[i].ave;
}
}
return 0;
}