C++程序搜集

#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;

class student{
private:
string No;
string Name;
double Math;
double Eng;
double Chn;
double Cpro;
double Sum;
public:
student(string no, string name, double math, double eng,
double chn, double cpro){
No = no;
Name = name;
Math = math;
Eng = eng;
Chn = chn;
Cpro = cpro;
Sum = math + eng + chn + cpro;
}

friend ostream& operator <<(ostream& out, student& S)
{
out << "\t" << S.No << "\t" << S.Name << "\t" << S.Math << "\t"
<< S.Eng << "\t" << S.Chn << "\t" << S.Cpro << "\t" << S.Sum;
return out;
}
const string getno()
{
return No;
}
const string getname()
{
return Name;
}
const double getsum()
{
return Sum;
}
~student(){

}
};

void main_remid(); /*输出主要提示信息 */
void score_remind(); /*输出成绩提示信息*/
int add(list<student> &lst); /*增加学生*/
int find(list<student> &lst); /*查询学生信息*/
int del(list<student> &lst); /*删除学生*/
void sort_sum(list<student> &lst); /*按总成绩降序打印学生成绩*/
void sort_no(list<student> &lst); /*按学号升序打印学生成绩*/
bool cmp_no(student& st1, student& st2); /*用于sort的比较函数*/

int main()
{
list<student> lst; /*使用list容器存储学生信息*/
char op = ' ';
main_remid();
while(op != 'q')
{
cin >> op;
switch(op)
{
case '1':
sort_no(lst);
break;
case '2':
add(lst);
break;
case '3':
find(lst);
break;
case '4':
del(lst);
break;
case '5':
sort_sum(lst);
break;
case '6':
main_remid();
break;
default:
cout << "输入指令未知,请重新输入" << endl;
break;
}
if(op != 'q')
cout << " 请继续选择您想要的操作:" << endl;
}
return 0;
}

/*增加学生*/
int add(list<student> &lst)
{
string No;
string Name;
double Math;
double Eng;
double Chn;
double Cpro;
cout << " 请输入要增加学生的学号:" << endl;
cin >> No;
for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
if(No == it->getno()){
cout << "添加失败,此学号已存在,请重新操作" << endl;
return 0;
}
cout << " 请输入要增加学生的姓名" << endl;
cin >> Name;
cout << " 请输入要增加学生的数学成绩:" << endl;
cin >> Math;
cout << " 请输入要增加学生的英语成绩:" << endl;
cin >> Eng;
cout << " 请输入要增加学生的语文成绩:" << endl;
cin >> Chn;
cout << " 请输入要增加学生的C语言成绩:" << endl;
cin >> Cpro;
student *st = new student(No, Name, Math, Eng, Chn, Cpro);
lst.push_back(*st);
}

/*查询学生信息*/
int find(list<student> &lst)
{
cout << "请输入要查询学生的学号:" << endl;
string no;
cin >> no;
for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
{
if(no == it->getno())
{
score_remind();
cout << *it << endl;
return 0;
}
}
cout << "不存在此学号,请重新选择操作" << endl;
}

/*删除学生*/
int del(list<student> &lst)
{
cout << " 请输入要删除学生的学号:" << endl;
string no;
string name;
cin >> no;
for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
if(no == it->getno())
{
no = it->getno();
name = it->getname();
lst.erase(it);
cout << "学生" << no << " " << name << "删除成功" << endl;
return 0;
}
cout << " 删除失败,不存在此学号" << endl;
}

/*按学号升序打印学生成绩*/
void sort_no(list<student> &lst)
{
list<student> temp;
temp.push_front(*lst.begin());
for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
{
list<student>::iterator jt = temp.begin();
while(jt!=temp.end() && strcmp(it->getno().c_str(), jt->getno().c_str()))
jt++;
temp.insert(jt, *it);
}
score_remind();
for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
{
cout << *it << endl;
}
}

/*用于sort的比较函数*/
/*bool cmp_no(const student& st1, const student& st2)
{
return st1.getno() < st2.getno();
}*/

/*按成绩升序打印学生成绩*/
void sort_sum(list<student> &lst)
{
list<student> temp;
temp.push_front(*lst.begin());
for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
{
list<student>::iterator jt = temp.begin();
while(jt!=temp.end() && it->getsum()<jt->getsum())
jt++;
temp.insert(jt, *it);
}
score_remind();
for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
{
cout << *it << endl;
}
}

/*输出主要提示信息 */
void main_remid()
{
cout << "\t\t\t学生成绩类" << endl << endl;
cout << "\t\t1.查询所有学生的成绩信息" << endl;
cout << "\t\t2.增加学生" << endl;
cout << "\t\t3.查找学生" << endl;
cout << "\t\t4.删除学生" << endl;
cout << "\t\t5.查看总分排名" << endl;
cout << "\t\t6.查看提示" << endl;
cout << "\t\tq.退出系统" << endl << endl;
}

/*输出成绩提示信息*/
void score_remind()
{
cout << "\t\t\t 学生成绩信息" << endl << endl;
cout << "\t学号\t" << "姓名\t" << "数学\t" << "英语\t"
<< "语文\t" << "C语言\t" << "总成绩" << endl;
}

 

转载自http://www.cnblogs.com/forerve/p/3908716.html

posted @ 2015-03-18 20:21  专注的西伯利亚狼  阅读(85)  评论(0)    收藏  举报