Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341154
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:07

#include
#include

struct student        
{
 int num;
 int score;
 struct student *next;
};

student *Creat() /* 创建学生链表,结点个数不限,输入学号0就表示结束*/
{
student *head,*p1,*p2;
head=p1=p2=new student;

int i=1;
cout<<   "*输入学生数据,输入学号为0时表示结束本次输入*\n\n";

while(1)
{
cout<<"       输入第"<    cin>>p1->num;
if(p1->num==0)
break;
    cout<<"       输入第"<    cin>>p1->score;
    cout<<"\n";
    p1=new student;
    p2->next=p1;
    p2=p1;
i++;
}
p2->next=NULL;

return(head);
}


student *Delete(student *head)
{
   student *p1,*p2;
   int Num;
   
   cout<<"请输入要删除学生的学号:";
   cin>>Num;

   if(head==NULL)
   {
      cout<<"没找到:";
  return(head);
   }
   p1=head;
   while((Num!=p1->num) && (p1->next!=NULL))
   {
   p2=p1;
   p1=p1->next;
   }
   if(Num==p1->num)
   {
       if(p1==head)
   head=p1->next;
   else
   p2->next=p1->next;
   delete p1;
   cout<<"已删除:"<   }
   else
   cout<   return(head);
}

student *Insert(student *head)
{
   student *p0,*p1,*p2;
   int Num,ab;

   cout<<"请输入要增加学生的学号:";
   cin>>Num;
   cout<<"请输入要增加到学号为多少的同学前:";
   cin>>ab;

   p1=head;
    while(p1->num!=ab)
   {
   p2=p1;
   p1=p1->next;
   if(p1->next==NULL)
   {
   cout<<"输入错误  请重新输入要增加到学号为多少的同学后:";
   cin>>ab;
   }
  
    }
p0=p2->next;
    p1=p0->next;
cout<<"请输入此学生的成绩:";
cin>>p0->score;


return(head);
}



void Output(student *head)
{
student *p1;
int i=1;
p1=head;

while(p1->num!=0)
{
cout<<"第"<cout<num<<"\n";
cout<<"第"<cout<score<<"\n";
cout<<"\n";

p1=p1->next;
i++;
}

}



void Save(student *head)
{
FILE *fp;

student *p1;
p1=head;
char aa[50];

cout<<"请输入要保存的绝对地址:";
cin>>aa[50];

if((fp=fopen(aa,"wb"))==NULL)
{cout<<"Can't open file!"<<"\n";
 return;
}

while(p1!=NULL&&p1->num!=0)
{
  if(fwrite(p1,sizeof(struct student),1,fp)!=1)
    printf("File write error!\n");

  p1=p1->next;
}
}

void Read(student *head)
{
FILE *fp;
int flag=1;
char aa[50];

cout<<" 请输入要打开文件的绝对地址:";
cin>>aa[50];

student *p1,*p2;
p1=head;

if((fp=fopen(aa,"rb"))==NULL)
{cout<<"Can't open file!"<<"\n";
 return;
}

while(p1!=NULL&&flag==1)
{
  fread(&p1->num,sizeof(int),1,fp);
  fread(&p1->score,sizeof(int),1,fp);
  fread(&p2,sizeof(struct student *),1,fp);
  if(p1->num==0)
  flag=0;
  p1=p1->next;
}

fclose(fp);
}

void Sort(student *head)
{
student *p1,*p2,*pa1,*pa2,*tm1,*tm2,*a,*b;
int min;

p1=p2=head;

while(p1->next!=NULL)
{
min=p1->num;
pa2=p1;
pa1=p1->next;
while(pa1!=NULL)
{
   if(pa1->num    {
min=pa1->num;
a=pa1;
b=pa2;
   }
   pa2=pa1;
   pa1=pa1->next;
}

tm1=p1->next;
p1->next=a->next;
a->next=tm1;

if(p2==head)
{
tm2=head;
head=b->next;
b->next=head;
}
else
{
tm2=p2->next;
    p2->next=b->next;
    b->next=tm2;
}
p2=p1;
p1=p1->next;
}

}

void main()
{
student *head;
int m;

cout<<"                     ***********************\n";
cout<<"                       欢迎进入学生管理系统\n";
cout<<"                     ***********************\n";

cout<<"\n"<<"          1  创建学生库           ";
cout<<"2  插入学生";
cout<<"\n"<<"          3  删除学生             ";
cout<<"4  读入学生库";
cout<<"\n"<<"          5  保存                 ";
cout<<"6  退出"<<"\n";

while(1)
{
cout<<"\n*请选择你需要的操作:  ";
    cin>>m;
cout<<"\n";
    switch(m)
{
     case 1:head=Creat();
            break;
case 2:Insert(head);
    break;

     case 3:Delete(head);
    break;

     case 4:
            Read(head);
            cout<<"Student Data Are:"<<"\n";
            Output(head);
            break;
case 5:Save(head);
    break;
     case 6:return;
}
}

}

我现在做的最大的程序!!但检查甚多遍,就是运行结果跟预想不一样 ````请高手们帮帮忙了 哦~~~~!万分感谢~!

--------------------next---------------------

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