冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 1399|回覆: 3

[求助] C作業

[複製鏈接]

7

主題

0

好友

221

積分

迷你贊助會員

Rank: 3Rank: 3

UID
161084
帖子
141
主題
7
精華
0
積分
221
楓幣
2887
威望
173
存款
0
贊助金額
150
推廣
0
GP
12
閱讀權限
30
在線時間
203 小時
註冊時間
2016-9-4
最後登入
2024-2-7

2021年紀念勳章 2021中秋節紀念勳章 2022年紀念勳章 懶人勳章 性別(男) 音樂勳章 神手勳章 太陽勳章 論壇粉絲 VIP會員 積分勳章

發表於 2021-9-22 09:34:48 |顯示全部樓層
本帖最後由 呂晨 於 2021-9-22 09:35 編輯

要讀txt檔

圖二

圖二

利用動態結構,由大到小排序,並且可以輸入A?,輸出值後面的值

圖三

圖三

這是我現在打的,但不知道該如何一個一個地把資料放進結構裡面,或者是一次把所有資料讀完放在一個矩陣裡再丟到結構裡面,檔案最大有到130000行

圖一

圖一

想詢問,我打的程式思路有沒有錯,並且該怎麼把資料放進結構裡
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #pragma warning( disable : 4996 )
  5. #define N 13

  6. struct test
  7. {
  8.         int name;
  9.         int x;
  10. } *matrix;

  11. int main()
  12. {
  13.         int i,j=1,line=0;
  14.         char name[N];
  15.         char value[N];
  16.         printf("enter the file name:(.txt)");
  17.         scanf("%s", &name);
  18.         FILE* fp=fopen(name,"r");
  19.         if (fp == NULL)
  20.         {
  21.                 puts("error");
  22.         }
  23.         while((i=fgetc(fp))!=EOF)
  24.         {
  25.                 if(i=='\n') line++;
  26.         }
  27.         fseek(fp,0L,SEEK_SET);
  28.         matrix=(struct test*)malloc(line*sizeof(struct test));
  29.         
  30.         while(fgets(value,sizeof(value),fp)!=NULL)
  31.         {

  32. //printf("%s",value);
  33.                 struct test A[j]=value;  (到這裡程式就無法執行了)
  34.         
  35.         }
  36. }
複製代碼
已有 1 人評分楓幣 威望 GP 收起 理由
冰楓 -10 -1 -1 求助文未分類

總評分: 楓幣 -10  威望 -1  GP -1   查看全部評分

複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$

88

主題

0

好友

281

積分

迷你贊助會員

Rank: 3Rank: 3

UID
92572
帖子
743
主題
88
精華
0
積分
281
楓幣
664
威望
224
存款
0
贊助金額
150
推廣
0
GP
140
閱讀權限
30
在線時間
390 小時
註冊時間
2015-2-18
最後登入
2024-2-26

2017端午節紀念勳章 發帖達人 懶人勳章 私服達人 神手勳章 Android勳章 2018萬聖節紀念勳章 性別(男) 性別(女) 幼兒勳章 太陽勳章 音樂勳章 論壇粉絲 VIP會員 積分勳章 2019中秋節紀念勳章 解說達人 2020年紀念勳章 2020中秋節紀念勳章 2020聖誕節紀念勳章 聖誕節紀念勳章 聖誕節紀念勳章2 論壇支持王

發表於 2021-9-22 17:06:37 |顯示全部樓層
你的思路沒有問題
我直接幫你寫完你試試看
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #pragma warning( disable : 4996 )
  5. #define N 13

  6. struct test
  7. {
  8.         char* name;
  9.         int x;

  10.         test() {};        // 建構子

  11.         void print() {
  12.                 printf("%s %d\n", name, x);
  13.         }
  14. };

  15. int binarySearch(test* matrix, int l, int r, int x)
  16. {
  17.         if (r >= l) {
  18.                 int mid = l + (r - l) / 2;
  19.                 if (matrix[mid].x == x)
  20.                         return mid;
  21.                 if (matrix[mid].x > x)
  22.                         return binarySearch(matrix, l, mid - 1, x);
  23.                 return binarySearch(matrix, mid + 1, r, x);
  24.         }
  25.         return -1;
  26. }

  27. void bubbleSort(test* matrix, int line) {
  28.         struct test temp;
  29.         int i, j;
  30.         for (i = 1; i < line; i++) {
  31.                 for (j = 0; j < line - i; j++) {
  32.                         if (matrix[j].x > matrix[j + 1].x) {
  33.                                 temp = matrix[j];
  34.                                 matrix[j] = matrix[j + 1];
  35.                                 matrix[j + 1] = temp;
  36.                         }
  37.                 }
  38.         }
  39. }

  40. int main()
  41. {
  42.         int i, j = 1, line = 0, tmp;
  43.         char name[N];
  44.         char value[N];
  45.         printf("enter the file name:(.txt)");
  46.         scanf("%s", &name);
  47.         FILE* fp = fopen(name, "r");
  48.         if (fp == NULL)
  49.         {
  50.                 puts("error");
  51.         }
  52.         while ((i = fgetc(fp)) != EOF)
  53.         {
  54.                 if (i == '\n') line++;
  55.         }
  56.         fseek(fp, 0L, SEEK_SET);

  57.         test* matrix = (test*)malloc(line * sizeof(test));
  58.         i = 0;        // 記錄第幾筆資料
  59.         while (fgets(value, sizeof(value), fp) != NULL)
  60.         {
  61.                 char* token = strtok(value, " ");        // 取得指向 A? 的指標
  62.                 matrix[i].name = (char*)malloc(sizeof(char) * N);
  63.                 strcpy(matrix[i].name, token);                // 賦值

  64.                 token = strtok(NULL, "\n");                        // 取得指向 數值 的指標
  65.                 tmp = atoi(token);                                        // 字串轉數字
  66.                 matrix[i].x = tmp;
  67.                 matrix[i].print();                                        // 印出結構

  68.                 i++;
  69.         }

  70.         // Bubble Sort
  71.         bubbleSort(matrix, line);

  72.         // Binary Search
  73.         int toFind;
  74.         for (i=0;i<line;i++)
  75.         {
  76.                 toFind = binarySearch(matrix, 0, line, matrix[i].x);

  77.                 printf("你要找的東西: ");
  78.                 matrix[toFind].print();
  79.         }

  80.         free(matrix);        // 釋放資源(指標類的東西必須要手動釋放!)
  81. }
複製代碼
[發帖際遇]: love6610716 不幸喝到「英狗狼」的毒茶飲,因而獲得健康賠償 1 楓幣 幸運榜 / 衰神榜
心情甚麼的...新年也孤單
點評回覆

使用道具 舉報

7

主題

0

好友

221

積分

迷你贊助會員

Rank: 3Rank: 3

UID
161084
帖子
141
主題
7
精華
0
積分
221
楓幣
2887
威望
173
存款
0
贊助金額
150
推廣
0
GP
12
閱讀權限
30
在線時間
203 小時
註冊時間
2016-9-4
最後登入
2024-2-7

2021年紀念勳章 2021中秋節紀念勳章 2022年紀念勳章 懶人勳章 性別(男) 音樂勳章 神手勳章 太陽勳章 論壇粉絲 VIP會員 積分勳章

發表於 2021-9-23 10:14:59 |顯示全部樓層
love6610716 發表於 2021-9-22 17:06
你的思路沒有問題
我直接幫你寫完你試試看

我是用visual studio來跑,會跑出這些錯誤
1.PNG
2.PNG

還是visual studio 寫C 有其它寫法?
點評回覆

使用道具 舉報

88

主題

0

好友

281

積分

迷你贊助會員

Rank: 3Rank: 3

UID
92572
帖子
743
主題
88
精華
0
積分
281
楓幣
664
威望
224
存款
0
贊助金額
150
推廣
0
GP
140
閱讀權限
30
在線時間
390 小時
註冊時間
2015-2-18
最後登入
2024-2-26

2017端午節紀念勳章 發帖達人 懶人勳章 私服達人 神手勳章 Android勳章 2018萬聖節紀念勳章 性別(男) 性別(女) 幼兒勳章 太陽勳章 音樂勳章 論壇粉絲 VIP會員 積分勳章 2019中秋節紀念勳章 解說達人 2020年紀念勳章 2020中秋節紀念勳章 2020聖誕節紀念勳章 聖誕節紀念勳章 聖誕節紀念勳章2 論壇支持王

發表於 2021-9-23 12:33:08 |顯示全部樓層
呂晨 發表於 2021-9-23 10:14
我是用visual studio來跑,會跑出這些錯誤

還是visual studio 寫C 有其它寫法?

抱歉我寫成C++的語法了
以下是C的
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #pragma warning( disable : 4996 )
  5. #define N 13

  6. struct test
  7. {
  8.         char* name;
  9.         int x;
  10. };

  11. void print(struct test _t) { printf("%s %d\n", _t.name, _t.x); }

  12. int binarySearch(struct test* matrix, int l, int r, int x)
  13. {
  14.         if (r >= l) {
  15.                 int mid = l + (r - l) / 2;
  16.                 if (matrix[mid].x == x)
  17.                         return mid;
  18.                 if (matrix[mid].x > x)
  19.                         return binarySearch(matrix, l, mid - 1, x);
  20.                 return binarySearch(matrix, mid + 1, r, x);
  21.         }
  22.         return -1;
  23. }

  24. void bubbleSort(struct test* matrix, int line) {
  25.         struct test temp;
  26.         int i, j;
  27.         for (i = 1; i < line; i++) {
  28.                 for (j = 0; j < line - i; j++) {
  29.                         if (matrix[j].x > matrix[j + 1].x) {
  30.                                 temp = matrix[j];
  31.                                 matrix[j] = matrix[j + 1];
  32.                                 matrix[j + 1] = temp;
  33.                         }
  34.                 }
  35.         }
  36. }

  37. int main()
  38. {
  39.         int i, j = 1, line = 0, tmp;
  40.         char name[N];
  41.         char value[N];
  42.         printf("enter the file name:(.txt)");
  43.         scanf("%s", &name);
  44.         FILE* fp = fopen(name, "r");
  45.         if (fp == NULL)
  46.         {
  47.                 puts("error");
  48.         }
  49.         while ((i = fgetc(fp)) != EOF)
  50.         {
  51.                 if (i == '\n') line++;
  52.         }
  53.         fseek(fp, 0L, SEEK_SET);

  54.         struct test* matrix = (struct test*)malloc(line * sizeof(struct test));
  55.         i = 0;        // 記錄第幾筆資料
  56.         while (fgets(value, sizeof(value), fp) != NULL)
  57.         {
  58.                 char* token = strtok(value, " ");                                        // 取得指向 A? 的指標
  59.                 matrix[i].name = (char*)malloc(sizeof(char) * N);
  60.                 strcpy(matrix[i].name, token);                                                // 賦值

  61.                 token = strtok(NULL, "\n");                                                        // 取得指向 數值 的指標
  62.                 tmp = atoi(token);                                  // 字串轉數字
  63.                 matrix[i].x = tmp;
  64.                 print(matrix[i]);                                   // 印出結構

  65.                 i++;
  66.         }

  67.         // Bubble Sort
  68.         bubbleSort(matrix, line);

  69.         // Binary Search
  70.         int toFind;
  71.         for (i = 0; i < line; i++)
  72.         {
  73.                 toFind = binarySearch(matrix, 0, line, matrix[i].x);

  74.                 printf("你要找的東西: ");
  75.                 print(matrix[toFind]);
  76.         }

  77.         free(matrix);        // 釋放資源(指標類的東西必須要手動釋放!)
  78. }
複製代碼
心情甚麼的...新年也孤單
點評回覆

使用道具 舉報

高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-3-29 14:38

回頂部