![]() |
#2
林月儿2015-05-08 19:03
#include <iostream>
#include <fstream> #include<cstring> #include<conio.h> #include <ctime> #include<cstdlib> using namespace std; struct Class { int Chinese; int Math; }; class Student{ public: Student(); void Ofile(ofstream &of); void Infile(ifstream &f); void Out(); void Set(char *name,int no,Class score); char *GetName(); int GetNo(); Class getScore(){ return Score; } Student *Next; protected: char Name[20]; int No; Class Score ; }; Student::Student():Next(0){} char *Student::GetName(){return Name;} int Student::GetNo(){return No;} void Student::Set(char *name,int no,Class score) { strcpy(Name,name); No=no; Score=score; } void Student::Infile(ifstream &f) { f>>Name>>No>>Score.Chinese>>Score.Math; //将数据输入到文件 } void Student::Ofile(ofstream &of) { of<<" "<<Name<<" "<<No<<" "<<Score.Chinese<<" "<<Score.Math; //从文件中提取数据 } void Student::Out() { cout<<Name<<"\t"<<No<<"\t"<<Score.Chinese<<"\t\t"<<Score.Math<<"\t"<<endl; } class Function //功能类 {public: Function(); //构造函数 ~Function(); //析构函数 void Menu(); //菜单函数 void Add(); //录入学生成绩函数 void Stat(); //统计平均成绩 void Search(); //查询学生成绩函数 void Delete(); //删除学生成绩函数 void Modify(); //修改学生成绩函数 void Show(); //显示学生成绩函数 private: Student *Student_First; void Read(); //读取学生成绩函数 void Save(); //保存学生成绩信息函数 }; Function::Function() { Student_First=new Student; Read(); } Function::~Function() { delete Student_First; } void Function::Add() //录入学生成绩信息函数 { char name[20]; int no; Class score; char choose; Student *f1,*p,*f2; system("cls"); f1=Student_First; f2=Student_First->Next; while(f1->Next) f1=f1->Next; do { p=new Student; cout<<"请输入您要添加的学生成绩信息:"<<endl; cout<<"请输入学生姓名:"; cin>>name; while(f2) { if(strcmp(f2->GetName(),name)==0) { cout<<"该学生已存在,请确定姓名!\n\n"; cout<<"请输入姓名:"; cin>>name; break; } f2=f2->Next; } cout<<"请输入学号:"; cin>>no; cout<<"请输入语文成绩:"; cin>>score.Chinese; cout<<"请输入数学成绩:"; cin>>score.Math; p->Set(name,no,score); f1->Next=p; p->Next=NULL; f1=f1->Next; cout<<"是否继续输入信息?(Y\\N) "<<endl; cin>>choose; }while(choose=='y'||choose=='Y'); Save(); cout<<"1.返回主菜单"<<endl; cin>>choose; while(choose!='1') { cout<<"1.返回主菜单"<<endl; cin>>choose; } Menu(); } void Function::Stat() //求平均成绩信息函数 { int flag(0); char choose; char t1[20]; int t2; system("cls"); Student *temp=Student_First->Next; do { cout<<"输入查询方式:\n1.按姓名查询\n2.按学号查询\n"; cin>>choose; if(choose=='1') { cout<<"请输入您要查询的姓名:"; cin>>t1; while(temp) { if(strcmp(t1,temp->GetName())==0) { flag=1; break; } temp=temp->Next; } if(flag==0) cout<<"\n无该学生的信息\n"<<endl; else { cout<<temp->GetName()<<"的平均成绩:"<<((temp->getScore()).Chinese+(temp->getScore()).Math)/2<<"\n"; } break; } else if(choose=='2') { cout<<"请输入您要查询的学号"; cin>>t2; while(temp) { if(t2==temp->GetNo()) { flag=1; break; } temp=temp->Next; } if(flag==0) cout<<"\n无该学生的信息\n"<<endl; else { cout<<temp->GetName()<<"的平均成绩:"<<((temp->getScore()).Chinese+(temp->getScore()).Math)/2<<"\n"; } break; } }while(choose!='1'||choose!='2'); cout<<"\n1.返回主菜单\n2.继续查询"<<endl; cin>>choose; while(choose!='1'&&choose!='2') { cout<<"1.返回主菜单\n2.继续查询"<<endl; cin>>choose; } if(choose=='1') Menu(); else if(choose=='2') Search(); } void Function::Delete() //删除信息函数 { char name[20]; int no; char choose; Student *temp,*p; system("cls"); p=temp=Student_First->Next; cout<<"请输入姓名:"; cin>>name; cout<<"输入学号:"; cin>>no; while(temp) { if(strcmp(temp->GetName(),name)==0&&temp->GetNo()==no) //判断该学生信息是否存在 { cout<<"姓名\t学号\t语文成绩\t数学成绩\n"; temp->Out(); cout<<"\n是否删除(Y/N)"; cin>>choose; if(choose=='y'||choose=='Y') { p->Next=temp->Next; delete temp; cout<<"删除成功:\n"; } break; } p=temp; temp=temp->Next; } Save(); cout<<"1.返回主菜单\n2.继续删除"<<endl; cin>>choose; while(choose!='1'&&choose!='2') { cout<<"1.返回主菜单\n2.继续删除"<<endl; cin>>choose; } if(choose=='1') Menu(); else if(choose=='2') Delete(); } void Function::Modify() //修改学生信息函数 { char choose,name[20]; Student *temp,*p; int no; Class score; system("cls"); temp=p=Student_First; cout<<"请输入您要修改的学生姓名:"; cin>>name; while(temp) { if(strcmp(temp->GetName(),name)==0) { cout<<"姓名\t学号\t语文成绩\t数学成绩\n"; temp->Out(); cout<<"请输入姓名:"; cin>>name; cout<<"请输入学号:"; cin>>no; cout<<"请输入语文成绩:"; cin>>score.Chinese; cout<<"请输入数学成绩:"; cin>>score.Math; temp->Set(name,no,score); break; } temp=temp->Next; } Save(); cout<<"修改成功!"<<endl; cout<<"1.返回主菜单\n2.继续修改"<<endl; cin>>choose; while(choose!='1'&&choose!='2') { cout<<"1.返回主菜单\n2.继续修改"<<endl; cin>>choose; } if(choose=='1') Menu(); else if(choose=='2') Modify(); } void Function::Read() //读取信息函数 { Student *p,*p2; p=Student_First; long t; ifstream is("Student.txt",ios::in); if(!is) { ofstream os("Student.txt",ios::out); os.close(); return ; } while(!is.eof()) { p2=new Student; p2->Infile(is); p->Next=p2; p2->Next=NULL; p=p->Next; } } void Function::Save() //保存学生成绩信息函数 { ofstream of("Student.txt",ios::out); Student *p=Student_First->Next; while(p) { p->Ofile(of); p=p->Next; } of.close(); } void Function::Search() { int flag(0); char choose; char t1[20]; int t2; system("cls"); Student *temp=Student_First->Next; do { cout<<"输入查询方式:\n1.按姓名查询\n2.按学号查询\n"; cin>>choose; if(choose=='1') { cout<<"请输入您要查询的姓名:"; cin>>t1; while(temp) { if(strcmp(t1,temp->GetName())==0) { flag=1; break; } temp=temp->Next; } if(flag==0) cout<<"\n无该学生的信息\n"<<endl; else { cout<<"姓名\t学号\t语文成绩\t数学成绩\n"; temp->Out(); } break; } else if(choose=='2') { cout<<"请输入您要查询的学号"; cin>>t2; while(temp) { if(t2==temp->GetNo()) { flag=1; break; } temp=temp->Next; } if(flag==0) cout<<"\n无该学生的信息\n"<<endl; else { cout<<"姓名\t学号\t语文成绩\t数学成绩\n"; temp->Out(); } break; } }while(choose!='1'||choose!='2'); cout<<"\n1.返回主菜单\n2.继续查询"<<endl; cin>>choose; while(choose!='1'&&choose!='2') { cout<<"1.返回主菜单\n2.继续查询"<<endl; cin>>choose; } if(choose=='1') Menu(); else if(choose=='2') Search(); } void Function::Show() { char choose; Student *temp; system("cls"); temp=Student_First->Next; if(!temp) { cout<<"文件无数据\n\n "<<endl; cout<<"1.返回主菜单"<<endl; cin>>choose; while(choose!='1') { cout<<"1.返回主菜单"<<endl; cin>>choose; } Menu(); } else { cout<<"姓名\t学号\t语文成绩\t数学成绩\n"; while(temp!=NULL) { temp->Out(); temp=temp->Next; } } cout<<"1.返回主菜单"<<endl; cin>>choose; while(choose!='1') { cout<<"1.返回主菜单"<<endl; cin>>choose; } Menu(); } void Function::Menu() { time_t t; time(&t); char choose; system("cls"); cout<<" ------------------------版权所有:张嘉铎-------------------------"<<endl; cout<<endl; cout<<"***********************************************************"<<endl; cout<<" 武汉工程大学 "<<endl<<endl; cout<<" 学生成绩信息管理系统 "<<endl<<endl; cout<<" 显示系统时间和日期: "<<ctime(&t)<<endl; cout<<"*********************************************************"<<endl<<endl; cout<<"请选择您需要的操作,选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl; cout<<"\n"; cout<<" 1 录入学生成绩信息:\n"<<endl; cout<<" 2 查询学生成绩信息:\n"<<endl; cout<<" 3 删除学生成绩信息:\n"<<endl; cout<<" 4 修改学生成绩信息:\n"<<endl; cout<<" 5 显示全部学生成绩信息:\n"<<endl; cout<<" 6 查询学生平均成绩信息\n"<<endl; cout<<" 7 退出系统"<<endl; cout<<"\n"; cin>>choose; switch(choose) { case '1': Add();break; case '2': Search();break; case '3': Delete();break; case '4': Modify();break; case '5': Show();break; case '6': Stat();break; case '7': exit(1);break; default: { cout<<"请按规定输入选择项!"<<endl; Menu(); } } } int main() { Function function; //定义功能接口 function.Menu(); //调用主菜单 } |
#include <iostream>
#include <fstream>
#include<cstring>
#include<conio.h>
#include <ctime>
using namespace std;
struct Class
{ int Chinese;
int Math;
};
class Student{
public:
Student();
void Ofile(ofstream &of);
void Infile(ifstream &f);
void Out();
void Set(char *name,int no,Class score);
char *GetName();
int GetNo();
Student *Next;
protected:
char Name[20];
int No;
Class Score ;
};
Student::Student():Next(0){}
char *Student::GetName(){return Name;}
int Student::GetNo(){return No;}
void Student::Set(char *name,int no,Class score)
{ strcpy(Name,name);
No=no;
Score=score;
}
void Student::Infile(ifstream &f)
{ f>>Name>>No>>Score.Chinese>>Score.Math; //将数据输入到文件
}
void Student::Ofile(ofstream &of)
{ of<<" "<<Name<<" "<<No<<" "<<Score.Chinese<<" "<<Score.Math; //从文件中提取数据
}
void Student::Out()
{ cout<<Name<<"\t"<<No<<"\t"<<Score.Chinese<<"\t\t"<<Score.Math<<"\t"<<endl;
}
class Function //功能类
{public:
Function(); //构造函数
~Function(); //析构函数
void Menu(); //菜单函数
void Add(); //录入学生成绩函数
void Search(); //查询学生成绩函数
void Delete(); //删除学生成绩函数
void Modify(); //修改学生成绩函数
void Show(); //显示学生成绩函数
private:
Student *Student_First;
void Read(); //读取学生成绩函数
void Save(); //保存学生成绩信息函数
};
Function::Function()
{ Student_First=new Student;
Read();
}
Function::~Function()
{ delete Student_First;
}
void Function::Add() //录入学生成绩信息函数
{ char name[20];
int no;
Class score;
char choose;
Student *f1,*p,*f2;
system("cls");
f1=Student_First;
f2=Student_First->Next;
while(f1->Next)
f1=f1->Next;
do
{ p=new Student;
cout<<"请输入您要添加的学生成绩信息:"<<endl;
cout<<"请输入学生姓名:";
cin>>name;
while(f2)
{ if(strcmp(f2->GetName(),name)==0)
{ cout<<"该学生已存在,请确定姓名!\n\n";
cout<<"请输入姓名:";
cin>>name;
break;
}
f2=f2->Next;
}
cout<<"请输入学号:";
cin>>no;
cout<<"请输入语文成绩:";
cin>>score.Chinese;
cout<<"请输入数学成绩:";
cin>>score.Math;
p->Set(name,no,score);
f1->Next=p;
p->Next=NULL;
f1=f1->Next;
cout<<"是否继续输入信息?(Y\\N) "<<endl;
cin>>choose;
}while(choose=='y'||choose=='Y');
Save();
cout<<"1.返回主菜单"<<endl;
cin>>choose;
while(choose!='1')
{ cout<<"1.返回主菜单"<<endl;
cin>>choose;
}
Menu();
}
void Function::Delete() //删除信息函数
{ char name[20];
int no;
char choose;
Student *temp,*p;
system("cls");
p=temp=Student_First->Next;
cout<<"请输入姓名:";
cin>>name;
cout<<"输入学号:";
cin>>no;
while(temp)
{ if(strcmp(temp->GetName(),name)==0&&temp->GetNo()==no) //判断该学生信息是否存在
{ cout<<"姓名\t学号\t语文成绩\t数学成绩\n";
temp->Out();
cout<<"\n是否删除(Y/N)";
cin>>choose;
if(choose=='y'||choose=='Y')
{ p->Next=temp->Next;
delete temp;
cout<<"删除成功:\n";
}
break;
}
p=temp;
temp=temp->Next;
}
Save();
cout<<"1.返回主菜单\n2.继续删除"<<endl;
cin>>choose;
while(choose!='1'&&choose!='2')
{ cout<<"1.返回主菜单\n2.继续删除"<<endl;
cin>>choose;
}
if(choose=='1')
Menu();
else if(choose=='2')
Delete();
}
void Function::Modify() //修改学生信息函数
{ char choose,name[20];
Student *temp,*p;
int no;
Class score;
system("cls");
temp=p=Student_First;
cout<<"请输入您要修改的学生姓名:";
cin>>name;
while(temp)
{ if(strcmp(temp->GetName(),name)==0)
{ cout<<"姓名\t学号\t语文成绩\t数学成绩\n";
temp->Out();
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入学号:";
cin>>no;
cout<<"请输入语文成绩:";
cin>>score.Chinese;
cout<<"请输入数学成绩:";
cin>>score.Math;
temp->Set(name,no,score);
break;
}
temp=temp->Next;
}
Save();
cout<<"修改成功!"<<endl;
cout<<"1.返回主菜单\n2.继续修改"<<endl;
cin>>choose;
while(choose!='1'&&choose!='2')
{ cout<<"1.返回主菜单\n2.继续修改"<<endl;
cin>>choose;
}
if(choose=='1')
Menu();
else if(choose=='2')
Modify();
}
void Function::Read() //读取信息函数
{ Student *p,*p2;
p=Student_First;
long t;
ifstream is("Student.txt",ios::in);
if(!is)
{ ofstream os("Student.txt",ios::out);
os.close();
return ;
}
while(!is.eof())
{ p2=new Student;
p2->Infile(is);
p->Next=p2;
p2->Next=NULL;
p=p->Next;
}
}
void Function::Save() //保存学生成绩信息函数
{ ofstream of("Student.txt",ios::out);
Student *p=Student_First->Next;
while(p)
{ p->Ofile(of);
p=p->Next;
}
of.close();
}
void Function::Search()
{ int flag(0);
char choose;
char t1[20];
int t2;
system("cls");
Student *temp=Student_First->Next;
do
{ cout<<"输入查询方式:\n1.按姓名查询\n2.按学号查询\n";
cin>>choose;
if(choose=='1')
{ cout<<"请输入您要查询的姓名:";
cin>>t1;
while(temp)
{ if(strcmp(t1,temp->GetName())==0)
{ flag=1;
break;
}
temp=temp->Next;
}
if(flag==0)
cout<<"\n无该学生的信息\n"<<endl;
else
{ cout<<"姓名\t学号\t语文成绩\t数学成绩\n";
temp->Out();
}
break;
}
else if(choose=='2')
{ cout<<"请输入您要查询的学号";
cin>>t2;
while(temp)
{ if(t2==temp->GetNo())
{ flag=1;
break;
}
temp=temp->Next;
}
if(flag==0)
cout<<"\n无该学生的信息\n"<<endl;
else
{ cout<<"姓名\t学号\t语文成绩\t数学成绩\n";
temp->Out();
}
break;
}
}while(choose!='1'||choose!='2');
cout<<"\n1.返回主菜单\n2.继续查询"<<endl;
cin>>choose;
while(choose!='1'&&choose!='2')
{ cout<<"1.返回主菜单\n2.继续查询"<<endl;
cin>>choose;
}
if(choose=='1')
Menu();
else if(choose=='2')
Search();
}
void Function::Show()
{ char choose;
Student *temp;
system("cls");
temp=Student_First->Next;
if(!temp)
{ cout<<"文件无数据\n\n "<<endl;
cout<<"1.返回主菜单"<<endl;
cin>>choose;
while(choose!='1')
{ cout<<"1.返回主菜单"<<endl;
cin>>choose;
}
Menu();
}
else
{ cout<<"姓名\t学号\t语文成绩\t数学成绩\n";
while(temp!=NULL)
{ temp->Out();
temp=temp->Next;
}
}
cout<<"1.返回主菜单"<<endl;
cin>>choose;
while(choose!='1')
{ cout<<"1.返回主菜单"<<endl;
cin>>choose;
}
Menu();
}
void Function::Menu()
{ time_t t;
time(&t);
char choose;
system("cls");
cout<<" ------------------------版权所有:张嘉铎-------------------------"<<endl;
cout<<endl;
cout<<"***********************************************************"<<endl;
cout<<" 武汉工程大学 "<<endl<<endl;
cout<<" 学生成绩信息管理系统 "<<endl<<endl;
cout<<" 显示系统时间和日期: "<<ctime(&t)<<endl;
cout<<"*********************************************************"<<endl<<endl;
cout<<"请选择您需要的操作,选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;
cout<<"\n";
cout<<" 1 录入学生成绩信息:\n"<<endl;
cout<<" 2 查询学生成绩信息:\n"<<endl;
cout<<" 3 删除学生成绩信息:\n"<<endl;
cout<<" 4 修改学生成绩信息:\n"<<endl;
cout<<" 5 显示全部学生成绩信息:\n"<<endl;
cout<<" 6 退出系统"<<endl;
cout<<"\n";
cin>>choose;
switch(choose)
{ case '1': Add();break;
case '2': Search();break;
case '3': Delete();break;
case '4': Modify();break;
case '5': Show();break;
case '6': exit(1);break;
default:
{ cout<<"请按规定输入选择项!"<<endl;
Menu();
}
}
}
void main()
{ Function function; //定义功能接口
function.Menu(); //调用主菜单
}