軟件質量測試 第二周 wordcount 作業
一.github地址
https://github.com/WKX121/WC
二.PSP
PSP表格
|
三.思路
1.根據需求首先最傳入main函數中的args解析出對應的命令,文件名
2.-c命令通過每次fgetc使wc_char加一
3.-w通過對空格和,的判斷使wc_word加一
4.-l通過對/n判斷使wc_line加一
四.程序的設計實現
通過對args不同數值的情況來處理各種統計模式,看對應哪個命令就調用相應的處理程序進行處理。然後將計數後的結果輸出到指定txt文件當中。
五.代碼說明
#include <stdio.h> #include <stdlib.h> #include<conio.h> #include <string.h> int main(int argc, char * argv[]) { int wc_char = 0; int wc_word = 1; int wc_line = 1; char filename[80]; FILE *fp = NULL; if (argc == 3) { fp = fopen(argv[2], "r"); if (fp == NULL) { printf("打開有誤!\n"); printf("請按enter鍵繼續...."); _getch(); exit(0); //... } char ch; while (1) { ch = fgetc(fp); wc_char++; if (ch == ‘ ‘||ch==‘,‘) { wc_word++; } else if (ch == ‘\n‘) { wc_word++; wc_line++; } else if (ch == EOF) { break; } } if (strcmp(argv[1],"-c")==0) { printf("The char count is %d\n", wc_char); fp = fopen("result.txt","w"); fprintf(fp,"字符數:%d\n", wc_char); } else if (strcmp(argv[1],"-w")==0) { printf("The word count is %d\n", wc_word); fp = fopen("result.txt","w"); fprintf(fp,"單詞數:%d\n", wc_word); } else if (strcmp(argv[1],"-l")==0) { printf("The line count is %d\n", wc_line); fp = fopen("result.txt","w"); fprintf(fp,"行數:%d\n", wc_line); } fclose(fp); } if (argc == 4) { fp = fopen(argv[3], "r"); if (fp == NULL) { printf("打開有誤!\n"); printf("請按enter鍵繼續...."); _getch(); exit(0); //... } char ch; while (1) { ch = fgetc(fp); wc_char++; if (ch == ‘ ‘||ch==‘,‘) { wc_word++; } else if (ch == ‘\n‘) { wc_word++; wc_line++; } else if (ch == EOF) { break; } } if (strcmp(argv[1],"-c")==0&&strcmp(argv[2],"-w")==0) { printf("The char count is %d\n", wc_char); printf("The word count is %d\n", wc_word); fp = fopen("result.txt","w"); fprintf(fp,"字符數:%d\n單詞數:%d\n", wc_char,wc_word); } if (strcmp(argv[1],"-c")==0&&strcmp(argv[2],"-l")==0) { printf("The char count is %d\n", wc_char); printf("The line count is %d\n", wc_line); fp = fopen("result.txt","w"); fprintf(fp,"字符數%d\n行數:%d\n", wc_char,wc_line); } if (strcmp(argv[1],"-w")==0&&strcmp(argv[2],"-l")==0) { printf("The word count is %d\n", wc_word); printf("The line count is %d\n", wc_line); fp = fopen("result.txt","w"); fprintf(fp,"單詞數:%d\n行數:%d\n", wc_word,wc_line); } fclose(fp); } if (argc == 5) { fp = fopen(argv[4], "r"); if (fp == NULL) { printf("打開有誤!\n"); printf("請按enter鍵繼續...."); _getch(); exit(0); //... } char ch; while (1) { ch = fgetc(fp); wc_char++; if (ch == ‘ ‘||ch==‘,‘) { wc_word++; } else if (ch == ‘\n‘) { wc_word++; wc_line++; } else if (ch == EOF) { break; } } if (strcmp(argv[1],"-c")==0) { printf("The char count is %d\n", wc_char); } else if (strcmp(argv[1],"-w")==0) { printf("The word count is %d\n", wc_word); } else if (strcmp(argv[1],"-l")==0) { printf("The line count is %d\n", wc_line); } if (strcmp(argv[2],"-c")==0) { printf("The char count is %d\n", wc_char); } else if (strcmp(argv[2],"-w")==0) { printf("The word count is %d\n", wc_word); } else if (strcmp(argv[2],"-l")==0) { printf("The line count is %d\n", wc_line); } if (strcmp(argv[3],"-c")==0) { printf("The char count is %d\n", wc_char); } else if (strcmp(argv[3],"-w")==0) { printf("The word count is %d\n", wc_word); } else if (strcmp(argv[3],"-l")==0) { printf("The line count is %d\n", wc_line); } fp = fopen("result.txt","w"); fprintf(fp,"字符數:%d\n單詞數:%d\n行數:%d\n", wc_char,wc_word,wc_line); fclose(fp); } }
六.測試設計過程
10個測試:
1.-c
2.-w
3.-l
4.-c -w
5.-c -l
6.-w -l
7.-c -w -l
8.復雜符號測試
9.代碼測試
10.復雜代碼測試
七.參考文獻
http://www.cnblogs.com/cool125/p/7560596.html
軟件質量測試 第二周 wordcount 作業