分类: C/C++
2009-09-27 11:15:54
#include
#include
#include
#include
using namespace std;
struct Student
{
string num;//学号
string name;//姓名
int Maths;//数学成绩
Student *next;
} ;
long iCount=0;
Student *head=NULL;
Student *pEnd=NULL;
void LoadRecords(char *path)//从文本里面读取记录
{
fstream infile(path,ios::in);
if(!infile){}
else
{
infile>>iCount;
int t=iCount;
if(iCount!=0)
{
for(;iCount>0;iCount--)
{
Student *p=new Student;
infile>>p->num>>p->name>>p->Maths;
if(head==NULL)
{
head=p;pEnd=p;pEnd->next=NULL;
}
else
{
pEnd->next=p;
pEnd=pEnd->next;
pEnd->next=NULL;
}
}
}
iCount=t;
}
infile.close();
}
void Print(Student *h)//打印记录
{
if(h==NULL)
{
cout<<"\n****************************************\n\n";
cout<<"没有学生记录!\n";
cout<<"\n****************************************\n";
return;
}
cout<<"\n********************************************************************************\n\n";
cout<<"所有的学生:\n\n";
cout<<" ------------------------------------------\n";
cout<<"| "<
{
cout<<" ------------------------------------------\n";
cout<<"| "<
h=h->next;
}
cout<<" ------------------------------------------\n";
cout<<"\n********************************************************************************\n\n";
}
void Print_Under40(Student *h)//打印40 分以下的记录
{
if(h==NULL){cout<<"\n****************************************\n";cout<<"没有学生记录!\n";cout<<"\n****************************************\n";return;}
cout<<"\n********************************************************************************\n";
cout<<"40分以下的学生:\n\n";
cout<<" ------------------------------------------\n";
cout<<"| "<
{
if(h->Maths<40)
{
cout<<" ------------------------------------------\n";
cout<<"| "<
}
h=h->next;
}
cout<<" ------------------------------------------\n";
cout<<"\n********************************************************************************\n\n";
}
void Print_Zhang(Student *h)//打印姓张的记录
{
if(h==NULL){cout<<"\n********************************************************************************\n";cout<<"没有学生记录!\n";cout<<"\n****************************************\n";return;}
cout<<"\n********************************************************************************\n";
cout<<"姓张的学生:\n\n";
cout<<" ------------------------------------------\n";
cout<<"| "<
{
if(h->name.find("张")!=-1)
{
cout<<" ------------------------------------------\n";
cout<<"| "<
}
h=h->next;
}
cout<<" ------------------------------------------\n";
cout<<"\n********************************************************************************\n\n";
}
void Add(Student *s)
{
if(pEnd==NULL){head=s;pEnd=s;s->next=NULL;}
else{pEnd->next=s;pEnd=s;pEnd->next=NULL;}
cout<<"\n==================================================\n";cout<
iCount++;
}
void main()
{
LoadRecords("c:\\test.txt");
int n;
cout<<"\n********************************************************************************\n";
cout<<"[1] 增加一个学生\n"
<<"[2] 显示所有的学生以及他们的成绩\n"
<<"[3] 显示数组中成绩<40的学生以及他们的成绩\n"
<<"[4] 显示姓张的学生以及他们的成绩\n"
<<"[5] 存盘并退出\n";
cout<<"********************************************************************************\n\n";
cout<<"请选择操作项:[1-5] ";
cin>>n;
while(1)
{
switch (n)
{
case 1:
{
Student *p=new Student;
cout<<"\n********************************************************************************\n\n";
cout<<"请输入内容:"<
cin>>p->num>>p->name>>p->Maths;Add(p);
break;
}
case 2:
Print(head);break;
case 3:
Print_Under40(head);break;
case 4:
Print_Zhang(head);break;
case 5:
{
fstream outfile("c:\\test.txt",ios::out);
if(!outfile){}
else
{
outfile<
{
outfile<
head=head->next;
}
}
outfile.close();
cout<<"\n==================================================\n";
cout<<"再见"<
return;
}
default:
cout<<"\n==================================================\n";
cout<<"无效输入"<
}
cout<<"***************按任意键继续***************\n";
getchar();getchar();system("cls");
//if(n>=1&&n<=5)
{
cout<<"\n********************************************************************************\n";
cout<<"[1] 增加一个学生\n"
<<"[2] 显示数组中所有的学生以及他们的成绩\n"
<<"[3] 显示数组中平均成绩<40的学生以及他们的成绩\n"
<<"[4] 显示数组中姓张的学生以及他们的成绩\n"
<<"[5] 存盘并退出\n";
cout<<"********************************************************************************\n\n";
cout<<"请选择操作项:[1-5] ";
cin>>n;
}
}
}