这最后一个模块的冒泡排列该怎么写
这段代码要添加学号排列这个功能该怎么做,用冒泡排列,我知道是调用前面定义的num,但是没明白怎么调用,然后再怎写循环体,新人,刚学c,问题很多,多多包涵#include<iostream>
#include<fstream>
#include <string>
using namespace std;
#define M 100
int N = 0;
class student
{
public:
student();
void set();
void add();
void del();
void change();
void search();
void arrange();
private:
int num, chinese, math, c;
string name;
};
student::student() {}
void student::set()
{
int i;
for (i = 0; i < M; i++)
{
cout << "姓名输入‘0’时,停止输入" << endl;
cout << "请输入学生姓名" << endl;
cin >> name;
if (name == "0")break;
if (name != "0")
{
N++;
cout << "请输入学号" << endl;
cin >> num;
cout << "请输入语文成绩" << endl;
cin >> chinese;
cout << "请输入数学成绩" << endl;
cin >> math;
cout << "请输入c语言成绩" << endl;
cin >> c;
}
cout << endl;
}
}
void student::add()
{
N++;
cout << "请输入添加的信息" << endl;
cout << "请输入学号" << endl;//这里缺少学号是否同号判断,自己改一下吧
cin >> num;
cout << "请输入姓名" << endl;
cin >> name;
cout << "请输入语文成绩" << endl;
cin >> chinese;
cout << "请输入数学成绩" << endl;
cin >> math;
cout << "请输入c语言成绩" << endl;
cin >> c;
cout << "添加成功" << endl;
}
void student::del()
{
int k, i, j, d = 0;//d用来判断是否有要删除的学生记录。
cout << "清除如要删除的学生学号" << endl;
cin >> k;
for (i = 0; i <= N; i++)
{
if (num == k)
{
for (j = i; j < N; j++)
{
num = num;
name = name;
chinese = chinese;
math = math;
c = c;
i--;/*为什么加上这句自己想想吧,如果后面一条记录也和前面记录一样,比如输入了两个一样的学号的。毕竟你的这程序是允许这样做的*/
d = 1;
N--;
}
}
//cout<<"已删除该生信息"<<endl;
}
if (d == 0)
cout << "没有学生信息" << endl;
else
cout << "已删除该生信息" << endl;
}
void student::change()
{
int a, b, d = 0;
cout << "请输入要修改的学生学号" << endl;
cin >> a;
for (b = 0; b <= M; b++)
{
if (num == a)
{
cout << "请输入新数据" << endl;
cout << "请修改姓名" << endl;
cin >> name;
cout << "请修改语文成绩" << endl;
cin >> chinese;
cout << "请修改数学成绩" << endl;
cin >> math;
cout << "请修改c语言成绩" << endl;
cin >> c;
d = 1;
}
//cout<<"信息已修改"<<endl;
}
if (d == 0)
cout << "没有学生信息" << endl;
else
cout << "信息已修改" << endl;
}
void student::search()
{
int a, b, d = 0;
cout << "请输入要查询的学生学号" << endl;
cin >> b;
for (a = 0; a <= M; a++)
if (num == b)
{
cout << "您要查询的学生信息" << endl;
cout << "学号\t" << num;
cout << "姓名\t" << name;
cout << "语文成绩\t" << chinese;
cout << "数学成绩\t" << math;
cout << "c语言成绩" << c << endl;
d = 1;
}
if (d == 0)
cout << "查无此人" << endl;
}
void student::arrange()
{
}
int main()
{
student stu;
char ch;
void menu();
menu();
cin >> ch;
while (ch != '0')
{
system("cls");
switch (ch)
{
case'1': stu.set(); break;
case'2': stu.add(); break;
case'3': stu.del(); break;
case'4': stu.change(); break;
case'5': stu.search(); break;
case'0':cout << "退出"; break;
default:cout << "错误" << endl; break;
}
menu();
cin >> ch;
}
return 0;
}//加上这行
void menu()
{
cout << "................................................." << endl;
cout << "学生管理系统" << endl;
cout << "1.输入学生信息" << endl;
cout << "2.添加学生信息" << endl;
cout << "3.删除学生信息" << endl;
cout << "4.修改学生信息" << endl;
cout << "5.查找学生信息" << endl;
cout << "6.学生成绩排名" << endl;
cout << "0.退出管理系统" << endl;
cout << ".................................................." << endl;
}
本帖最后由 风过无痕1989 于 2020-12-3 00:56 编辑
给你一个冒泡排序,你自己插入到合适的位置。1 至 12 行,你用的时候,用你自己的变量名称,同时将自定义的冒泡程序中的变量对应着就行了
#include<stdio.h>
int main()
{
void maopao(int a[]);
int a = { 0 };
int i;
for (i = 0;i < 10;i++) //用 for 循环给数组元素赋值
scanf("%d", &a);
maopao(a);
}
void maopao(int a[]) // 定义冒泡排序函数体
{
int g, j, k, i;
for (j = 0;j < 10;j++)
for (k = 0;k < 9;k++)
if (a < a) // 由小到大排序,若由大到小排序,改为小于号即可
{
g = a;
a = a;
a = g;
}
for (i = 0;i < 10;i++)
printf("%d ", a);
}
页:
[1]