Re: 程設

看板Marginalman作者 ( )時間3年前 (2022/12/07 10:36), 3年前編輯推噓9(901)
留言10則, 9人參與, 3年前最新討論串7/12 (看更多)
我不會寫C 剛問AI AI回答我這樣 你看對不對: #include <stdio.h> #include <stdlib.h> #include <string.h> // 定義一個學生的結構 typedef struct student { char name[100]; int score; int id; } student; int main() { // 讀取檔案並建立串列 FILE* fp = fopen("text.txt", "r"); student* students = malloc(sizeof(student)); int num_students = 0; // 讀取每個學生的資料並儲存到串列中 while (fscanf(fp, "%s %d %d", students[num_students].name, &students[num_students].score, &students[num_students].id) == 3) { num_students++; students = realloc(students, (num_students+1) * sizeof(student)); } // 請使用者輸入排列順序 char order[4]; printf("請輸入排列順序(123或213):"); scanf("%s", order); // 依據使用者輸入的順序輸出學生資料 for (int i = 0; i < num_students; i++) { if (strcmp(order, "123") == 0) { printf("%s %d %d\n", students[i].name, students[i].score, students[i].id); } else if (strcmp(order, "213") == 0) { printf("%d %s %d\n", students[i].score, students[i].name, students[i].id); } else { printf("輸入錯誤!\n"); return 1; } } // 釋放動態記憶體 free(students); return 0; } ※ 引述《Sugarwithkis (sugarwithkis)》之銘言: : 作業有一題 : 說老師會給一個text.txt的檔案 : 裡面有亂數排列的學生名字成績學號 : 然後要我們程式可以input 各種順序的123 : 123分別代表名字 成績 學號 : 假設input 123 : 就output 每個人名字成績學號 一個人一行 : 如果是213 : 那就output 成績名字學號 一個人一行 : 聽說會用到指標 動態記憶體配置 串列 : 可是我還在想怎麼開檔案 : 漬鯊 : -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.200.162.222 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1670380566.A.3B0.html

12/07 10:37, 3年前 , 1F

12/07 10:38, 3年前 , 2F
那321怎麼辦 還有四種組合沒有加上去
12/07 10:38, 2F
可能我問的不好吧 要改一下問法 畢竟我不會寫C, 有基礎的人會知道怎麼問比較好

12/07 10:38, 3年前 , 3F
哪裡可以問
12/07 10:38, 3F

12/07 10:39, 3年前 , 4F
你用哪套AI阿 AlphaCode?
12/07 10:39, 4F

12/07 10:39, 3年前 , 5F
AI好屌…
12/07 10:39, 5F
※ 編輯: medama (1.200.162.222 臺灣), 12/07/2022 10:40:00

12/07 10:39, 3年前 , 6F
好屌
12/07 10:39, 6F

12/07 10:40, 3年前 , 7F
笑死 AI的正確用法
12/07 10:40, 7F

12/07 10:42, 3年前 , 8F
AI真猛
12/07 10:42, 8F

12/07 10:44, 3年前 , 9F
好屌
12/07 10:44, 9F

12/07 10:48, 3年前 , 10F
笑死
12/07 10:48, 10F
文章代碼(AID): #1ZZ_mMEm (Marginalman)
討論串 (同標題文章)
本文引述了以下文章的的內容:
0
6
完整討論串 (本文為第 7 之 12 篇):
4
4
2
8
9
10
0
6
文章代碼(AID): #1ZZ_mMEm (Marginalman)