Chinaunix首页 | 论坛 | 博客
  • 博客访问: 926864
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: C/C++

2009-09-27 11:16:41

#include
#include
#include
#include
using namespace std;
struct Student
{
char num[10];//学号
char name[10];//姓名
int Maths;//数学成绩
Student *next;
} ;
long iCount=0;
Student *head=NULL;
Student *pEnd=NULL;
void LoadRecords(char *path)//从文本里面读取记录
{
fstream infile(path,ios::in|ios::binary);
if(!infile){}
else
{
   infile.read((char*)&iCount,sizeof(long));
   int t=iCount;
   if(iCount!=0)
   {
    for(;iCount>0;iCount--)
     {
      Student *p=new Student;
      infile.read((char*)p,sizeof(Student));
      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<<"| "<   <<"| "<   <<"| "<while(h)
   {
    cout<<" ------------------------------------------\n";
    cout<<"| "<num<<"| "<name<<"| "<Maths<<"    |\n";
    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<<"| "<   <<"| "<   <<"| "<while(h)
   {
    if(h->Maths<40)
    {
     cout<<" ------------------------------------------\n";
     cout<<"| "<num<<"| "<name<<"| "<Maths<<"    |\n";
    }
     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<<"| "<   <<"| "<   <<"| "<while(h)
   {
    if(strstr(h->name,"张"))
    {
     cout<<" ------------------------------------------\n";
     cout<<"| "<num<<"| "<name<<"| "<Maths<<"    |\n";
    }
    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<name<<" 的资料添加完毕!\n";cout<<"==================================================\n";
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<<"请输入内容:"<     cout<<"学号\t\t姓名\t\t数学\n";
     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|ios::binary);
     if(!outfile){}
     else
     {
      outfile.write((char*)&iCount,sizeof(long));
      while(head)
       {
        outfile.write((char*)head,sizeof(Student));
        head=head->next;
       }
     
     }
     outfile.close();
     cout<<"\n==================================================\n";
     cout<<"再见"<     cout<<"==================================================\n\n";
     return;
     }
    default:
     cout<<"\n==================================================\n";
     cout<<"无效输入"<     cout<<"==================================================\n\n";
   }
   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;
   }
}

}

阅读(914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~