鱼C论坛

 找回密码
 立即注册
查看: 2736|回复: 5

[技术交流] [作业提交]甲鱼的结构体作业

[复制链接]
发表于 2013-8-17 17:04:16 | 显示全部楼层 |阅读模式

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

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

x
折腾了半天 没心思写注释了 代码很烂但貌似可以用 希望高手勿喷啊 ~~~~(>_<)~~~~ 个人技术放在这了~~~~(>_<)~~~~

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<string.h>
  4. #define LEN sizeof(struct student)
  5. struct student *create();//创建链表
  6. struct student *del(struct student *head,int num);//删除链表中数字 (链表头指针,要删除的学号)
  7. struct student *insert(struct student *head,struct student *head_2);//网链表中加元素(链表头指针,要加的那个的)
  8. void print(struct student *head);//输出链表
  9. struct student *clear(struct student *head);//清空链表
  10. void change(struct student *head,int num,float score);//更改链表数据

  11. struct student
  12. {
  13.         long int num;
  14.         float score;
  15.         struct student *next;
  16. };
  17. int n;
  18. char ch;
  19. int main()
  20. {
  21.         struct student *head,head_2;
  22.         int num,i;
  23.         float score;
  24.         printf("请先建立一个链表!(要结束请在number处输入0)\n");
  25.         head = create();
  26. s:        setbuf(stdin,NULL);
  27.         printf("---------------------------------------------------------\n");
  28.         printf("请选择要的操作:\n");
  29.         printf("1.打印记录     ");
  30.         printf("2.插入记录     ");
  31.         printf("3.删除记录     \n");
  32.         printf("4.更改记录     ");
  33.         printf("5.清除记录     ");
  34.         printf("6.退出程序     \n");
  35.         printf("---------------------------------------------------------\n");
  36.         scanf("%d",&i);
  37.         switch(i)//根据用户输入的不同数字来执行不同的代码
  38.         {
  39.         case 1:
  40.                 print(head);
  41.                 goto s;
  42.                 break;
  43.         case 2:
  44. s4:            setbuf(stdin,NULL);
  45.                 printf("what number are you want to insert:");
  46.                 scanf("%d",&head_2.num);
  47.                         while((ch=getchar())!='\n')
  48.                 {
  49.                         if((ch>=48&&ch<=57)||ch==46)
  50.                         {
  51.                                 ;
  52.                         }
  53.                         else
  54.                         {
  55.                                 printf("你的输入有误!\n");
  56.                                 goto s4;
  57.                         }
  58.                 }
  59. s5:                setbuf(stdin,NULL);
  60.                 printf("what score are you want to insert:");
  61.                 scanf("%f",&head_2.score );
  62.                         while((ch=getchar())!='\n')
  63.                 {
  64.                         if((ch>=48&&ch<=57)||ch==46)
  65.                         {
  66.                                 ;
  67.                         }
  68.                         else
  69.                         {
  70.                                 printf("你的输入有误!\n");
  71.                                 goto s5;
  72.                         }
  73.                 }
  74.                 head = insert(head,&head_2);
  75.                 goto s;
  76.                 break;
  77.         case 3:
  78. s6:         setbuf(stdin,NULL);
  79.                 printf("please putinto the number you want to delete:");
  80.                 scanf("%ld",&num);
  81.                         while((ch=getchar())!='\n')
  82.                 {
  83.                         if((ch>=48&&ch<=57)||ch==46)
  84.                         {
  85.                                 ;
  86.                         }
  87.                         else
  88.                         {
  89.                                 printf("你的输入有误!\n");
  90.                                 goto s6;
  91.                         }
  92.                 }
  93.                 head = del(head,num);
  94.                 goto s;
  95.                 break;
  96.         case 4:
  97. s7:                setbuf(stdin,NULL);
  98.                 printf("Please putinto the number you want to change:");
  99.                 scanf("%d",&num);
  100.                         while((ch=getchar())!='\n')
  101.                 {
  102.                         if((ch>=48&&ch<=57)||ch==46)
  103.                         {
  104.                                 ;
  105.                         }
  106.                         else
  107.                         {
  108.                                 printf("你的输入有误!\n");
  109.                                 goto s7;
  110.                         }
  111.                 }
  112. s8:                setbuf(stdin,NULL);
  113.                 printf("Please putinto the score you want to save:");
  114.                 scanf("%f",&score);
  115.                         while((ch=getchar())!='\n')
  116.                 {
  117.                         if((ch>=48&&ch<=57)||ch==46)
  118.                         {
  119.                                 ;
  120.                         }
  121.                         else
  122.                         {
  123.                                 printf("你的输入有误!\n");
  124.                                 goto s8;
  125.                         }
  126.                 }
  127.                 change(head,num,score);
  128.                 break;
  129.         case 5:
  130.                 head=clear(head);
  131.                 break;
  132.         case 6:
  133.                 return 0;
  134.                 break;
  135.         default:
  136.                 printf("对不起,你的输入有误,请重新输入!");
  137.                 goto s;
  138.                 break;
  139.         }
  140.        
  141.         goto s;
  142. }
  143. struct student *create()
  144. {
  145.         struct student *head;
  146.         struct student *p1,*p2;
  147. s:        setbuf(stdin,NULL);
  148.         head = NULL;
  149.         p1 = p2 = (struct student *)malloc(LEN);
  150.         printf("Please purinto student's munber:");
  151.         scanf("%ld",&p1->num );
  152.                 while((ch=getchar())!='\n')
  153.                 {
  154.                         if((ch>=48&&ch<=57)||ch==46)
  155.                         {
  156.                                 ;
  157.                         }
  158.                         else
  159.                         {
  160.                                 printf("你的输入有误!\n");
  161.                                 goto s;
  162.                         }
  163.                 }
  164. s1:        printf("Please putinto student's score:");
  165.         setbuf(stdin,NULL);
  166.         scanf("%f",&p1->score );
  167.         while((ch=getchar())!='\n')
  168.                 {
  169.                         if((ch>=48&&ch<=57)||ch==46)
  170.                         {
  171.                                 ;
  172.                         }
  173.                         else
  174.                         {
  175.                                 printf("你的输入有误!\n");
  176.                                 goto s1;
  177.                         }
  178.                 }
  179.         n = 0;
  180.         while(p1->num )
  181.         {
  182.         n++;
  183.         if(n==1)
  184.         {
  185.                 head = p1;
  186.         }
  187.         else
  188.         {
  189.                 p2->next =p1;
  190.         }
  191.         p2 = p1;
  192.         p1 = (struct student *)malloc(LEN);
  193. s2:        setbuf(stdin,NULL);
  194.         printf("Please purinto student's munber:");
  195.         scanf("%ld",&p1->num );
  196.         while((ch=getchar())!='\n')
  197.                 {
  198.                         if((ch>=48&&ch<=57)||ch==46)
  199.                         {
  200.                                 ;
  201.                         }
  202.                         else
  203.                         {
  204.                                 printf("你的输入有误!\n");
  205.                                 goto s2;
  206.                         }
  207.                 }
  208.         if(p1->num ==0)
  209.         {
  210.                 goto e;
  211.         }
  212. s3:        setbuf(stdin,NULL);
  213.         printf("Please putinto student's score:");
  214.         scanf("%f",&p1->score );
  215.                 while((ch=getchar())!='\n')
  216.                 {
  217.                         if((ch>=48&&ch<=57)||ch==46)
  218.                         {
  219.                                 ;
  220.                         }
  221.                         else
  222.                         {
  223.                                 printf("你的输入有误!\n");
  224.                                 goto s3;
  225.                         }
  226.                 }
  227.         }
  228. e:        p2->next =NULL;
  229.         return head;
  230. }

  231. void print(struct student *head)
  232. {
  233.         struct student *p;
  234.         p = head;
  235.         printf("共有%d条记录:\n",n);
  236.         if(head)
  237.         {
  238.                 do
  239.                 {
  240.                         printf("%ld号学生的成绩是:%f\n",p->num,p->score);
  241.                         p = p->next ;
  242.                 }while(p);
  243.         }
  244. }

  245. struct student *del(struct student *head,int num)
  246. {
  247.         struct student *p1,*p2;
  248.         if(NULL == head)
  249.         {
  250.                 printf("The List is NULL!\n");
  251.                 goto END;
  252.         }
  253.        
  254.        
  255.         p1 = head;
  256.         while(p1->num !=num&&p1->next !=NULL)
  257.         {
  258.                 p2 = p1;
  259.                 p1 = p1 ->next ;
  260.         }
  261.        
  262.         if(num == p1->num )
  263.         {
  264.                 if(p1 == head)
  265.                 {
  266.                         head = p1 ->next ;
  267.                         printf("Delete NO.%d succeed!\n",num);
  268.                         n--;
  269.                 }
  270.                 else
  271.                 {
  272.                         p2->next =p1->next ;
  273.                         printf("Delete NO.%d succeed!\n",num);
  274.                         n--;
  275.                 }
  276.         }
  277.         else
  278.         {
  279.                 printf("NO.%d not been found!\n",num);
  280.         }
  281. END:   
  282.         return head;
  283. }

  284. struct student *insert(struct student *head,struct student *head_2)
  285. {
  286.         struct student *p0,*p1,*p2;
  287.         p0 = head_2;
  288.         p1 = head;
  289.         if(p1==NULL)
  290.         {
  291.                 head = p0;
  292.                 p0->next =NULL;
  293.         }
  294.         else
  295.         {
  296.                 while(p0->num >p1->num && p1->next !=NULL)
  297.                 {
  298.                         p2 = p1;
  299.                         p1 = p1 ->next ;
  300.                 }
  301.                 if(p0->num <= p1->num)
  302.                 {
  303.                         if(p1 == head)
  304.                         {
  305.                                 head = p0;
  306.                         }
  307.                         else
  308.                         {
  309.                                 p2->next =p0;
  310.                         }
  311.                         p0->next =p1;
  312.                 }
  313.                 else
  314.                 {
  315.                         p1->next =p0;
  316.                         p0->next =NULL;
  317.                 }
  318.         }
  319.         n++;
  320. e:        return head;
  321. }

  322. struct student *clear(struct student *head)
  323. {
  324.         head = NULL;
  325.         n=0;
  326.         printf("以清除全部记录!\n");
  327.         return head;
  328. }

  329. void change(struct student *head,int num,float score)
  330. {
  331.         struct student *p1,*p2;
  332.         if(head == NULL)
  333.         {
  334.                 printf("This is a NULL!\n");
  335.                 goto end_2;
  336.         }
  337.         p1 = head;
  338.         while(p1->num !=num&&p1->next !=NULL)
  339.         {
  340.                 p2 = p1;
  341.                 p1=p1->next ;
  342.         }
  343.         if(p1->num ==num)
  344.         {
  345.                 p1->score = score;
  346.         }
  347.         else
  348.         {
  349.                 printf("NO.%d not been found!",num);
  350.         }
  351. end_2:
  352.         printf("");
  353. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-8-17 17:04:51 | 显示全部楼层
我自己都有点看不下去了 ~~~~(>_<)~~~~  可我没其他办法啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-17 17:34:21 | 显示全部楼层
不能实现保存到文件里吗?
这样好像每次都得重新输入的样子。
{:7_172:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-17 17:35:39 | 显示全部楼层

现在这不是还没没看到文件操作 嘛  刚刚看到结构体
真的不会啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-17 17:39:26 | 显示全部楼层
加油。。。好长阿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-17 17:53:53 | 显示全部楼层
牡丹花下死做鬼 发表于 2013-8-17 17:35
现在这不是还没没看到文件操作 嘛  刚刚看到结构体
真的不会啊

{:7_158:}好吧。我搞混了 。我记得以后文件操作也有个类似的作业。
不过那个功能就局限了。在文件中删除记录什么的据说要重写文件才能实现。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-15 05:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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