一.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