亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 3004 | 回復(fù): 8
打印 上一主題 下一主題

[C] 請(qǐng)問(wèn)這個(gè)文件讀取代碼如何實(shí)現(xiàn),使用sortbubble 排序,求高手指教 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2014-12-05 22:45 |只看該作者 |倒序?yàn)g覽
本帖最后由 caowenqq17 于 2014-12-07 23:54 編輯

如題如題如題如題如題

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
2 [報(bào)告]
發(fā)表于 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. }
復(fù)制代碼

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2014-12-06 13:08 |只看該作者
非常感謝!!回復(fù) 2# cobras


   

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2014-12-06 23:48 |只看該作者
你好請(qǐng)問(wèn)如果使用字母從a-z的順序排序,這個(gè)怎么實(shí)現(xiàn)?謝謝了回復(fù) 2# cobras


   

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [報(bào)告]
發(fā)表于 2014-12-07 00:20 |只看該作者
那得問(wèn)個(gè)問(wèn)題?不同長(zhǎng)度字符串如何比較?是編碼優(yōu)先還是長(zhǎng)度優(yōu)先?
直接將這個(gè)比較函數(shù)替換sortItems中的if條件就可以了。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2014-12-07 12:24 |只看該作者
非常謝謝你的熱心幫助回復(fù) 5# cobras


   

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2014-12-08 09:48 |只看該作者
我嘗試了很多遍,還是不行啊,如果有空得話,能夠幫我看看嗎,謝謝了,就是實(shí)現(xiàn)按人名的那種首字母的排序,例如這樣
amazon
chang jiang
huang he
Yang Zi river


回復(fù) 5# cobras


   

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
8 [報(bào)告]
發(fā)表于 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. }
復(fù)制代碼

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2014-12-09 10:51 |只看該作者
辛苦了,十分謝謝!回復(fù) 8# cobras


   
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP