Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61824
  • 博文数量: 7
  • 博客积分: 192
  • 博客等级: 入伍新兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-15 08:58
文章分类

全部博文(7)

文章存档

2012年(1)

2011年(6)

分类: C/C++

2011-11-13 23:25:35

用单链表创建的学生成绩管理系统,实现学生成绩的动态管理,按学号添加学生成绩,删除学生成绩,统计总成绩,计算平均成绩等基本功能。
具体代码如下(仅供学习):
#include
#include
#include
struct Link/*定义结构体链表*/
{
    int number;
    char name[20];
    char ***[4];
    int chinese;
    int math;
    int lizong;
    int english;
    int sum;
    float average;
    struct Link*next;
};
void Picture(void);/*进行函数调用*/
char Menu1(void);
char Menu2(void);
struct Link *Append(struct Link *head);
void Print(struct Link*head);
struct Link *Delete(struct Link *head);
struct Link *Change(struct Link *head);
void Find(struct Link*head);
void Sort1(struct Link*head);
void Sort2(struct Link*head);
void Sort3(struct Link*head);
void Sort4(struct Link*head);
void Deletememory(struct Link*head);
void Beifen(struct Link*head);
struct Link*Huanyuan(struct Link*head);
int Total(struct Link*head);
const char *file="wenjian";
char *Mima(void);
int main()
{
 char choice,ch,shuru[7],mima[7]={'1','2','3','4','5','6'},i;
    struct Link *head=NULL;
    system("color 4f");
    Picture();
    printf("登陆身份认证:\n你是一位老师还是一名学生?(Teacher/Student)\n");
    scanf("%c",&ch);
    if(ch=='T'||ch=='t')
    {
       do
       { printf("请输入6位密码:\n");
        scanf("%s",shuru);
        if(strcmp(shuru,mima)==0)
        break;
        else printf("输入密码不正确\n");
       }while(strcmp(shuru,mima)!=0);
     while(1)
    {
        system("cls");
        choice=Menu1();
        switch(choice)
        {
            case '1':
                     head=Append(head);
                     system("cls");
                     break;
            case '2':
                     Print(head);
                     break;
            case '3':
                     head=Delete(head);
                     break;
            case '4':system("cls");
                     head=Change(head);
                     printf("想要查看吗?(Y/N)");
                     scanf(" %c",&ch);
                     if(ch=='Y'||ch=='y')
                     Print(head);
                     break;
            case '5':Find(head);
                     break;
            case '6':Sort1(head);
                     printf("已经排好顺序了\n");
                     break;
            case '7':Sort2(head);
                    printf("已经排好顺序了\n");
                     break;
            case '8':Sort3(head);
                     printf("已经排好顺序了\n");
                     break;
            case '9':Sort4(head);
                    printf("已经排好顺序了\n");
                     break;
            case 'A':Beifen(head);
                     exit(0);
            case 'B':head=Huanyuan(head);
                      i=getchar();
                     printf("您已经成功还原,按ENTER键返回主菜单\n");
                     i=getchar();
                     break;
            case 'C':Deletememory(head);
                     exit(0);
            default:printf("input error!");
                    break;
        }
    }
 }
 else
 {
   while(1)
    {
        system("cls");
        choice=Menu2();
        switch(choice)
        {
            case '1':
                     Print(head);
                     break;
            case '2':Find(head);
                     break;
            case '3':Sort1(head);
                      i=getchar();
                     printf("已经排好顺序了,按ENTER键返回主菜单\n");
                     i=getchar();
                     break;
            case '4':Sort2(head);
                     i=getchar();
                     printf("已经排好顺序了,按ENTER键返回主菜单\n");
                     i=getchar();
                     break;
            case '5':Sort3(head);
                     i=getchar();
                     printf("已经排好顺序了,按ENTER键返回主菜单\n");
                     i=getchar();
                     break;
            case '6':Sort4(head);
                     i=getchar();
                     printf("已经排好顺序了,按ENTER键返回主菜单\n");
                     i=getchar();
                     Print(head);
                     break;
            case '7':head=Huanyuan(head);
                     i=getchar();
                     printf("您已经成功还原,按ENTER键返回主菜单\n");
                     i=getchar();
                     break;
            case '8':Deletememory(head);
                     exit(0);
            default:printf("input error!");
                    break;
        }
    }
 }
 return 0;
}
char Menu1(void)/*教师菜单*/
{
    char ch;
    printf("%5\t教师菜单\n");
    printf("创名校 做名师 育名人\n");
    printf("%5\t1、增添\n");
    printf("%5\t2、列表显示\n");
    printf("%5\t3、删除\n");
    printf("%5\t4、修改\n");
    printf("%5\t5、查询\n");
    printf("%5\t6、以总分降序排列\n");
    printf("%5\t7、以总分升序排列\n");
    printf("%5\t8、以学号降序排列\n");
    printf("%5\t9、以学号升序排列\n");
    printf("%5\tA、备份文件\n");
    printf("%5\tB、还原文件\n");
    printf("%5\tC、退出\n");
    printf("please enter your choice:\n");
    scanf(" %c",&ch);
    return ch;
}
char Menu2(void)/*学生菜单*/
{
     char ch;
    printf("%5\t学生菜单\n");
    printf("规格严格  功夫到家\n");
    printf("%5\t1、列表显示\n");
    printf("%5\t2、查询\n");
    printf("%5\t3、以总分降序排列\n");
    printf("%5\t4、以总分升序排列\n");
    printf("%5\t5、以学号降序排列\n");
    printf("%5\t6、以学号升序排列\n");
    printf("%5\t7、还原文件\n");
    printf("%5\t8、退出\n");
    printf("please enter your choice:\n");
    scanf(" %c",&ch);
    return ch;
}
struct Link *Append(struct Link *head)/*增添的函数*/
{
   char c;
   do{
        system("cls");
        system("color 1f");
        struct Link *p=NULL;
        struct Link *pr=head;
        p=(struct Link*)malloc(sizeof(struct Link));
        if(p==NULL)
           {
                printf("没有足够内存,返回主菜单\n");
                break;
            }
        if(head==NULL)
            {
                head=p;
            }
            else
            {
                while(pr->next!=NULL)
                {
                    pr=pr->next;
                }
                pr->next=p;
            }
                pr=p;
                printf("请输入学号\n");
                scanf("%d",&p->number);
                printf("请输入姓名\n");
                scanf("%s",p->name);
                printf("请输入性别\n");
                scanf("%s",p->***);
                printf("请输入语文成绩(0——150)\n");
                do{
                    scanf("%d",&p->chinese);
                if((p->chinese)<0||(p->chinese)>150)
                printf("输入分数有误,请重新输入\n");
                }while((p->chinese)<0||(p->chinese)>150);
                printf("请输入数学(0——150)\n");
                scanf("%d",&p->math);
                printf("请输入理科综合成绩(0——300)\n");
                scanf("%d",&p->lizong);
                printf("请输入英语成绩(0——150)\n");
                scanf("%d",&p->english);
                p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
                p->average=(float)(p->sum)/4;
                pr->next=NULL;
                printf("\nnew nodes have been append!\n");
                printf("\n是否想新增加一个新成员?(Y/N)\n");
                scanf(" %c",&c);
            }
    while(c=='Y'||c=='y');
    return head;
}
void Print(struct Link*head)/*列表显示的函数*/
{
    if(head==NULL)
    {
        char i=getchar();
        printf("无内容,无法显示,按ENTER键返回菜单\n");
        i=getchar();
        system("cls");
    }
    else{
        printf("学号   姓名   性别   语文   数学   理科综合   英语   总分   平均分\n");
    struct Link*p=head;
    while(p!=NULL)
    {
        printf("\n%d   %s     %s     %d",p->number,p->name,p->***,p->chinese);
        printf("  %d   %d     %d     %d     %.3f",p->math,p->lizong,p->english,p->sum,p->average);
        p=p->next;
    }
    char i=getchar();
    printf("\n按ENTER键返回主菜单\n");
    i=getchar();
    system("cls");
    }
}
struct Link *Delete(struct Link *head)/*删除的函数*/
{
         if(head==NULL)
    {
        char i=getchar();
        printf("链表为空,没有要删除对象,按ENTER键返回主菜单\n");
        i=getchar();
        system("cls");
    }
    else{
    int c;
    char ch;
    do{
         if(head==NULL)
    {
        char i=getchar();
        printf("链表为空,没有要删除对象,按ENTER键返回主菜单\n");
        i=getchar();
        system("cls");
        break;
    }
    printf("请输入要删除的学号\n");
    scanf("%d",&c);
    struct Link *p=head,*pr=head;
    while(c!=p->number&&p->next!=NULL)
    {
        pr=p;
        p=p->next;
    }
    if(c==p->number)
    {
        if(p==head)
        {
            head=p->next;
        }
        else
        {
            pr->next=p->next;
        }
        free(p);
        printf("您已经成功删除");
    }
    else printf("没有找到!");
    printf("\n想继续吗?(Y/N)");
    scanf(" %c",&ch);
    }while(ch=='Y'||ch=='y');
    system("cls");
}
return head;
}
struct Link *Change(struct Link *head)/*修改的函数*/
{
    int c;
    char ch;
    do{
         if(head==NULL)
    {
        printf("链表为空,没有要删除对象\n");
        break;
    }
    printf("请输入要修改的学号\n");
    scanf("%d",&c);
    struct Link *p=head;
    while(c!=p->number&&p!=NULL)
    {
        p=p->next;
    }
    if(c==p->number)
    {
        printf("请输入改后学号\n");
        scanf("%d",&p->number);
        printf("请输入改后姓名\n");
        scanf("%s",p->name);
        printf("请输入改后性别\n");
        scanf("%s",p->***);
        printf("请输入改后语文成绩\n");
        scanf("%d",&p->chinese);
        printf("请输入改后数学\n");
        scanf("%d",&p->math);
        printf("请输入改后理科综合成绩\n");
        scanf("%d",&p->lizong);
        printf("请输入改后英语成绩\n");
        scanf("%d",&p->english);
        p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
        p->average=(float)(p->sum)/4;
        printf("您已经成功修改\n");
    }
    else printf("没找到!\n");
    printf("想继续修改吗?(Y/N)");
    scanf(" %c",&ch);
    }while(ch=='Y'||ch=='y');
    return head;
}
void Find(struct Link*head)/*查找的函数*/
{
    int c;
    char ch;
    struct Link*p=head;
   do{
       printf("请输入想要查询的学号:\n");
   scanf("%d",&c);
   if(head==NULL)
   {
       printf("链表为空,没有查询对象\n");
        break;
   }
   while(c!=p->number&&p!=NULL)
   {
       p=p->next;
   }
    if(c==p->number)
    {
      printf("学号   姓名   性别   语文   数学   理科综合   英语   总分   平均分\n");
      printf("\n%d       %s        %s      %d",p->number,p->name,p->***,p->chinese);
      printf("     %d       %d       %d       %d     %.2f",p->math,p->lizong,p->english,p->sum,p->average);
    }
    else printf("您的输入有误,暂时没有此学号的学生,请查证后再查询\n");
    printf("想继续查询吗?(Y/N)\n");
    scanf(" %c",&ch);
   }while(ch=='Y'||ch=='y');
}
void Picture(void)/*超豪华界面,后来发现可以比这更简单,但已经做完了,就没有修改*/
{
   printf ("hello world");
}
void Sort1(struct Link*head)/*排序函数1*/
{
    struct Link *p;
    int flag=0;
    char temp1[20];
    int temp2;
    do
    {
             if(head==NULL)
        {
            char i=getchar();
            printf("链表为空,按ENTER键返回菜单\n");
            i=getchar();
            break;
        }
        flag=0;
        p=head;
        while(p->next!=NULL)
        {
            if((p->sum)<(p->next->sum))/*只交换节点内容*/
            {
                temp2=(p->next->sum);//sum
                (p->next->sum)=(p->sum);
                (p->sum)=temp2;
                strcpy(temp1,(p->next->name));//name
                strcpy((p->next->name),(p->name));
                strcpy((p->name),temp1);
                strcpy(temp1,(p->next->***));//***
                strcpy((p->next->***),(p->***));
                strcpy((p->***),temp1);
                temp2=(p->next->chinese);
                (p->next->chinese)=(p->chinese);
                (p->chinese)=temp2;
                 temp2=(p->next->math);
                (p->next->math)=(p->math);
                (p->math)=temp2;
                 temp2=(p->next->lizong);
                (p->next->lizong)=(p->lizong);
                (p->lizong)=temp2;
                 temp2=(p->next->english);
                (p->next->english)=(p->english);
                (p->english)=temp2;
                 temp2=(int)(p->next->average);
                (p->next->average)=(p->average);
                (p->average)=temp2;
                flag=1;
            }
            p=p->next;
        }
    }while(flag);
}
void Sort2(struct Link*head)/*排序函数2*/
{
    struct Link*p;
    int flag=0;
    char temp1[20];
    int temp2;
    do
    {
              if(head==NULL)
        {
            char i=getchar();
            printf("链表为空,按ENTER键返回菜单\n");
            i=getchar();
            break;
        }
        flag=0;
        p=head;
        while(p->next!=NULL)
        {
            if((p->sum)>(p->next->sum))/*只交换节点内容*/
            {
                temp2=(p->next->sum);//sum
                (p->next->sum)=(p->sum);
                (p->sum)=temp2;
                strcpy(temp1,(p->next->name));//name
                strcpy((p->next->name),(p->name));
                strcpy((p->name),temp1);
                strcpy(temp1,(p->next->***));//***
                strcpy((p->next->***),(p->***));
                strcpy((p->***),temp1);
                temp2=(p->next->chinese);
                (p->next->chinese)=(p->chinese);
                (p->chinese)=temp2;
                 temp2=(p->next->math);
                (p->next->math)=(p->math);
                (p->math)=temp2;
                 temp2=(p->next->lizong);
                (p->next->lizong)=(p->lizong);
                (p->lizong)=temp2;
                 temp2=(p->next->english);
                (p->next->english)=(p->english);
                (p->english)=temp2;
                 temp2=(int)(p->next->average);
                (p->next->average)=(p->average);
                (p->average)=temp2;
                flag=1;
            }
            p=p->next;
        }
    }while(flag);
}
void Sort3(struct Link*head)/*排序函数3*/
{
    struct Link*p;
    int flag=0;
    char temp1[20];
    int temp2;
    do
    {
              if(head==NULL)
        {
            char i=getchar();
            printf("链表为空,按ENTER键返回菜单\n");
            i=getchar();
            break;
        }
        flag=0;
        p=head;
        while(p->next!=NULL)
        {
            if((p->number)<(p->next->number))/*只交换节点内容*/
            {
                temp2=(p->next->sum);//sum
                (p->next->sum)=(p->sum);
                (p->sum)=temp2;
                strcpy(temp1,(p->next->name));//name
                strcpy((p->next->name),(p->name));
                strcpy((p->name),temp1);
                strcpy(temp1,(p->next->***));//***
                strcpy((p->next->***),(p->***));
                strcpy((p->***),temp1);
                temp2=(p->next->chinese);
                (p->next->chinese)=(p->chinese);
                (p->chinese)=temp2;
                 temp2=(p->next->math);
                (p->next->math)=(p->math);
                (p->math)=temp2;
                 temp2=(p->next->lizong);
                (p->next->lizong)=(p->lizong);
                (p->lizong)=temp2;
                 temp2=(p->next->english);
                (p->next->english)=(p->english);
                (p->english)=temp2;
                 temp2=(int)(p->next->average);
                (p->next->average)=(p->average);
                (p->average)=temp2;
                flag=1;
            }
            p=p->next;
        }
    }while(flag);
}
void Sort4(struct Link*head)/*排序函数4*/
{
    struct Link*p;
    int flag=0;
    char temp1[20];
    int temp2;
    do
    {
              if(head==NULL)
        {
            char i=getchar();
            printf("链表为空,按ENTER键返回菜单\n");
            i=getchar();
            break;
        }
        flag=0;
        p=head;
        while(p->next!=NULL)
        {
            if((p->number)>(p->next->number))/*只交换节点内容*/
            {
                temp2=(p->next->sum);//sum
                (p->next->sum)=(p->sum);
                (p->sum)=temp2;
                strcpy(temp1,(p->next->name));//name
                strcpy((p->next->name),(p->name));
                strcpy((p->name),temp1);
                strcpy(temp1,(p->next->***));//***
                strcpy((p->next->***),(p->***));
                strcpy((p->***),temp1);
                temp2=(p->next->chinese);
                (p->next->chinese)=(p->chinese);
                (p->chinese)=temp2;
                 temp2=(p->next->math);
                (p->next->math)=(p->math);
                (p->math)=temp2;
                 temp2=(p->next->lizong);
                (p->next->lizong)=(p->lizong);
                (p->lizong)=temp2;
                 temp2=(p->next->english);
                (p->next->english)=(p->english);
                (p->english)=temp2;
                 temp2=(int)(p->next->average);
                (p->next->average)=(p->average);
                (p->average)=temp2;
                flag=1;
            }
            p=p->next;
        }
    }while(flag);
}
void Deletememory(struct Link *head)/*清楚内存*/
{
    struct Link*p=head,*pr=NULL;
    while(p!=NULL)
    {
        pr=p;
        p=p->next;
        free(pr);
    }
}
  void Beifen(struct Link*head)/*仿照数据库的思路,用备份和还原的方法解决文件问题*/
  {
      FILE*fp;
      struct Link*p=head;
      int x=0;
      fp=fopen(file,"w+");
      if(fp==NULL)
      {
          printf("Can't open %s file\n",file);
          exit(1);
      }
      x=Total(head);
      fputc(x,fp);
       while(p!=NULL)
      {
          fwrite(p,sizeof(struct Link),1,fp);
          p=p->next;
      }
      printf("您已经成功备份,按ENTER键退出系统\n");
      fclose(fp);
  }
  struct Link*Huanyuan(struct Link*head)/*还原文件*/
  {
      int m=1,n;
      FILE*fp;
      fp=fopen(file,"r+");
       if(fp==NULL)
      {
          printf("Can't open %s file\n",file);
          //exit(1);
      }
      struct Link*p=NULL;
      struct Link*pr=NULL;
       n=fgetc(fp);
    pr=(struct Link*)malloc(sizeof(struct Link));
    fread(pr,sizeof(struct Link),1,fp);
    head=pr;
      while(m<=n)
      {
          p=(struct Link*)malloc(sizeof(struct Link));
          fread(p,sizeof(struct Link),1,fp);
          pr->next=p;
          pr=p;
          pr->next=NULL;
          m=m+1;
      }
      return head;
  }
int Total(struct Link*head)/*计算输入的学生总数,在还原函数中要用到*/
{
    struct Link*p=head;
    int n=-1;
    while(p!=NULL)
    {
        n++;
        p=p->next;
    }
    return n;
}
阅读(9166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~