1 / 9

Homework 4

Homework 4. 透過 命令列引數 輸入欲讀入及輸出的檔名 u9209xxhw4 input_file output_file 統計每個字元出現的次數 將統計結果輸出到一個檔案中. 基本要求. 輸入檔案名稱 要檢查輸入的檔案是否存在 將整篇文章輸出到螢幕( printf ) 統計 文章中每個字出現的次數,由多到少列出 將統計結果輸出到一個檔案中 盡可能的將程式寫成多個函式. 相關規定. 輸入測試檔如網頁上 hw4 後之連結 抄襲: 0% 不按規定繳交作業視同未交作業 Deadline : 2003/12/20 (Sat.) 晚上 24:00

oren
Download Presentation

Homework 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 4 • 透過命令列引數輸入欲讀入及輸出的檔名 • u9209xxhw4 input_file output_file • 統計每個字元出現的次數 • 將統計結果輸出到一個檔案中

  2. 基本要求 • 輸入檔案名稱 • 要檢查輸入的檔案是否存在 • 將整篇文章輸出到螢幕(printf) • 統計文章中每個字出現的次數,由多到少列出 • 將統計結果輸出到一個檔案中 • 盡可能的將程式寫成多個函式

  3. 相關規定 • 輸入測試檔如網頁上 hw4 後之連結 • 抄襲:0% • 不按規定繳交作業視同未交作業 • Deadline: 2003/12/20 (Sat.) 晚上24:00 • 遲交:八折/三天

  4. 評分標準 • 輸入檔案名稱由命令列輸入,且必須檢查使用者檔案是否能開啟 (15%) • 將整篇文章顯示在螢幕上(15%) • 統計整篇文章每個字出現的數目並由大到小列出,大小寫視為相同的字,並輸出到檔案中(50%) • 註解、程式結構(20%) • 如有更動,以課程網頁上之內容為標準 • 測試檔案公布於網頁

  5. 作業目的 • 練習指標的使用 • 練習檔案的輸入 • 練習對檔案資料作處理 • 練習使用命令列引數

  6. 命令列引數 (1/3) • Command-line Arguments • int main(int argc, char *argv[]) • argc (argument count):變數的數目 • *argv[] (argument vector):儲存輸入變數向量 • 例如:gcc -Wall haha.c -o haha 0 1 2 3 4 argc = 5, gcc\0 argv[0] argv -Wall\0 argv[1] haha.c\0 argv[2] -o\0 argv[3] Haha\0 argv[4]

  7. 命令列引數 (2/3) #include <stdio.h> int main (int argc, char *argv[]) { int i; i = argc-1; do{ printf("%s \n", argv[i--]); }while(i >= 0); return 0; } $> gcc -Wall kk.c -o kk $> ./kk haha ccc lll lll ccc haha ./kk $> ./kk ./kk

  8. /* 命令列引數例子 - 2 */ #include <stdio.h> #include <stdlib.h> //atoi 跟 atof void main(int argc, char **argv) { int i,num1; float num2; for(i = 0; i < argc; i++) printf("第 %d 個引數為 %s\n",i+1, argv[i]); //如果已知第二個引數為整數 //而且第三個引數為浮點數 num1 = atoi(argv[1]); num2 = atof(argv[2]); printf("第二個引數轉換過之後為 %d\n", num1); printf("第三個引數轉換過之後為 %f\n", num2); } 命令列引數 (3/3) $> ./kk 1 3.14 abc def xxx -c haha 第 1 個引數為 ./kk 第 2 個引數為 1 第 3 個引數為 3.14 第 4 個引數為 abc 第 5 個引數為 def 第 6 個引數為 xxx 第 7 個引數為 -c 第 8 個引數為 haha 第二個引數轉換過之後為 1 第三個引數轉換過之後為 3.140000

  9. 期望結果 :174 e: 95 t: 75 a: 71 o: 69 s: 61 n: 50 … … … 0: 3 -: 1 7: 1 4: 1 1: 1 x: 1 :174 e: 95 t: 75 a: 71 o: 69 s: 61 n: 50 i: 50 l: 44 r: 41 h: 39 d: 28 u: 23 p: 20 c: 18 y: 17 m: 16 .: 16 k: 16 \n: 14 w: 14 f: 13 g: 12 b: 8 ,: 8 v: 6 j: 6 ': 5 0: 3 -: 1 7: 1 4: 1 1: 1 x: 1

More Related