数据描述:学号 姓名 班级 高数 英语 总分
程序完成功能:
(1) 浏览数据 (2) 增加数据 (3) 修改数据
(4) 查询数据(按姓名,按总分) (5) 退出
*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
public:
student(int , string ,int ,int ,int );
string getName(){return _name;};
int getID(){return _ID;};
void setID(int i){_ID=i;};
void setName(string);
void setClass(int c){_class=c;};
void setMath(int m){_math=m;_total=_math+_english;};
void setEnglish(int e){_english=e;_total=_math+_english;}
void print();
static int _stNum;
private:
int _ID;
string _name;
int _class;
int _math;
int _english;
int _total;
};
int student::_stNum=0;
student::student(int id, string name, int iclass, int ma, int en)
{
_ID=id;
_name=name;
_class=iclass;
_math=ma;
_english=en;
_total=_math+_english;
_stNum++;
};
void student::setName(string s)
{
_name=s;
};
void student::print()
{
cout<<_ID
<<"\t"<< _name
<<"\t"<<_class
<<"\t"<<_math
<<"\t"<<_english
<<"\t"<<_total <<endl;
}
void printMenu(void);
void printHead(void);
void printTail(int);
void main()
{
int tmp_ID;
string tmp_name;
int tmp_class;
int tmp_math;
int tmp_english;
int order=0;
vector<student> stvec;
printMenu();
cin>>order;
while(order!=5)
{
switch(order)
{
case 1:
if (stvec.size()==0)
{
cout<<"ERR:---->No data in the system!"<<endl<<endl;
printMenu();
cin>>order;
break;
}
else
{
vector<student>::iterator iter=stvec.begin();
vector<student>::iterator iter_end=stvec.end();
printHead();
for(;iter!=iter_end;iter++)
{
(*iter).print();
}
printTail(stvec.size());
printMenu();
cin>>order;
break;
}
case 2:
cout<<"Insert-->ID:";
cin>>tmp_ID;
cout<<"Insert-->Name:";
cin>>tmp_name;
cout<<"Insert-->Class:";
cin>>tmp_class;
cout<<"Insert-->Mark of Math:";
cin>>tmp_math;
cout<<"Insert-->Mark of English:";
cin>>tmp_english;
stvec.push_back(student(tmp_ID,tmp_name,tmp_class,tmp_math,tmp_english));
printMenu();
cin>>order;
break;
case 3:
{
string search_name;
bool isFind=false;
cout<<"\nOrignal_Update-->Name:";
cin>>search_name;
vector<student>::iterator iter=stvec.begin();
vector<student>::iterator iter_end=stvec.end();
for(;iter!=iter_end;iter++)
{
if( (*iter).getName()==search_name )
{
isFind=true;
cout<<"New_Update-->ID:";
cin>>tmp_ID;
cout<<"New_Update-->Name:";
cin>>tmp_name;
cout<<"New_Update-->Class:";
cin>>tmp_class;
cout<<"New_Update-->Mark of Math:";
cin>>tmp_math;
cout<<"New_Update-->Mark of English:";
cin>>tmp_english;
(*iter).setID(tmp_ID);
(*iter).setName(tmp_name);
(*iter).setClass(tmp_class);
(*iter).setMath(tmp_math);
(*iter).setEnglish(tmp_english);
}
}
if(!isFind)
{
cout<<"There no student named "<<search_name<<endl;
}
printMenu();
cin>>order;
break;
}
case 4:
string search_name;
int search_ID;
int name_ID=0;
bool isFind=false;
cout<<"-----------"<<endl;
cout<<"1.Name:"<<endl;
cout<<"2.ID:"<<endl;
cout<<"-----------"<<endl;
cin>>name_ID;
if (name_ID==1)
{
cout<<"\nSearch-->name:"<<endl;
cin>>search_name;
vector<student>::iterator iter=stvec.begin();
vector<student>::iterator iter_end=stvec.end();
for(;iter!=iter_end;iter++)
{
if( (*iter).getName()==search_name )
{
if(!isFind)
{
cout<<endl<<"Your search:"<<endl;
printHead();
}
(*iter).print();
isFind=true;
}
}
if(isFind){ cout<<"-------------------------------------------------"<<endl;}
if(!isFind)
{
cout<<"There no student named :"<<search_name<<endl;
}
printMenu();
cin>>order;
break;
}
if (name_ID==2)
{
cout<<"\nSearch-->ID:"<<endl;
cin>>search_ID;
vector<student>::iterator iter=stvec.begin();
vector<student>::iterator iter_end=stvec.end();
for(;iter!=iter_end;iter++)
{
if( (*iter).getID()==search_ID)
{
if(!isFind)
{cout<<endl<<"Your search:"<<endl; printHead();}
(*iter).print();
isFind=true;
}
}
if(isFind){ cout<<"-------------------------------------------------"<<endl;}
if(!isFind)
{
cout<<"There no student ID :"<<search_name<<endl;
}
printMenu();
cin>>order;
break;
}
}
}
}
void printMenu(void)
{
cout<<endl<<"*************Make**your**choice***************"<<endl;
cout<<"1. View the data"<<endl;
cout<<"2. ADD the data"<<endl;
cout<<"3. Update the data"<<endl;
cout<<"4. Search the data"<<endl;
cout<<"5. Quit"<<endl;
cout<<"********************************************"<<endl<<endl;
}
void printHead(void)
{
cout<<"-------------------------------------------------"<<endl;
cout<<"ID\tName\tClass\tMath\tEnglish\tTotal"<<endl;
}
void printTail(int i)
{
cout<<"There are total ";
if (i ==1)
{cout<< i<<" student"<<endl;}
else if(i >1)
{cout<<i<<" students"<<endl;}
cout<<"-------------------------------------------------"<<endl;
}
温馨提示:答案为网友推荐,仅供参考