免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2909 | 回复: 8
打印 上一主题 下一主题

[C] 请问这个文件读取代码如何实现,使用sortbubble 排序,求高手指教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-05 22:45 |只看该作者 |倒序浏览
本帖最后由 caowenqq17 于 2014-12-07 23:54 编辑

如题如题如题如题如题

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2014-12-06 12:36 |只看该作者
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define TAX (0.13)

  4. struct Item
  5. {
  6.         struct Item *next;
  7.     char name[21];
  8.     int upc;
  9.     double price;
  10.     int isTaxed;
  11. };

  12. void prnTitles(void)
  13. {
  14.     printf("UPC  | Name               |   Price   |   Tax   |   Total\n"
  15.                 "-----+--------------------+-----------+---------+----------- \n");
  16. }

  17. void prnItemRow(struct Item *A)
  18. {
  19.         printf("%-5d|%-20s|%11.2lf|", A->upc, A->name, A->price);
  20.         if (A->isTaxed)
  21.                 printf("%9.2lf|%11.2lf\n", A->price * TAX, A->price * (1 + TAX));
  22.         else
  23.                 printf("%9.2lf|%11.2lf\n", 0.0, A->price);
  24. }

  25. int readItem(struct Item* Ip, FILE* fptr)
  26. {
  27.     return fscanf(fptr, "%d,%20[^,],%lf,%d", &Ip->upc, Ip->name, &Ip->price, &Ip->isTaxed)==4;
  28. }

  29. struct Item **insertItem(struct Item **io_list_last, struct Item *node)
  30. {
  31.         node->next = *io_list_last;
  32.         *io_list_last = node;
  33.         return &node->next;
  34. }

  35. struct Item *findItem(struct Item *list, int num)
  36. {
  37.         struct Item *node;

  38.         for (node = list; node != NULL; node = node->next) {
  39.                 if (node->upc == num) {
  40.                         return node;
  41.                 }
  42.         }
  43.         return NULL;
  44. }

  45. void swapPointer(void **p1, void **p2)
  46. {
  47.         void *tmp;

  48.         tmp = *p1;
  49.         *p1 = *p2;
  50.         *p2 = tmp;
  51. }

  52. struct Item **swapItem(struct Item **node_fore, struct Item **node_back)
  53. {
  54.         if ((*node_fore)->next == *node_back) {
  55.                 swapPointer(node_fore, node_back);
  56.                 swapPointer(&(*node_fore)->next, &(*node_back)->next);
  57.                 return &(*node_fore)->next;
  58.         }else {
  59.                 swapPointer(node_fore, node_back);
  60.                 swapPointer(&(*node_fore)->next, &(*node_back)->next);
  61.                 return node_back;
  62.         }
  63. }

  64. void sortItems(struct Item **plist)
  65. {
  66.         struct Item **pnext;

  67.         for (; *plist != NULL; plist = &(*plist)->next) {
  68.                 for (pnext = &(*plist)->next; *pnext != NULL; pnext = &(*pnext)->next) {
  69.                         if ((*plist)->price > (*pnext)->price) {
  70.                                 pnext = swapItem(plist, pnext);
  71.                         }
  72.                 }
  73.         }
  74. }

  75. int main(void)
  76. {
  77.         struct Item I;
  78.         FILE *fptr;
  79.     struct Item *node;
  80.         struct Item *list, **plast;
  81.         int num;

  82.         fptr = fopen("sample\\items.txt", "r");
  83.     if (fptr != NULL) {
  84.                 list = NULL;
  85.                 plast = &list;
  86.         while (readItem(&I, fptr)) {
  87.                         node = (struct Item *)malloc(sizeof(struct Item));
  88.                         if (node == NULL) {
  89.                                 printf("not enough memory\n");
  90.                                 break;
  91.                         }
  92.                         *node = I;
  93.                         plast = insertItem(plast, node);
  94.         }
  95.         fclose(fptr);
  96.                 sortItems(&list);
  97.                 prnTitles();
  98.                 for (node = list; node != NULL; node = node->next) {
  99.                         prnItemRow(node);
  100.                 }
  101.                 printf("please enter a UPC number: ");
  102.                 scanf("%d",&num);
  103.                 node = findItem(list, num);
  104.                 if (node != NULL) {
  105.                         prnItemRow(node);
  106.                 }else {
  107.                         printf("the number can not be found\n");
  108.                 }
  109.                 while (list != NULL) {
  110.                         node = list;
  111.                         list = list->next;
  112.                         free(node);
  113.                 }
  114.                 return 0;
  115.     }else {
  116.         printf("Could not open the file!\n");
  117.     }
  118.     return -1;
  119. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2014-12-06 13:08 |只看该作者
非常感谢!!回复 2# cobras


   

论坛徽章:
0
4 [报告]
发表于 2014-12-06 23:48 |只看该作者
你好请问如果使用字母从a-z的顺序排序,这个怎么实现?谢谢了回复 2# cobras


   

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [报告]
发表于 2014-12-07 00:20 |只看该作者
那得问个问题?不同长度字符串如何比较?是编码优先还是长度优先?
直接将这个比较函数替换sortItems中的if条件就可以了。

论坛徽章:
0
6 [报告]
发表于 2014-12-07 12:24 |只看该作者
非常谢谢你的热心帮助回复 5# cobras


   

论坛徽章:
0
7 [报告]
发表于 2014-12-08 09:48 |只看该作者
我尝试了很多遍,还是不行啊,如果有空得话,能够帮我看看吗,谢谢了,就是实现按人名的那种首字母的排序,例如这样
amazon
chang jiang
huang he
Yang Zi river


回复 5# cobras


   

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
8 [报告]
发表于 2014-12-08 22:57 |只看该作者
  1. /* sortbubble.c
  2. * an example of sorting linked table by bubble mechanism.
  3. */

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. #define TAX (0.13)

  8. struct Item
  9. {
  10.         struct Item *next;
  11.         char name[21];
  12.         int upc;
  13.         double price;
  14.         int isTaxed;
  15. };

  16. void prnTitles(void)
  17. {
  18.         printf("UPC  | Name               |   Price   |   Tax   |   Total\n"
  19.                 "-----+--------------------+-----------+---------+----------- \n");
  20. }

  21. void prnItemRow(struct Item *A)
  22. {
  23.         printf("%-5d|%-20s|%11.2lf|", A->upc, A->name, A->price);
  24.         if (A->isTaxed)
  25.                 printf("%9.2lf|%11.2lf\n", A->price * TAX, A->price * (1 + TAX));
  26.         else
  27.                 printf("%9.2lf|%11.2lf\n", 0.0, A->price);
  28. }

  29. int readItem(struct Item* Ip, FILE* fptr)
  30. {
  31.         return fscanf(fptr, "%d,%20[^,],%lf,%d", &Ip->upc, Ip->name, &Ip->price, &Ip->isTaxed)==4;
  32. }

  33. struct Item **insertItem(struct Item **io_list_last, struct Item *node)
  34. {
  35.         node->next = *io_list_last;
  36.         *io_list_last = node;
  37.         return &node->next;
  38. }

  39. struct Item *findItem(struct Item *list, int num)
  40. {
  41.         struct Item *node;
  42.        
  43.         for (node = list; node != NULL; node = node->next) {
  44.                 if (node->upc == num) {
  45.                         return node;
  46.                 }
  47.         }
  48.         return NULL;
  49. }

  50. void swapPointer(void **p1, void **p2)
  51. {
  52.         void *tmp;
  53.        
  54.         tmp = *p1;
  55.         *p1 = *p2;
  56.         *p2 = tmp;
  57. }

  58. struct Item **swapItem(struct Item **node_fore, struct Item **node_back)
  59. {
  60.         if ((*node_fore)->next == *node_back) {
  61.                 swapPointer(node_fore, node_back);
  62.                 swapPointer(&(*node_fore)->next, &(*node_back)->next);
  63.                 return &(*node_fore)->next;
  64.         }else {
  65.                 swapPointer(node_fore, node_back);
  66.                 swapPointer(&(*node_fore)->next, &(*node_back)->next);
  67.                 return node_back;
  68.         }
  69. }

  70. int compare_price(const struct Item *item1, const struct Item *item2)
  71. {
  72.         return item1->price > item2->price;
  73. }

  74. int compare_name(const struct Item *item1, const struct Item *item2)
  75. {
  76.         int len1, len2;
  77.         int len;

  78.         len1 = strlen(item1->name);
  79.         len2 = strlen(item2->name);
  80.         len = min(len1, len2);
  81.         switch (strncmp(item1->name, item2->name, len)) {
  82.         case 1:
  83.                 return 1;
  84.         case -1:
  85.                 return 0;
  86.         default:
  87.                 return len1 > len2;
  88.         }
  89. }

  90. void sortItems(struct Item **plist, int (*cmp_func)(const void *item1, const void *item2))
  91. {
  92.         struct Item **pnext;

  93.         for (; *plist != NULL; plist = &(*plist)->next) {
  94.                 for (pnext = &(*plist)->next; *pnext != NULL; pnext = &(*pnext)->next) {
  95.                         if ((*cmp_func)(*plist, *pnext)) {
  96.                                 pnext = swapItem(plist, pnext);
  97.                         }
  98.                 }
  99.         }
  100. }

  101. int main(void)
  102. {
  103.         struct Item I;
  104.         FILE *fptr;
  105.         struct Item *node;
  106.         struct Item *list, **plast;
  107.         int num;

  108.         list = NULL;
  109.         /* load table from file */
  110.         fptr = fopen("sample\\items.txt", "r");
  111.         if (fptr != NULL) {
  112.                 plast = &list;
  113.                 while (readItem(&I, fptr)) {
  114.                         node = (struct Item *)malloc(sizeof(struct Item));
  115.                         if (node == NULL) {
  116.                                 printf("not enough memory\n");
  117.                                 break;
  118.                         }
  119.                         *node = I;
  120.                         plast = insertItem(plast, node);
  121.                 }
  122.                 fclose(fptr);
  123.         }else {
  124.                 printf("Could not open the file!\n");
  125.         }
  126.         puts("sort table by price");
  127.         sortItems(&list, compare_price);
  128.         /* print table */
  129.         prnTitles();
  130.         for (node = list; node != NULL; node = node->next) {
  131.                 prnItemRow(node);
  132.         }
  133.         puts("sort table by name");
  134.         sortItems(&list, compare_name);
  135.         /* print table */
  136.         prnTitles();
  137.         for (node = list; node != NULL; node = node->next) {
  138.                 prnItemRow(node);
  139.         }
  140.         /* search table */
  141.         printf("please enter a UPC number: ");
  142.         scanf("%d",&num);
  143.         node = findItem(list, num);
  144.         if (node != NULL) {
  145.                 prnItemRow(node);
  146.         }else {
  147.                 printf("the number can not be found\n");
  148.         }
  149.         /* release table */
  150.         while (list != NULL) {
  151.                 node = list;
  152.                 list = list->next;
  153.                 free(node);
  154.         }
  155.         return 0;
  156. }
复制代码

论坛徽章:
0
9 [报告]
发表于 2014-12-09 10:51 |只看该作者
辛苦了,十分谢谢!回复 8# cobras


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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP