GIT地址 | 点击查看 |
结队伙伴 | 杜文锋201831061119 |
一 PSP表格
二 接口的设计与实现过程
a.设计思路
b 代码实现思路
源码:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include<stdlib.h> 4 #include<string.h> 5 #define MAX 1000000 6 using namespace std; 7 8 typedef struct node { 9 char word[100]; 10 int n; 11 }Node; 12 13 int lines = 0;//文件有效行数 14 int characters = 0;//文件字符数 15 16 int isLetter(char c) { 17 if (c <= 'Z' && c >= 'A' || c <= 'z' && c >= 'a') { 18 return 1; 19 } 20 return 0; 21 }//判断是不是字母 22 23 int isDigit(char c) { 24 if (c <= '9' && c >= '0') { 25 return 1; 26 } 27 return 0; 28 }//判断是不是数字 29 30 int isletterOrDigit(char c) { 31 return (isLetter(c) || isDigit(c)); 32 }//判断是数字还是字母 33 34 char tolower(char c) { 35 if (c <= 'Z' && c >= 'A') { 36 return (char)(c - 'A' + 'a'); 37 } 38 return c; 39 }//将大写字母转化为小写字母 40 41 char* readFile() { 42 FILE* fp; 43 char* text = NULL; 44 int length = 0; 45 int i, size = 0; 46 char temp[1024] = { 0 }; 47 char* getk = 0; 48 fp = fopen("input.txt", "r"); 49 if (fp == NULL) { 50 printf("打开文件失败!\n"); 51 exit(-1); 52 } 53 text = (char*)malloc(sizeof(char) * (MAX)); 54 while (!feof(fp)) { 55 getk = fgets(temp, 1024, fp); 56 if (feof(fp) && !getk) { 57 break; 58 } 59 length = strlen(temp); 60 characters += length; 61 if (length > 1) { 62 lines++; 63 } 64 for (i = 0; i < length; i++) { 65 if (isletterOrDigit(temp[i])) { 66 text[size++] = tolower(temp[i]); 67 } 68 else { 69 text[size++] = ' '; 70 } 71 } 72 } 73 text[size] = '\0'; 74 fclose(fp); 75 return text; 76 }//读取input文件 77 78 int notIn(char* words[], int count) { 79 int i; 80 for (i = 0; i < count; i++) { 81 if (strcmp(words[i], words[count]) == 0) { 82 return 0; 83 } 84 } 85 return 1; 86 }//终止条件 87 88 char** split(char* text, int* size) { 89 int i; 90 int count = 0, L = 0; 91 char** words; 92 words = (char**)malloc(sizeof(char*) * (MAX / 2)); 93 for (i = 0; i < MAX / 2; i++) { 94 words[i] = (char*)malloc(sizeof(char) * 100); 95 } 96 int len = strlen(text); 97 98 for (i = 0; i < len; i++) { 99 if (text[i] == ' ' || text[i] == '\0') { 100 words[count][L++] = '\0'; 101 if (L > 1 && !isDigit(words[count][0])) { 102 count++; 103 (*size)++; 104 } 105 L = 0; 106 } 107 else { 108 words[count][L++] = text[i]; 109 } 110 } 111 words[count][L++] = '\0'; 112 if (L > 1 && !isDigit(words[count][0])) { 113 count++; 114 (*size)++; 115 } 116 return words; 117 }//把一个字符串变为字符串数组 118 119 int remove_repeat(char** words, int osize, Node* nodes) { 120 int i, j; 121 char** ws; 122 int wi = 0; 123 int exist = 0; 124 int size = 0; 125 ws = (char**)malloc(sizeof(char*) * (osize)); 126 for (i = 0; i < osize; i++) { 127 ws[i] = (char*)malloc(sizeof(char) * 100); 128 } 129 for (i = 0; i < osize; i++) { 130 exist = 0; 131 for (j = 0; j < size; j++) { 132 if (strcmp(words[i], ws[j]) == 0) { 133 exist = 1; 134 } 135 } 136 if (!exist) { 137 size++; 138 strcpy(ws[wi], words[i]); 139 strcpy(nodes[wi].word, words[i]); 140 nodes[wi].n = 0; 141 wi++; 142 } 143 } 144 return size; 145 }//统计重复单词 146 147 void count(char* text) { 148 char** words; 149 Node* n_count; 150 int i, j, size = 0; 151 int nsize = 0; 152 words = split(text, &nsize); 153 Node t; 154 n_count = (Node*)malloc(sizeof(Node) * nsize); 155 size = remove_repeat(words, nsize, n_count); 156 157 for (i = 0; i < nsize; i++) { 158 for (j = 0; j < size; j++) { 159 if (strcmp(n_count[j].word, words[i]) == 0) { 160 n_count[j].n++; 161 } 162 } 163 } 164 for (i = 0; i < size; i++) { 165 for (j = 0; j < size - i - 1; j++) { 166 if (n_count[j].n < n_count[j + 1].n) { 167 t = n_count[j]; 168 n_count[j] = n_count[j + 1]; 169 n_count[j + 1] = t; 170 } 171 } 172 } 173 cout << "characters:" << characters << endl; 174 cout<<"words:"<< nsize << endl; 175 cout<<"lines:"<< lines << endl; 176 177 size = size > 10 ? 10 : size; 178 179 for (i = 0; i < size; i++) { 180 cout << n_count[i].word <<":"<< n_count[i].n << endl; 181 } 182 183 184 }//统计数据并输出 185 186 int main() { 187 char* text; 188 text = readFile(); 189 count(text); 190 system("pause"); 191 return 0; 192 }
c 程序运行结果
三 代码复审
a编码规范: Google C++编程规范
b代码复审:在代码完成拼接后,无错误提是但是有10个警告,无法运行程序.
经过百度的搜索学习 在前面添加#define _CRT_SECURE_NO_WARNINGS
忽视警告进行运行即可.
四性能测试
由图可以得到main主函数的使用次数最多
int main() { char* text; text = readFile(); count(text); system("pause"); return 0; }
六 异常处理
当未创建一个input.txt文件时
六 结队过程
在本次结队编程中由于我和杜文锋是室友,且桌子靠近,所以我们在编程时交流频繁,让我们对于个自的代码都比较熟悉,并在编写函数时进行了充分的测试.体会到了结队编程的便捷和高效.深刻的了解结队编程.