[問題] 一題C的作業問題

看板C_and_CPP (C/C++)作者 (阿詠~^^萬歲)時間16年前 (2009/09/29 20:30), 編輯推噓4(4022)
留言26則, 3人參與, 最新討論串1/1
題目是這樣的.. Write a program to read a series of names, one per line, from standard input ,and write out those names spelled in reverse order to a file called file1. Write another program to read the names in from file1, reverse them again, sort them, and write them out to a file called file2. 我在寫file1的時候卡住了.. 可以讀名字進去..但是我不會排序.. 程式碼如下: #include <stdio.h> #include <string.h> int main(void) { FILE *file; char filename[20]; char name[20]; printf("Enter the name of the file: "); gets(filename); file=fopen(filename,"w"); if((file=fopen(filename,"w"))!=NULL){ printf("Enter several names:\n"); scanf("%s",name); while(!feof(stdin)){ fprintf(file,"%s\n",name); scanf("%s",name); } } fclose(file); return 0; } 我不知道還要加什麼.. 請大家幫忙一下.. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.130.138.118

09/29 20:37, , 1F
如果給你一串數字會排序嗎?? 會的話字串排序/比大小
09/29 20:37, 1F

09/29 20:37, , 2F
你名子是要排序什麼 開頭a.b.c.d嗎
09/29 20:37, 2F

09/29 20:38, , 3F
可以找 strcmp() 這個函數; 如果排序還不會的話可以
09/29 20:38, 3F

09/29 20:38, , 4F
在板上尋找 排序 看看有沒有可以參考的文章:)
09/29 20:38, 4F

09/29 20:40, , 5F
對了, 你的程式裡目前寫檔案這段好像不符合題目的要
09/29 20:40, 5F

09/29 20:40, , 6F
求, 要reverse order不是嗎?_?
09/29 20:40, 6F

09/29 20:41, , 7F
不過不知道會輸入幾筆還要倒著寫檔實在很麻煩, 總不
09/29 20:41, 7F

09/29 20:41, , 8F
是直接實作linked-list吧XD
09/29 20:41, 8F

09/29 20:43, , 9F
其實題目我有一點看不懂= =
09/29 20:43, 9F

09/29 20:44, , 10F
第二步也怪怪的, 反正都要排序了有沒有倒者讀回來有
09/29 20:44, 10F

09/29 20:44, , 11F
差嗎?? 莫非其實小弟的英文劣化到這種程度了....Orz
09/29 20:44, 11F

09/29 20:57, , 12F
if裡的file=fopen("filemane","w")感覺好像是多餘的
09/29 20:57, 12F

09/29 20:58, , 13F
我新手不好意思= =
09/29 20:58, 13F

09/29 20:58, , 14F
file!=NULL應該就可以了
09/29 20:58, 14F

09/29 21:00, , 15F
謝謝囉..我在想想!!
09/29 21:00, 15F

09/29 21:03, , 16F
我問到了..第一個是把輸入的名字反過來存
09/29 21:03, 16F

09/29 21:04, , 17F
jack變成kcaj
09/29 21:04, 17F

09/29 21:04, , 18F
第二個是在把它反回來後再排序
09/29 21:04, 18F

09/29 21:05, , 19F
但是我還是不會做= =
09/29 21:05, 19F

09/29 21:13, , 20F
再設一個字元陣列2 字串的結束符號是/0反過來讀取存到陣列2
09/29 21:13, 20F

09/29 21:15, , 21F
再用fputs將陣列2的字串寫入檔案
09/29 21:15, 21F

09/29 21:21, , 22F
我看不懂= =
09/29 21:21, 22F

09/29 22:12, , 23F
其實倒不見得一定要用另一個陣列存反序好的字串啦....
09/29 22:12, 23F

09/29 22:12, , 24F
直接把字串倒著用fprintf與%c印入檔案去就好了XD
09/29 22:12, 24F

09/29 22:13, , 25F
不過考慮之後的字串排序, 也許需要用動態配置字串陣列
09/29 22:13, 25F

09/29 22:14, , 26F
才放得下所有的字串來排序@_@"
09/29 22:14, 26F
文章代碼(AID): #1AmVu2tA (C_and_CPP)
文章代碼(AID): #1AmVu2tA (C_and_CPP)