成绩管理系统
1.增加学生信息
2.删除学生
3.修改学生
4.列出
5.查找
0.退出 把学生信息保存到文件
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
class Stu{
private:
int no;
string name;
int s[3];
public:
Stu(){}
Stu(int no):no(no){}
Stu(int no,string name,int s[]){
this->no = no;
this->name = name;
memcpy(this->s,s,sizeof(this->s));
}
bool operator==(const Stu& s)const{
return no == s.no;
}
friend ostream& operator<<(ostream& os,const Stu& s);
friend istream& operator>>(istream& is,Stu &s);
};
ostream& operator<<(ostream& os,const Stu& s){
os << s.no << " " << s.name;
for(int i=0;i<3;i++)
os << " " << s.s[i];
return os<<endl;
}
istream& operator>>(istream& is,Stu &s){
is >> s.no >> s.name;
for(int i=0;i<3;i++)
is >> s.s[i];
return is;
}
void load(vector<Stu>& vs){
ifstream ifs("stu.dat");
while(!ifs.eof()){
Stu s;
ifs >> s;
if(!ifs.eof()){
vs.push_back(s);
cout << s ;
}
}
ifs.close();
}
void save(vector<Stu>& vs){
ofstream ofs("stu.dat",ios::out|ios::trunc);
vector<Stu>::iterator it;
for(it=vs.begin();it!=vs.end();it++){
ofs << *it;
}
}
void add(vector<Stu>& vs){
Stu s;
cout << "no name s[3]:";
cin >> s;
vs.push_back(s);
}
void list(vector<Stu>& vs){
vector<Stu>::iterator it;
cout << "no name s" << endl;
for(it=vs.begin();it!=vs.end();++it){
cout << *it;
}
}
void find(vector<Stu>& vs){
int no;
cout << "input stu's no:";
cin >> no;
Stu s(no);
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
}else{
cout << "查无此人" << endl;
}
}
void del(vector<Stu>& vs){
int no;
cout << "input stu's no:";
cin >> no;
Stu s(no);
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
vs.erase(it);
}else{
cout << "查无此人" << endl;
}
}
void mod(vector<Stu>& vs){
Stu s;
cout << "input stu:";
cin >> s;
vector<Stu>::iterator it = find(vs.begin(),vs.end(),s);
if(it != vs.end()){
cout << *it;
*it = s;
cout << *it;
}else{
cout << "查无此人" << endl;
}
}
void run(){
vector<Stu> vs;
load(vs);
while(true){
cout << "成绩管理系统" << endl;
cout << "1.增加学生" << endl;
cout << "2.列出" << endl;
cout << "3.查找" << endl;
cout << "4.删除" << endl;
cout << "5.修改" << endl;
cout << "0.退出" << endl;
cout << "input:";
int opt = 0;
cin >> opt;
switch(opt){
case 1:
add(vs);
break;
case 2:
list(vs);
break;
case 3:
find(vs);
break;
case 4:
del(vs);
break;
case 5:
mod(vs);
break;
case 0:
save(vs);
return;
}
}
}
int main(){
run();
return 0;
}
|