- //
- // main.cpp
- // gezhongpaixu_c++
- //
- // Created by duanqibo on 2019/7/12.
- // Copyright © 2019 年 duanqibo. All rights reserved.
- // 快速排序, 直接选择排序, 直接插入排序, 冒泡排序
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- class Grade
- {
- private:
- int ID;
- char Name[20];
- int Math;
- int Chinese;
- int Computer;
- int Total;
- public:
- Grade();
- void setGrade(int id,char *name,int math,int chinese,int computer)
- {
- ID=id;
- strcpy(Name,name);
- Math=math;
- Chinese=chinese;
- Computer=computer;
- Total=Math+Chinese+Computer;
- }
- int getID()
- {
- return ID;
- }
- string getName()
- {
- return Name;
- }
- int getMath()
- {
- return Math;
- }
- int getChinese()
- {
- return Chinese;
- }
- int getComputer()
- {
- return Computer;
- }
- int getTotal()
- {
- return Total;
- }
- void show();
- };
- // 初始化私有变量
- Grade::Grade()
- {
- ID=0;
- strcpy(Name,"学生姓名");
- Math=0;
- Chinese=0;
- Computer=0;
- Total=0;
- }
- void Grade::show()
- {
- cout<<ID<<'\t';
- cout<<Name<<'\t';
- cout<<Math<<'\t';
- cout<<Chinese<<'\t';
- cout<<Computer<<'\t';
- cout<<Total<<'\t';
- cout<<endl;
- }
- // 按语文成绩 --- 快速排序
- int quickpartition(Grade r[],int low,int high)
- {
- // 对顺序表 r 中的语文序列 r[low] 到 r[high] 进行快速排序
- Grade w;
- w=r[low];
- while(low<high)
- {
- //low 和 high 没有碰面则反复运行
- while((r[high].getChinese()>r[0].getChinese()) && (low<high))
- {
- high--;
- }
- r[low]=r[high];
- while((r[low].getChinese()<=r[0].getChinese()) && (low<high))
- {
- low++;
- }
- r[high]=r[low];
- }
- r[low]=w;
- return low;
- }
- // 按语文成绩快速排序
- void chinese_sort(Grade stu[],int low,int high)
- {
- if(low<high)
- {
- int temp=quickpartition(stu,low,high);
- chinese_sort(stu,low,temp-1);
- chinese_sort(stu,temp+1,high);
- }
- }
- // 按计算机成绩 -- 直接插入排序
- void computer_sort(Grade stu[],int n)
- {
- cout<<"按计算机成绩从小到大排序"<<endl;
- int i,j;
- Grade temp;
- for(i=1;i<=n;i++)
- {
- temp=stu[i];
- j=i-1;
- while(temp.getComputer()<stu[j].getComputer())
- {
- stu[j+1]=stu[j];
- j--;
- }
- stu[j+1]=temp;
- }
- }
- // 按总分 -- 冒泡排序
- void total_sort(Grade stu[],int n)
- {
- cout<<"按总分由小到大排序:"<<endl;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n-i-1;j++)
- {
- if(stu[j].getTotal()<stu[j+1].getTotal())
- {
- Grade temp;
- temp=stu[j];
- stu[j]=stu[j+1];
- stu[j+1]=temp;
- }
- }
- }
- }
- // 按数学成绩 -- 直接选择排序
- void math_sort(Grade stu[],int n)
- {
- cout<<"按数学成绩由小到大排序:"<<endl;
- int i=0,j=0,min=0;
- Grade tmp;
- for(i=0;i<n-1;i++)
- {
- min=i;
- for(j=i;j<n;j++)
- {
- if(stu[min].getMath()>stu[j].getMath())
- {
- min=j;
- }
- }
- if(min!=i)
- {
- tmp=stu[min];
- stu[min]=stu[i];
- stu[i]=tmp;
- }
- }
- }
- void menu()
- {
- cout<<"**************************"<<endl;
- cout<<"1. 请输入 5 个学生"<<endl;
- cout<<"2. 按总分排序"<<endl;
- cout<<"3. 按数学成绩排序"<<endl;
- cout<<"4. 按语文成绩排序"<<endl;
- cout<<"5. 按计算机成绩排序"<<endl;
- cout<<"7. 将结果输出至文本文件"<<endl;
- cout<<"0. 退出系统"<<endl;
- cout<<"**************************"<<endl;
- }
- void _exit()
- {
- cout<<"**************************"<<endl;
- cout<<"欢迎使用本系统"<<endl;
- cout<<endl;
- cout<<"再见!"<<endl;
- cout<<"**************************"<<endl;
- }
- void table()
- {
- cout<<"显示学生情况:"<<endl;
- cout<<"学号"<<'\t';
- cout<<"姓名"<<'\t';
- cout<<"数学"<<'\t';
- cout<<"语文"<<'\t';
- cout<<"计算机"<<'\t';
- cout<<"总分"<<'\t';
- cout<<endl;
- }
- // 主函数
- int main()
- {
- const int N=5;
- int i,id,num;
- char name[20];
- int math,chinese,computer;
- Grade student[N];
- ofstream ff;
- ff.open("c:\\stu_file.txt",iOS::out);
- menu();
- while(1)
- {
- cout<<"请选择:";
- cin>>num;
- switch(num)
- {
- case 1:
- cout<<"请输入 5 个学生的情况:"<<endl;
- for(i=0;i<5;i++)
- {
- cin>>id>>name>>math>>chinese>>computer;
- student[i].setGrade(id,name,math,chinese,computer);
- }
- table();
- for(i=0;i<N;i++)
- student[i].show();
- break;
- case 2:
- total_sort(student,N);
- table();
- for(i=0;i<N;i++)
- student[i].show();
- break;
- case 3:
- math_sort(student,N);
- table();
- for(i=0;i<N;i++)
- student[i].show();
- break;
- case 4:
- cout<<"按语文成绩排序"<<endl;
- chinese_sort(student,0,N-1);
- table();
- for(i=0;i<N;i++)
- student[i].show();
- break;
- case 5:
- computer_sort(student,N);
- table();
- for(i=0;i<N;i++)
- student[i].show();
- break;
- case 6:
- cout<<"单科成绩均大于 85 的学生的成绩:"<<endl;
- cout<<"学号"<<'\t';
- cout<<"姓名"<<'\t';
- cout<<"数学"<<'\t';
- cout<<"语文"<<'\t';
- cout<<"计算机"<<'\t';
- cout<<"总分"<<'\t';
- cout<<endl;
- for(i=0;i<N;i++)
- {
- if(student[i].getMath()>85 && student[i].getChinese()>85 && student[i].getComputer()>85)
- student[i].show();
- }
- break;
- case 7:
- cout<<"文件已输出至 c:\\stu_file.txt"<<endl;
- ff<<"学号"<<'\t'
- <<"姓名"<<'\t'
- <<"数学"<<'\t'
- <<"语文"<<'\t'
- <<"计算机"<<'\t'
- <<"总分"<<endl;
- for(i=0;i<N;i++)
- {
- ff<<student[i].getID()<<'\t'
- <<student[i].getName()<<'\t'
- <<student[i].getMath()<<'\t'
- <<student[i].getChinese()<<'\t'
- <<student[i].getComputer()<<'\t'
- <<student[i].getTotal()<<endl;
- }
- break;
- case 0:
- system("cls");
- _exit();
- exit(1);
- break;
- default:
- cout<<"没有此选项!";
- break;
- }
- }
- return 1;
- }
运行结果:
各种排序
来源: http://www.bubuko.com/infodetail-3121656.html