| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:请问这个程序有什么问题啊?
只看楼主 加入收藏
dj1310002607
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
请问这个程序有什么问题啊?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*宏定义*/
#define N 100      /*假设学生个数是100*/
#define HEADER1 "--------------------------------------------------------------------\n"
#define HEADER2 "| Number | Name | Computer | Math | English | Sum | Average | Place |\n"
#define HEADER3 "|--------|------|----------|------|---------|-----|---------|-------|\n"
#define FORMAT "|%-10d|%-8d|%8d|%8d|%8d|%5d|%7.2f|%5d|\n"
#define DATA p->num,p->name,p->course,p->mscore,p->escore,p->total,p->ave,p->place
#define END    "-----------------------------------------------------------------\n"
/*定义数据单元的结构体*/
typedef struct student
{
        int num;                                                                    /*学号*/
        char name[15];                                                              /*姓名*/
        int cscore;                                                                 /*计算机成绩*/
        int mscore;                                                                 /*数学成绩*/
        int escore;                                                                 /*英语成绩*/
        int total;                                                                  /*总分*/
        float ave;                                                                  /*平均分*/
        int place;                                                                  /*名次*/
}STU;
/*数据单元序列的相关信息*/
typedef struct pointer_info
{
        STU*phead;                                                                  /*指向第一个数据单元的指针*/
        int count;                                                                  /*数据单元的个数*/
        char fname[15];                                                             /*数据记录的文件名*/
        int saveflag;                                                               /*0:已保存;1: 修改未保存*/
}PI;
/*函数申明*/
void Menu();                                                                        /*主菜单*/
void Add(PI*);                                                                      /*添加新数据记录*/
void Disp(PI*);                                                                     /*显示数据记录*/
void Del(PI*);                                                                      /*删除数据记录*/
void Save(PI*pi);                                                                   /*保存数据记录*/
void our(PI*pi);                                                                    /*查询数据记录*/
void Modify(PI*pi);                                                                /*修改数据记录*/
void Sort(PI*pi);                                                                   /*排序数据记录*/
void Load(PI*pi);                                                                   /*加载数据记录文件*/
void New(PI*pi);                                                                    /*新建数据记录文件*/
void Wrong();                                                                       /*错误提示*/
void printheader();                                                                 /*格式化输出表头*/   
void main()
{
     int sel;
     STU * pstu;
     PI pi;
     /*分配数据序列单元内存*/
     pstu = (STU*)malloc(N * sizeof(STU));
     /*初始化pi的成员*/
     pi.pHead = pstu;
     pi.count = 0;
     pi.fname[0] = '\0';
     pi.saveflag = 0;
     /*显示主菜单*/
     Menu();
     printf("Please input your choice[0~9]:");
     do {
         scanf("%d",&sel);
         if (sel == 0)
         {
                 if (pi.saveflag == 1)
                 {
                                 if (strlen(pi.fname))
                                 {
                                                      Save(&pi);
                                 }
                                 else
                                 {
                                                          New(&pi);
                                                          Save(&pi);
                                 }
                 }
                  break;
         }
         /*根据键盘输入进入相应的功能模块*/
         switch(sel)
         {
                    case 1:Add(&pi);break;                /*增加学生记录*/
                    case 2:Disp(&pi);break;               /*显示学生记录*/
                    case 3:Our(&pi);break;                /*查询学生记录*/
                    case 4:Modify(&pi);break;             /*修改学生记录*/
                    case 5:Del(&pi);break;                /*删除学生记录*/
                    case 6:Sort(&pi);break;               /*排序学生记录*/
                    case 7:New(&pi);break;                /*新建数据文件*/
                    case 8:Load(&pi);break;               /*加载数据文件*/
                    case 9:Save(&pi);break;               /*保存数据文件*/
                    default:Wrong();break;                /*按键有误,必须为数值0~9*/
         
          } while(1);
          /*释放内存*/
          pstu = pi.pHead;
          free(pstu);
}
                                      
/*格式化输出表头*/
void printheaher()
{
     printf(HEADER1);
     printf(HEADER2);
     printf(HEADER3);
}
/*主菜单*/
void Menu()
{
     system("cls");  /*调用DOS命令,清屏,与clrscr()功能相同*/
     printf("        The Students' Grade Management SSystem \n");
     printf("        ************************MENU***************************\n");
     printf("        *            1 input record                *\n");
     printf("        *            2 display record              *\n");
     printf("        *            3 search record               *\n");
     printf("        *            4 modify record               *\n");
     printf("        *            5 delete                      *\n");
     printf("        *            6 sort record                 *\n");
     printf("        *            7 new                         *\n");
     printf("        *            8 load                        *\n");
     printf("        *            9 save                        *\n");
     printf("        *            0 quit                        *\n");
     printf("*************************************************************\n");
     }
     /*输出错误信息*/
void Wrong()
{
     printf("\n******Error: input has wrong! press any key to continue******\n");
}
/*由键盘录入学生成绩记录*/
void Add(PI * pi)
{
     int num;
     int i, numRepeatFlag = 0;
     STU * pstu, *p;
     
     do {
         /*pstu指向下一个需要写入数据的单元*/
         pstu = pi->pHead + pi->count;
         /**p用来寻找重复的学号,因此指向数据序列的第一个单元*/
         p = pi->pHead;
         
         printf("please input the number: (enter '0' return menu)");
         scanf("%d",&num);
         /*学好重复处理: 要求输入的学号不能重复,否则重新输入*/
         for (I = 1; i < pi->count;i++)
         {
             if (num == p->num)
             {
                     printf("the number you input has exit,please inout another one!\n");
                     numRepeatFlag = 1;
                     break;
             }
             p++;
         }
         if (numRepeatFlag)
         {
                      numRepeatFlag = 0;
                      continue;
         }
         else
         {
             Menu();
             printf("Please input your choice[0~9]:");
             break;
         }
         /*输入姓名*/
         printf("please input the name:")
         scanf("%s",pstu->name);
         /*输入计算机成绩*/
         printf("please input the computer score:");
         scanf("%d",&pstu->cscore);
         /*输入数学成绩*/
         printf("please input the math score:");
         scanf("%d",&pstu->mscore);
         /*输入英语成绩*/
         printf("please input the english score:");
         scanf("%d",&pstu->escore);
         
         /*计算总分和平均分*/
         pstu->total = pstu->cscore + pstu->mscore + pstu->escore;
         pstu->ave = (float)(pstu->total) / 3;
         /*排名暂时清零,在排序模块中实现排名*/
         pstu->place = 0;
         /*数据单元个数自增1*/
         pi->count++;
         /*数据修改,为保存标志置1*/
         pi->saveflag = 1;
         } while(1);
}
/*在屏幕上先是学生成绩记录*/
void Disp(PI * pi)
{
     int i;
     STU * p;
     /*显示主菜单*/
     Menu();
     p = pi->pHead;
     if (pi->count == 0)
     {
                   printf("no student record!");
                   printf("\nPlease input your choice[0~9]:");
                   return;
     }
     /*打印表格头部*/
     printheader();
     /*打印数据序列*/
     for (i = 1; i <=pi->count; i++)
     {
         printf(FORMAT,DATE);
         printf(HEADER3);
         p++;
     }
}
/*查询*/
void Qur(PI * pi)
{
     int sel, i, num;
     STU * p;
     char name[15];
     /*p为指向数据序列首部的指针*/
     p = pi->pHead;
     Menu();
     /*1:根据学号查询;2:根据姓名查询;其他:返回*/
     printf("1------------------------------search by number\n");
     printf("2------------------------------search by name\n");
     printf("any key other------------------return\n");
     printf("please input your choice:[1,2]?");
     
     scanf("%d",&sel);
     if (sel != 1 && sel !=2)
     {
             return;
     }
     
     if (sel == 1)/*按学号查询*/
     
              printf("please input the number for search:");
              scanf("%d",&num);
              
              /*按学号搜索*/
              for (i = 1; i <= pi->count; i++)
              {
                  if (num == p->num)
                  {
                          break;
                  }
                  p++;
              }
              /*没有找到记录的情况*/
              if (i > pi->count)
              {
                    printf("no record found!\n");
                    return;
              }
              else{/*找到则打印输出*/
                   printheader();
                   printf(FORMAT,DATA);
                   printf(END);
                   }
     }
     else if (sel == 2)/*按姓名查询*/
     {
          printf("please input name to searc:");
          scanf("%s",name);
         
          /*按姓名搜索*/
          for (i = 1; i <= pi->count; i++)
          {
              if (!strcmp(name, p->name))
              {
                       break;
              }
              p++;
          }
          /*没有找到记录的情况*/
          if  (i > pi->count)
          {
              printf("no record found!\n");
              return;
          }
          else
          {
              /*找到则打印输出*/
              printheader();
              printf (FORMAT,DATA);
              printf(END);
          }
     }
     printf("Please input your choice[0~9]:");
}
void Modify(PI * pi)
{
     int num,i;
     STU *p;
     p = pi->pHead;
     
     /*显示主菜单并将当前数据序列打印*/
     Menu();
     Disp(pi);
     
     /*输入要修改的学生学号*/
     printf("please inpput the number of student to modify:");
     scanf("%d", &num);
     
     /*通过指针遍历寻找要修改的数据单元*/
     for (i = 1; i < pi->count; i++)
     {
         if (num = p->num)
         {
                 break;
         }
         p++;
     }
     if (i > pi->count)
     {  /*没有找到记录的情况*/
          printf("no record found!\n");
          return;
     }
     else
     {   /*找到则修改*/
           printf("please input the number:(enter '0' return menu)");
           scanf("%d", &num);
           if (num != 0)
           {
                   p->num = num;
           }
           else
           {
               Menu();
               return;
           }
           printf("please input the name:");
           scanf("%s",p->name);
           printf("please input the computer score:");
           scanf("%d",&p->cscore);
           printf("please input the math score:");
           scanf("%d",&p->mscore);
           printf("please input the english score:");
           scanf("%d",&p->escore);
           /*计算总分和平均分*/
           p->total = p->cscore + p->mscore + p->escore;
           p->ave = (float)(p->total/3);
           p->place = 0;
           /*修改后,将为保存标志为1*/
           pi->saveflag = 1;
     }
}
/*删除学生记录*/
void Del(PI * pi)
{
     int sel, i, num;
     int num;
     char name[15];
     STU * p;
     
     p = pi->pHead;
     Menu();
     /*提供按照学号删除和按照姓名删除两种方式*/
     printf("1-------------------------------delete by number\n");
     printf("2-------------------------------delete by name\n");
     printf("any key other-------------------return\n");
     printf("please input your choice:[1,2]?");
     
     scanf("%d",&sel);
     if (sel != 1 && sel != 2){
             return;
     }
     /*按学号删除*/
     if (sel == 1)
     {
             /*输入要删除的学生记录的学号*/
             printf("please input the number to delete:");
             scanf("%d",&num);
             /*按学号搜索*/
             for (i = 1; i <= pi->count; i++)
             {
                 if (num == p->num)                          
                 {
                         break;
                 }
                 p++;
             }
             if (i > pi->count) {
                   printf("no break found!\n");
                   return;
             }
             else if (i == pi->num) { /*如果删除最后一个记录,只需要讲count减1即可*/
                     pi->count--;
                     pi->saveflag = 1;
                     printf("record delete successfully!\n");
                     }
             else{                  /*删除中间某个记录,只要将其后的数据覆盖即可*/
                      mecpy(p, p+1,(pi->pHead + pi->count - p)*sizeof(STU));
                      pi->count--;
                      pi->saveflag = 1;
                      printf("record delete successfully!\n");
                 }
     }
     else if(sel == 2)/*按名字删除*/
     {
            printf("please input name to delete:");
            scanf("%s",name);
                  
            /*按姓名搜索*/
            for (i = 1; i <= pi-count; i++)
            {
            if (!stremp(name, p->name))
            {
                  break;
            }
            p++;
     }
     if (i > pi->count) {  /*没有找到记录的情况*/
                          printf("no record found!\n");
                          return;
                  }
     else if (i == pi->count)  {          /*如果删除最后一个记录,只需将count减1即可*/
                          pi->count--;
                          pi->saveflag = 1;
                          printf("record delete successfully!\n");
                  }
     else{                                /*删除某个记录,只需将其后的数据前移覆盖即可*/
                          memcpy(p, p+1,(pi->pHead + pi->count - p)*sizeof(STU));
                          pi->count--;
                          pi->saveflag = 1;
                          printf("record delete successfully!\n");
         }
     }
     printf("Please input your choice[0~9]:");
}
void Sort(PI * pi)
{
     int i,j;
     int sel;
     STU * p;
     
     /*提供按照学号排序和按照总分排序两钟方式*/
     printf("1-----------------------------sort by number\n");
     printf("2-----------------------------sort by sum\n");
     printf("any key other-----------------return\n");
     printf("please input your choice:[1,2]?");
     scanf("%d",&sel);
     
     if (sel != 1 && sel != 2){
             return;
     }
     if (sel == 1)
     {
             /*冒泡法。按学号排序*/
             for (i = 1; i <= pi->count -1; i++)
             {
                 p = pi->pHead;
                 for (j = 1; j <= pi->count - i; j++, p++)
                 {
                     if (p->num > (p+1)->num) {
                                Swap(p, p+1);    /*交换两个数据单元,由Swap函数实现*/
                     }
                 }
                 pi->saveflag = 1;
             }
             /*排序后的顺序就是排名(place)*/
             p = pi->pHead;
             for (i = 1; i <= pi->count; i++)
             {
                 p->place = i;
                 p++;
             }
             pi->saveflag = 1;
     }
     /*将排序结果打印输出*/
     Disp(pi);
     printf("Place input your choice[0~9]:");
}
/*新建数据记录文件*/
void New(Pi * pi)
{
     FILE * fp;
     char name[15];
     char fname[15] =".\\";
     printf("please input the data file name for create:");
     scanf("%s",name);
     
     /*数据记录文件保存在当前文件夹下,后缀名为.dat*/
     strcat(fname,name);
     strcat(fname,".dat");
     
     /*以只写方式打开二进制文献*/
     fp=fopean(fname,"wb");
     if(fp==NULL)          /*打开文件失败*/
     {
           printf("\n====>open file error!\n");
           return;
     }
     strcpy(pi->fname, fname);
     fclose(fp);/*关闭文件*/
}
/*加载数据记录文件*/
void Load(PI * pi)
{
     FILE * fp;
     STU * p = pi->pHead;
     char name[15];
     char fname[15] =".\\";
     
     printf("please input the data file name for load:");
     scanf("%s",name);
     
     strcat(fname,name);
     /*以只读方式打开二进制文件*/
     fp=fopen(fname,"rb");
     if(fp==NULL)         /*打开文件失败处理*/
     {
              printf("\n====>open file error!\n");
              return;
     }
      
      /*pi清零*/
      pi->count = 0;
      pi->saveflag = 0;
      while(!feof(fp))
      {
              if(fread(p,sizeof(STU),1,fp))      /*一次从文件中读取一条学生成绩记录*/
              {
                       pi->count++;
                       p++;
              }
      }
      strcpy(pi->fname, name);
      printf("load data file %s successfully!", name);
      fclose(fp);
}
/*保存数据记录文件*/
void Save(PI * pi)
{
     FILE * fp;
     int numwritten;
     /*判断是否数据记录文件存在,如果不存在,则新建一个*/
     if (!strlen(pi->fname))
     New(pi);
     fp=fopen(pi->fname,"wb";/*以只读方式打开二进制文件*/
     if(fp==NULL)            /*带来文件失败*/
     {
            printf("\n====>open file error!\n");
            fclose(fp);
            return;
     }
     /*如果数据记录不为空,则写入文件*/
     if (pi->count)        {
            numwritten = fwrite(pi->pHead, 1, pi->count * sizeof(STU), fp);
            pi->saveflag = 0;
            printf("file save successfully!");
     }
     else
           printf("no record for save!");
           fclose(fp);
}
搜索更多相关主题的帖子: English 100 include course 
2012-07-10 22:32
clyde3
Rank: 2
等 级:论坛游民
帖 子:68
专家分:65
注 册:2012-7-10
收藏
得分:7 
可真多!

堇色安年,谁许我一世荒芜→→玖零逅.那扯蛋的青春!
2012-07-10 22:39
dj1310002607
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-7-10
收藏
得分:0 
回复 2楼 clyde3
呵呵,你能找到哪里出错了吗?万分感谢
2012-07-10 22:50
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:7 
代码真长,你可以把代码分段测试,找出错误。

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2012-07-11 08:35
netlin
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:24
帖 子:544
专家分:4308
注 册:2012-4-9
收藏
得分:7 
楼主,要练习一下如何调试程序啊!

做自己喜欢的事!
2012-07-12 21:57
快速回复:请问这个程序有什么问题啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047946 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved