鱼C论坛

 找回密码
 立即注册
查看: 72|回复: 1

C语言 商品单链表

[复制链接]
发表于 2024-5-7 22:46:49 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>

  5. struct Goods
  6. {
  7.     char name[20];
  8.     int num;
  9.     float price;
  10.     struct Goods *next;
  11. };

  12. void getInput(struct Goods *goods)
  13. {
  14.     printf("请输入商品名称:");
  15.     scanf("%s", goods -> name);
  16.     do
  17.     {
  18.         printf("请输入商品数量(数量小于100):");
  19.         scanf("%d", &goods -> num);
  20.     }while(goods -> num > 100);
  21.    
  22.     do
  23.     {
  24.         printf("请输入商品价格(价格小于1000):");
  25.         scanf("%f", &goods -> price);
  26.     }while(goods -> price > 1000);
  27. }

  28. void addGoods(struct Goods **head)
  29. {
  30.     struct Goods *goods, *temp;
  31.    
  32.     goods = (struct Goods *)malloc(sizeof(struct Goods));//分配内存
  33.    
  34.     assert(goods);
  35.    
  36.     getInput(goods);
  37.    
  38.     if(*head != NULL)//尾插
  39.     {
  40.         temp = *head;
  41.         while(temp -> next != NULL)
  42.                 {
  43.                         temp = temp -> next;
  44.                 }

  45.         temp -> next = goods;
  46.         goods -> next = NULL;
  47.     }
  48.     else
  49.     {
  50.         *head = goods;
  51.         goods -> next = NULL;
  52.     }
  53. }

  54. void print_Goods(struct Goods *head)//打印链表
  55. {
  56.         struct Goods *goods;

  57.         goods = head;
  58.         while(goods != NULL)
  59.         {
  60.                 printf("商品名称:%s, 商品数量:%d, 商品价格:%0.2f", goods -> name, goods -> num, goods -> price);
  61.                 goods = goods -> next;
  62.                 putchar('\n');
  63.         }
  64. }

  65. void sum_Price(struct Goods *head)
  66. {
  67.         struct Goods *goods;
  68.        
  69.         goods = head;
  70.         while(goods != NULL)
  71.         {
  72.                 printf("商品名称:%s, 商品总价:%0.2f", goods -> name, (goods -> num) * (goods -> price));
  73.                 goods = goods -> next;
  74.                 putchar('\n');
  75.         }
  76. }

  77. void menu()
  78. {
  79.     putchar('\n');
  80.     printf("|== 欢迎来到商品菜单 ==|\n");
  81.     printf("| 1.显示所有商品       |\n");
  82.     printf("| 2.数量最多的商品名称 |\n");
  83.     printf("| 3.单价最高的商品名称 |\n");
  84.     printf("| 4.商品总价           |\n");
  85.     printf("| 5.退出系统           |\n");
  86.     printf("|== author:chenxing == |\n");
  87. }

  88. char *search_max_num(struct Goods *head, int max)
  89. {
  90.    
  91.     while(head != NULL)
  92.     {
  93.         if(head -> num == max)
  94.         {
  95.             return head -> name;
  96.         }
  97.         
  98.         head = head -> next;
  99.     }
  100.    
  101.     return NULL;
  102. }

  103. void num_Max_name(struct Goods *head)
  104. {
  105.         struct Goods *current;
  106.     int max = head -> num;

  107.     while(head != NULL)
  108.     {
  109.         if(max >= head -> num)
  110.         {
  111.             max = head -> num;
  112.         }
  113.                 head = head -> next;
  114.     }
  115.    
  116.     char *goods = search_max_num(current, max);
  117.    
  118.     if(goods == NULL)
  119.     {
  120.             printf("没有最大的价格!");
  121.     }
  122.     else
  123.     {
  124.             printf("数量最多的商品名称是:%s", goods);
  125.     }
  126. }

  127. char *search_max_price(struct Goods *head, float max)
  128. {
  129.         while(head != NULL)
  130.         {
  131.                 if(head -> price == max)
  132.         {
  133.             return head -> name;
  134.         }
  135.        
  136.                 head = head -> next;
  137.         }

  138.         return NULL;//没有匹配的价格
  139. }

  140. void price_Max_name(struct Goods *head)
  141. {
  142.     struct Goods *current;
  143.         float max = head -> price;
  144.        
  145.     current = head;
  146.         while(head != NULL)
  147.         {
  148.                 if(max < head -> price)
  149.                 {
  150.                         max = head -> price;
  151.                 }
  152.                 head = head -> next;
  153.         }
  154.        
  155.         char *goods = search_max_price(current, max);

  156.         if(goods == NULL)
  157.         {
  158.                 printf("没有最大的价格!");
  159.         }
  160.         else
  161.         {
  162.                 printf("售价最高的商品名称是:%s", goods);
  163.     }
  164. }

  165. int main(void)
  166. {
  167.     struct Goods *head = NULL;
  168.     struct Goods *goods;
  169.     int code;
  170.     char ch;
  171.     int count = 100;
  172.    
  173.     while(count--)
  174.     {
  175.         printf("请问是否需要录入商品信息(Y/N)?");
  176.         do
  177.         {
  178.             ch = getchar();
  179.         }while(ch != 'Y' && ch != 'N');
  180.         
  181.         if(ch == 'Y')
  182.         {
  183.             addGoods(&head);
  184.         }
  185.         else
  186.         {
  187.             goto FINDIT;
  188.         }
  189.     }
  190. FINDIT:
  191.     printf("商品信息录入完毕!\n");

  192.     menu();
  193.     while(1)
  194.     {
  195.         printf("\n请输入一个指令:");
  196.         scanf("%d", &code);
  197.         switch(code)
  198.         {
  199.             case 1:
  200.                 print_Goods(head);
  201.                 break;
  202.             case 2:
  203.                 num_Max_name(head);
  204.                 break;
  205.             case 3:
  206.                 price_Max_name(head);
  207.                 break;
  208.             case 4:
  209.                 printf("\n商品总价:\n");
  210.                 sum_Price(head);
  211.                 break;
  212.             case 5:
  213.                 exit(1);
  214.                 break;
  215.             default:
  216.                 printf("请输入1~5的数字!");
  217.                 break;
  218.         }
  219.     }

  220.     return 0;
  221. }
复制代码


@FishC,帮我看看case2: 段错误的原因?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-7 22:47:02 | 显示全部楼层
在你的代码中,段错误主要是因为你在函数`num_Max_name`中使用了一个未初始化的指针`current`。

在`num_Max_name`函数中,你声明了一个指针变量`current`,但是没有给它赋值。然后,在调用`search_max_num`函数时,你传递了这个未初始化的指针`current`作为参数,导致出现段错误。

为了解决这个问题,你可以将`current`的初始化语句放在`num_Max_name`函数的开头,像这样:

  1. void num_Max_name(struct Goods *head)
  2. {
  3.     struct Goods *current = head;
  4.     int max = head->num;

  5.     while(head != NULL)
  6.     {
  7.         if(max >= head->num)
  8.         {
  9.             max = head->num;
  10.         }
  11.         head = head->next;
  12.     }

  13.     char *goods = search_max_num(current, max);

  14.     if(goods == NULL)
  15.     {
  16.         printf("没有最大的数量!");
  17.     }
  18.     else
  19.     {
  20.         printf("数量最多的商品名称是:%s", goods);
  21.     }
  22. }
复制代码


这样,就能够正确地使用指针`current`了,避免出现段错误。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 22:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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