鱼C论坛

 找回密码
 立即注册
查看: 3365|回复: 4

c链表的一些问题

 关闭 [复制链接]
发表于 2011-8-10 00:59:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 dong50252409 于 2011-8-10 12:10 编辑
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #define LEN sizeof(struct student)

  5. struct student
  6. {
  7.         char num[6], name[20], sex[3];//定义学号、姓名、性别
  8.         float math, english, politics, chinese, sum;//定义数学、英语、**、语文、总分
  9.         struct student *next;
  10. };

  11. int n;//全局变量,用于记录每个班级学生人数

  12. void menu()
  13. {
  14.         system("cls");//清屏
  15.         printf("-------------------------------------------------------------------------------");
  16.         printf("\n                                 学生数据管理系统\n");
  17.         printf("\n                         [0]退出\n");
  18.         printf("\n                         [1]创建学生数据\n");
  19.         printf("\n                         [2]添加学生数据\n");
  20.         printf("\n                         [3]删除学生数据\n");
  21.         printf("\n                         [4]查询\n");
  22.         printf("-------------------------------------------------------------------------------");
  23.         
  24. }

  25. // 等待用户按回车后回到主菜单
  26. void to_menu()
  27. {
  28. char c1, c2;
  29. printf("\n\n\n按回车键返回主菜单...");
  30. scanf("%c%c",&c1,&c2);//第一个字符吸收上次的确认回车键
  31. menu();
  32. }

  33. //打印
  34. void list(struct student *head, char c_name[20])
  35. {
  36.         struct student *p;
  37.         p  = head;
  38.         system("cls");
  39.         printf("\n---------------------------[%s]学生成绩表----------------------------\n",c_name);
  40.         printf("\n\t学号\t姓名\t性别\t数学\t英语\t**\t语文\t总分\n");
  41.         while(p != NULL)
  42.         {
  43.                 printf("\n\t%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",p->num,p->name,p->sex,p->math,p->english,p->politics,p->chinese,p->sum);
  44.                 p = p->next;
  45.         }
  46.         printf("\n班级共%d名学生!",n);
  47. }

  48. //保存
  49. void save_data(struct student *head, char c_name[20])
  50. {
  51.         FILE *fp;
  52.         struct student *p;
  53.         p = head;
  54.         if((fp = fopen(c_name,"wb")) == NULL)
  55.         {
  56.                 printf("\n不能保存指定文件!");
  57.                 exit(0);
  58.         }
  59.         while(p->next != NULL)//当next等于NULL时表明已经是最后一块数据,退出
  60.         {
  61.                 fwrite(p, sizeof(struct student), 1, fp);
  62.                 p = p->next;
  63.         }
  64.         fclose(fp);
  65.         list(head, c_name);
  66. }

  67. //载入
  68. void load_data(char c_name[20])
  69. {
  70.         FILE *fp;
  71.         struct student *p, *last, *head;//p工作指针、last最后一项指针、head头指针
  72.         head = NULL;
  73.         head = (struct student *)malloc(LEN);
  74.         last = head;
  75.         if((fp = fopen(c_name,"rb")) == NULL)
  76.         {
  77.                 printf("\n不能打开指定文件!");
  78.                 exit(0);
  79.         }
  80.         while(!feof(fp))
  81.         {
  82.                 p = (struct student *)malloc(LEN);
  83.                 if((fread(p, sizeof(struct student), 1, fp)) == 1)
  84.                 {
  85.                         last->next = p;
  86.                         last = p;
  87.                 }
  88.         }
  89.         fclose(fp);
  90.         last->next = NULL;//表示文件结束
  91.         /*return head = head->next;*///临时
  92. }
  93. void add_data()
  94. {
  95.         FILE *fp;
  96.         struct student *p1,*p2,*head;
  97.         char c_name[20];//存放班级名
  98.         char ch1, ch2;//判定是否继续输入学生信息

  99.         n = 0;

  100.         printf("\n请输入班级名:");
  101.         scanf("%s",c_name);
  102.         fp = fopen(c_name,"wb");//创建班级数据文件
  103.         //head = NULL;
  104.         while(ch2 != '0')
  105.         {        
  106.                 n++;
  107.                 if(n == 1)
  108.                 {
  109.                         p1 =  (struct student *)malloc(LEN);
  110.                         printf("\n学号:");
  111.                         scanf("%s",p1->num);
  112.                         printf("\n姓名:");
  113.                         scanf("%s",p1->name);
  114.                         printf("\n性别:");
  115.                         scanf("%s",p1->sex);
  116.                         printf("\n数学成绩:");
  117.                         scanf("%f",&p1->math);
  118.                         printf("\n英语成绩:");
  119.                         scanf("%f",&p1->english);
  120.                         printf("\n**:");
  121.                         scanf("%f",&p1->politics);
  122.                         printf("\n语文成绩:");
  123.                         scanf("%f",&p1->chinese);
  124.                         p1->sum = p1->math + p1->english + p1->politics + p1->chinese;
  125.                         printf("\n学号\t姓名\t性别\t数学\t英语\t**\t语文\t总分\n");
  126.                         printf("------------------------------------------------------------\n");
  127.                         printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",p1->num, p1->name, p1->sex, p1->math, p1->english, p1->politics, p1->chinese, p1->sum);
  128.                         head = p1;
  129.                         p1->next = NULL;
  130.                         LOOP1: printf("\n按0结束,回车继续输入!\n");
  131.                         printf("\n请输入:");
  132.                         scanf("%c%c",&ch1,&ch2);
  133.                         if( ch2 != '0' && ch2 != '\n')//判断如果输入的字符不为0或回车,报错并从新输入
  134.                         {
  135.                                 printf("\n输入错误!请从新输入!");
  136.                                 goto LOOP1;
  137.                         }
  138.                 }
  139.                 else
  140.                 {
  141.                         p2 = p1;
  142.                         p1 = (struct student *)malloc(LEN);
  143.                         printf("\n学号:");
  144.                         scanf("%s",p1->num);
  145.                         printf("\n姓名:");
  146.                         scanf("%s",p1->name);
  147.                         printf("\n性别:");
  148.                         scanf("%s",p1->sex);
  149.                         printf("\n数学成绩:");
  150.                         scanf("%f",&p1->math);
  151.                         printf("\n英语成绩:");
  152.                         scanf("%f",&p1->english);
  153.                         printf("\n**:");
  154.                         scanf("%f",&p1->politics);
  155.                         printf("\n语文成绩:");
  156.                         scanf("%f",&p1->chinese);
  157.                         p1->sum = p1->math + p1->english + p1->politics + p1->chinese;
  158.                         printf("\n学号\t姓名\t性别\t数学\t英语\t**\t语文\t总分\n");
  159.                         printf("------------------------------------------------------------\n");
  160.                         printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",p1->num, p1->name, p1->sex, p1->math, p1->english, p1->politics, p1->chinese, p1->sum);
  161.                         p2->next = p1;
  162.                         LOOP2: printf("\n按0结束,回车继续输入!\n");
  163.                         printf("\n请输入:");
  164.                         scanf("%c%c",&ch1,&ch2);
  165.                         if( ch2 != '0' && ch2 != '\n')//判断如果输入的字符不为0或回车,报错并从新输入
  166.                         {
  167.                                 printf("\n输入错误!请从新输入!");
  168.                                 goto LOOP2;
  169.                         }
  170.                 }
  171.         }
  172.         p1->next= NULL;
  173.         fclose(fp);
  174.         save_data(head,c_name);
  175.         return head;
  176. }

  177. void main()
  178. {
  179.         struct student *p;
  180.         char fun;
  181.         menu();

  182.         while(1)
  183.         {
  184.                 printf("请输入功能号[0-4]:");
  185.                 scanf("%c",&fun);
  186.                 switch(fun)
  187.                 {
  188.                         case '0': break;
  189.                         case '1':
  190.                         {
  191.                                 p = add_data();
  192.                                 to_menu();
  193.                         }
  194.                        
  195.                 }
  196.                 if(fun == '0')
  197.                 {
  198.                         break;
  199.                 }
  200.         }
  201. }
复制代码
184行为什么不能返回return的值呢?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-10 08:45:50 | 显示全部楼层
void add_data()
怎么能返回呢
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-10 09:53:37 | 显示全部楼层
那怎样才能返回呢
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-10 10:07:35 | 显示全部楼层
既然你返回的是一个指向结构的指针,那就struct student *add_data();
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-10 10:17:50 | 显示全部楼层
谢谢 呵呵呵
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-7 16:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表