冰楓論壇

標題: C作業 [打印本頁]

作者: 呂晨    時間: 2021-9-22 09:34
標題: C作業
本帖最後由 呂晨 於 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. }
複製代碼

作者: love6610716    時間: 2021-9-22 17:06
你的思路沒有問題
我直接幫你寫完你試試看
  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. }
複製代碼

作者: 呂晨    時間: 2021-9-23 10:14
love6610716 發表於 2021-9-22 17:06
你的思路沒有問題
我直接幫你寫完你試試看

我是用visual studio來跑,會跑出這些錯誤
1.PNG 2.PNG
還是visual studio 寫C 有其它寫法?
作者: love6610716    時間: 2021-9-23 12:33
呂晨 發表於 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. }
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓