(一):学生信息管理系统 一个cpp 文件。。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<windows.h>
using namespace std;
//基类 People
class People{
public:
People(int num , string na , string se , long sco ,string pho);
People(){}
~People(){}
virtual void Display(){}
protected:
int id;
string name;
string sex;
long score;
string phone;
};
People::People(int num , string na , string se , long sco ,string pho)
{
id = num;
score = sco;
sex = se;
name = na;
phone = pho;
}
void TestMain_MessageBox()
{
//调用API函数 MessageBox
int nSelect = ::MessageBox(NULL, "Hello,Good Luck\n ----Made by piaoyi", "Welcom", MB_OK);
return ;
}
bool TestOk_MessageBox()
{
//调用API函数 MessageBox
int nSelect = ::MessageBox(NULL, "Hello,此操作会改变信息,且不可恢复,是否执行?", "Hello!!!", MB_OKCANCEL);
if(nSelect == IDOK)
return true;
else
return false;
}
void ShowStudenInf()
{
cout<<" ***********学生的信息************\n\n";
cout<<" ======== 学号 姓名 性别 得分 电话 =======\n\n";
}
class Student:public People{
public:
Student(int num , string na , string se , long sco ,string pho):People(num , na , se , sco , pho){}
Student(){}
//Student(Student& t);
~Student(){}
Student *next;
friend ostream& operator<<(ostream& , const Student&);
friend istream& operator>>(istream& , Student&);
friend class Control;
};
//操作符<< >> 的重载
ostream& operator<<(ostream& out , const Student& t1)
{
out<<" ";
out<<setw(10)<<setiosflags(ios::left)<<setw(8)<<t1.id<<setw(8)<<t1.name<<setw(8)<<t1.sex<<setw(8)<<t1.score<<setw(8)<<t1.phone<<endl;
return out;
}
istream& operator>>(istream& in , Student& t1)
{
in>>t1.id>>t1.name>>t1.sex>>t1.score>>t1.phone;
return in;
}
class Control{
public:
Control()
{
Sp=NULL;
ReadFile();
}
~Control();
void StudentMenu();
void Build();
void Add();
void Find();
void Modify();
void Del();
void Display();
void Save();
void Sortt();
void ReadFile();
private:
Student *Sp;
};
void Control::Build()
{
if(TestOk_MessageBox())
{
Sp=NULL;
ShowStudenInf();
Student *s = new Student;
cin>>*s;
s->next = Sp;
Sp = s;
Save();
}
}
void Control::Add()
{
ShowStudenInf();
cout<<"请输入新建信息\n";
Student *s = new Student;
cin>>*s;
s->next = Sp;
Sp = s;
Save();
}
void Control::Del()
{
cout<<" ************** 删除方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
if(!TestOk_MessageBox())
return ;
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
Student *p2 = p1;
while(p1)
{
if(alt==1&&p1->id==Endid)
{
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
loop=1;
break;
}
p2 = p1;
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新删除\n\n";
Del();
}
else
{
if(p1 == Sp)
{
Sp = Sp->next;
delete p1;
}
else
{
p2->next = p1->next;
delete p1;
}
cout<<"删除成功";
Save();
}
system("pause\n");
}
void Control::Modify()
{
cout<<" ************** 修改方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
if(!TestOk_MessageBox())
return ;
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
while(p1)
{
if(alt==1&&p1->id==Endid)
{
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
loop=1;
break;
}
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新修改\n\n";
Modify();
}
else
{
ShowStudenInf();
cout<<"请输入新的信息";
cin>>*p1;
Save();
}
system("pause\n");
}
void Control::Display()
{
Student *p1 = Sp;
ShowStudenInf();
while(p1)
{
cout<<*p1;
p1 = p1->next;
}
system("pause\n");
}
void Control::Find()
{
cout<<" ************** 查找方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
ShowStudenInf();
while(p1)
{
if(alt==1&&p1->id==Endid)
{
cout<<*p1;
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
cout<<*p1;
loop=1;
break;
}
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新查找\n\n";
Find();
}
system("pause\n");
}
void Control::Sortt()
{
cout<<" ************** 排序方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 成绩 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
int alt;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
Student *p,*q;
Student *loop;
Student *Head = new Student;
int i,j,n=0;
for(loop = Sp ; loop ; loop = loop->next)
n++;
cout<<n<<endl;
Head->next = Sp;
for(i=1;i<n;i++)
{
loop = Head;
for(j=1;j<=n-i;j++)
{
p = loop->next;
q = p->next;
if(alt==1 && p->id > q->id)
{
loop->next=q;
p->next = q->next;
q->next = p;
}
else if(alt==2 && p->score > q->score)
{
loop->next=q;
p->next = q->next;
q->next = p;
}
loop = loop->next;
}
}
Sp=Head->next;
delete Head;
cout<<" **********排序成功!!!***********\n";
Save();
}
void Control::Save()
{
Student *p1 = Sp;
ofstream outfile("E:\\Student.txt",ios::out);
while(p1)
{
cout<<"正在保存!!\n";
outfile<<p1->id<<" ";
outfile<<p1->name<<" ";
outfile<<p1->sex<<" ";
outfile<<p1->score<<" ";
outfile<<p1->phone<<"\n";
p1 = p1->next;
}
cout<<"保存文件成功\n";
outfile.close();
system("pause\n");
}
Control::~Control()
{
Student *p1 = Sp;
while(p1)
{
Sp = Sp->next;
delete p1;
p1 = Sp;
}
}
void Control::ReadFile()
{
Sp = NULL;
ifstream infile("E:\\Student.txt",ios::in);
int id;
string name;
string sex;
long score;
string phone;
while(infile)
{
infile>>id>>name>>sex>>score>>phone;
if(!infile) continue;
Student *p = new Student(id,name,sex,score,phone);
p->next = Sp;
Sp = p;
cout<<" 正在载入 …………!!\n";
}
infile.close();
cout<<" 学生信息载入成功,欢迎使用!! \n";
system("pause\n");
}
void Control::StudentMenu() //菜单函数
{
system("cls");//清屏!!
cout<<" ★★★★★ C++课程设计 ★★★★★ \n";
cout<<" ※※※※※※※ 学生信息管理系统 ※※※※※※※※\n";
cout<<" ※ 1 : 新建 ※\n";
cout<<" ※ 2 : 添加 ※\n";
cout<<" ※ 3 : 查找 ※\n";
cout<<" ※ 4 : 修改 ※\n";
cout<<" ※ 5 : 删除 ※\n";
cout<<" ※ 6 : 显示 ※\n";
cout<<" ※ 7 : 保存 ※\n";
cout<<" ※ 8 : 排序 ※\n";
cout<<" ※ 9 : 退出系统 ※\n";
cout<<" ※※※※※※※※※※※※※※※※※※※※※※※※\n";
cout<<"请选择:1-- 9\n";
int alt;
cin>>alt;
switch(alt)
{
case 1: Build();StudentMenu();break;
case 2: Add();StudentMenu();break;
case 3: Find();StudentMenu();break;
case 4: Modify();StudentMenu();break;
case 5: Del();StudentMenu();break;
case 6: Display();StudentMenu();break;
case 7: Save();StudentMenu();break;
case 8: Sortt();StudentMenu();break;
case 9: exit(0);
default: exit(0);
}
}
int main()
{
system("color 5E");
TestMain_MessageBox();
Control a;
a.StudentMenu();
}
(二)学生成绩管理系统 之多文件
Ps:这个多了一个学号验证,其余和上面几乎一样。。
1.1:People类的声明部分:
//文件名 People.h
#ifndef PEOPLE_H
#define PEOPLE_H
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<windows.h>
using namespace std;
//基类 People
class People{
public:
People(int num , string na , string se , long sco ,string pho);
People(){}
~People(){}
protected:
int id;
string name;
string sex;
long score;
string phone;
};
void TestMain_MessageBox()
{
//调用API函数 MessageBox
int nSelect = ::MessageBox(NULL, "Hello,Good Luck\n ----Made by piaoyi", "Welcom!!!", MB_OK);
return ;
}
bool TestOk_MessageBox()
{
//调用API函数 MessageBox
int nSelect = ::MessageBox(NULL, "Hello,此操作会改变信息,且不可恢复,是否执行?", "Hello!!!", MB_OKCANCEL);
if(nSelect == IDOK)
return true;
else
return false;
}
void ShowStudenInf()
{
cout<<" ***********学生的信息************\n\n";
cout<<" ======== 学号 姓名 性别 得分 电话 =======\n\n";
}
#endif // PEOPLE_H_INCLUDED
//文件名 People.cpp
#include"People.h"
People::People(int num , string na , string se , long sco ,string pho)
{
id = num;
score = sco;
sex = se;
name = na;
phone = pho;
}
2.1 Student 类的声明部分
//文件名 Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include"People.h"
class Student:public People{
public:
Student(int num , string na , string se , long sco ,string pho):People(num , na , se , sco , pho){}
Student(){}
~Student(){}
Student *next;
friend ostream& operator<<(ostream& , const Student&);
friend istream& operator>>(istream& , Student&);
friend class Control;
};
#endif // STUDENT_H_INCLUDED
2.2 Student 类的实现部分
//文件名 Student.cpp
#include"Student.h"
ostream& operator<<(ostream& out , const Student& t1)
{
out<<" ";
out<<setw(10)<<setiosflags(ios::left)<<setw(8)<<t1.id<<setw(8)<<t1.name<<setw(8)<<t1.sex<<setw(8)<<t1.score<<setw(8)<<t1.phone<<endl;
return out;
}
istream& operator>>(istream& in , Student& t1)
{
in>>t1.id>>t1.name>>t1.sex>>t1.score>>t1.phone;
return in;
}
3.1 Control 类的声明部分
//文件名 Control.h
#ifndef CONTROL_H_INCLUDED
#define CONTROL_H_INCLUDED
#include"Student.h"
#include"Student.cpp"
#include"People.h"
#include"People.cpp"
class Control{
public:
Control()
{
Sp=NULL;
ReadFile();
}
~Control();
void StudentMenu();
void Build();
void Add();
void Find();
bool FindConid(int Id);
bool FindConname(string name);
void Modify();
void Del();
void Display();
void Save();
void Sortt();
void ReadFile();
private:
Student *Sp;
};
#endif // CONTROL_H_INCLUDED
3.2 Control 类的实现部分
//文件名 Control.cpp
#include"Control.h"
void Control::Build()
{
if(TestOk_MessageBox())
{
Sp=NULL;
ShowStudenInf();
Student *s = new Student;
cin>>*s;
s->next = Sp;
Sp = s;
Save();
}
}
void Control::Add()
{
ShowStudenInf();
cout<<"请输入新建信息\n";
Student *s = new Student;
cin>>*s;
if(FindConid(s->id))
{
cout<<"学好已存在,重新添加!!!\n";
Add();
}
else
{
s->next = Sp;
Sp = s;
Save();
}
}
void Control::Del()
{
cout<<" ************** 删除方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
if(!TestOk_MessageBox())
return ;
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
Student *p2 = p1;
while(p1)
{
if(alt==1&&p1->id==Endid)
{
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
loop=1;
break;
}
p2 = p1;
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新删除\n\n";
Del();
}
else
{
if(p1 == Sp)
{
Sp = Sp->next;
delete p1;
}
else
{
p2->next = p1->next;
delete p1;
}
cout<<"删除成功";
Save();
}
system("pause\n");
}
void Control::Modify()
{
cout<<" ************** 修改方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
if(!TestOk_MessageBox())
return ;
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
while(p1)
{
if(alt==1&&p1->id==Endid)
{
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
loop=1;
break;
}
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新修改\n\n";
Modify();
}
else
{
ShowStudenInf();
cout<<"请输入新的信息";
cin>>*p1;
Save();
}
system("pause\n");
}
void Control::Display()
{
Student *p1 = Sp;
ShowStudenInf();
while(p1)
{
cout<<*p1;
p1 = p1->next;
}
system("pause\n");
}
void Control::Find()
{
cout<<" ************** 查找方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 姓名 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
int alt,Endid,loop=0;
string Endname;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
if(alt==1)
{
cout<<"请输入学号!\n";
cin>>Endid;
}
if(alt==2)
{
cout<<"请输入姓名!\n";
cin>>Endname;
}
Student *p1 = Sp;
ShowStudenInf();
while(p1)
{
if(alt==1&&p1->id==Endid)
{
cout<<*p1;
loop=1;
break;
}
if(alt==2&&p1->name==Endname)
{
cout<<*p1;
loop=1;
break;
}
p1 = p1->next;
}
if(loop==0)
{
cout<<"不存在!请重新查找\n\n";
Find();
}
system("pause\n");
}
bool Control::FindConid(int Id )
{
int loop=0;
Student *p1 = Sp;
while(p1)
{
if(p1->id==Id)
{
cout<<*p1;
loop=1;
break;
}
p1 = p1->next;
}
if(loop)
return true;
else
return false;
}
bool Control::FindConname(string name )
{
int loop=0;
Student *p1 = Sp;
while(p1)
{
if(p1->name == name)
{
cout<<*p1;
loop=1;
break;
}
p1 = p1->next;
}
if(loop)
return true;
else
return false;
}
void Control::Sortt()
{
cout<<" ************** 排序方式 **********\n";
cout<<" * 1 : 学号 *\n";
cout<<" * 2 : 成绩 *\n";
cout<<" * 3 : 退出 *\n";
cout<<" **********************************\n";
cout<<"请选择!!1--3\n";
int alt;
cin>>alt;
while(alt<1||alt>3)
{
cout<<"输入错误,请重新输入!!\n";
cin>>alt;
}
if(alt==3) return ;
Student *p,*q;
Student *loop;
Student *Head = new Student;
int i,j,n=0;
for(loop = Sp ; loop ; loop = loop->next)
n++;
cout<<n<<endl;
Head->next = Sp;
for(i=1;i<n;i++)
{
loop = Head;
for(j=1;j<=n-i;j++)
{
p = loop->next;
q = p->next;
if(alt==1 && p->id > q->id)
{
loop->next=q;
p->next = q->next;
q->next = p;
}
else if(alt==2 && p->score > q->score)
{
loop->next=q;
p->next = q->next;
q->next = p;
}
loop = loop->next;
}
}
Sp=Head->next;
delete Head;
cout<<" **********排序成功!!!***********\n";
Save();
}
void Control::Save()
{
Student *p1 = Sp;
ofstream outfile("D:\\Student.txt",ios::out);
while(p1)
{
cout<<"正在保存!!\n";
outfile<<p1->id<<" ";
outfile<<p1->name<<" ";
outfile<<p1->sex<<" ";
outfile<<p1->score<<" ";
outfile<<p1->phone<<"\n";
p1 = p1->next;
}
cout<<"保存文件成功\n";
outfile.close();
system("pause\n");
}
Control::~Control()
{
Student *p1 = Sp;
while(p1)
{
Sp = Sp->next;
delete p1;
p1 = Sp;
}
}
void Control::ReadFile()
{
Sp = NULL;
ifstream infile("D:\\Student.txt",ios::in);
int id;
string name;
string sex;
long score;
string phone;
while(infile)
{
infile>>id>>name>>sex>>score>>phone;
if(!infile) continue;
Student *p = new Student(id,name,sex,score,phone);
p->next = Sp;
Sp = p;
cout<<" 正在载入 …………!!\n";
}
infile.close();
cout<<" 学生信息载入成功,欢迎使用!! \n";
system("pause\n");
}
void Control::StudentMenu() //菜单函数
{
system("cls");//清屏!!
cout<<" ★★★★★ C++课程设计 ★★★★★ \n";
cout<<" ※※※※※※※ 学生信息管理系统 ※※※※※※※※\n";
cout<<" ※ 1 : 新建 ※\n";
cout<<" ※ 2 : 添加 ※\n";
cout<<" ※ 3 : 查找 ※\n";
cout<<" ※ 4 : 修改 ※\n";
cout<<" ※ 5 : 删除 ※\n";
cout<<" ※ 6 : 显示 ※\n";
cout<<" ※ 7 : 保存 ※\n";
cout<<" ※ 8 : 排序 ※\n";
cout<<" ※ 9 : 退出系统 ※\n";
cout<<" ※※※※※※※※※※※※※※※※※※※※※※※※\n";
cout<<"请选择:1--8\n";
int alt;
cin>>alt;
switch(alt)
{
case 1: Build();StudentMenu();break;
case 2: Add();StudentMenu();break;
case 3: Find();StudentMenu();break;
case 4: Modify();StudentMenu();break;
case 5: Del();StudentMenu();break;
case 6: Display();StudentMenu();break;
case 7: Save();StudentMenu();break;
case 8: Sortt();StudentMenu();break;
case 9: exit(0);
default: exit(0);
}
}
4 :主函数main
//文件名 main.cpp
#include"Control.h"
#include"Control.cpp"
bool Key()
{
string key;
cout<<" Input Password ";
cin>>key;
if(key == "admin")
return true;
else
return false;
}
int main()
{
while(!Key())
cout<<"Error! Input,Again..\n\n";
system("color 5E");
TestMain_MessageBox();
Control a;
a.StudentMenu();
}