读书人

可以帮小弟我找上异常吗

发布时间: 2013-03-01 18:33:02 作者: rapoo

可以帮我找下错误吗

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

void lowercase_remove_punct(char *original_string, char *processed_string) {
int i1, i2;
char c;
for (i1 = 0, i2 = 0; i1 < strlen(original_string); i1++) {
c = original_string[i1];
if ((c >= 'A') && (c <= 'Z'))
processed_string[i2++] = original_string[i1] - 'A' + 'a';
if (((c >= 'a') && (c <= 'z')) || (c == '\''))
processed_string[i2++] = original_string[i1];
}
processed_string[i2] = '\0';
}

void BubbleSort(int* a, char** b, int length) {
if (length > 1) {
int j, k, t;
char* temp;
temp = (char*) malloc(20 * sizeof (char));
while (1) {
int flag = 0;
j = 0;
for (k = j + 1; k < length; j++, k++) {
if (a[j] > a[k]) {
t = a[j];
strcpy(temp, b[j]);
a[j] = a[k];
strcpy(b[j], b[k]);
a[k] = t;
strcpy(b[k], temp);
flag = 1;
}
}
if (flag == 0) break;
}
}
}

int main(int argc, char* argv[]) {
FILE* in_file;
in_file = fopen("textfile.txt", "r");
char* in_string;
char* out_string;
char** store_string;


int* count_array;
int end_of_file;
store_string = (char**) malloc(500 * sizeof (char*));
count_array = (int*) malloc(500 * sizeof (int));
if (in_file == NULL) {
printf("file open fail");
} else {
int num = -1;
do {
in_string = (char*) malloc(20 * sizeof (char));
out_string = (char*) malloc(20 * sizeof (char));
end_of_file = fscanf(in_file, "%s", in_string);
if (end_of_file != EOF) {
num++;
lowercase_remove_punct(in_string, out_string);
if (out_string[0] == '\0') {
num--;
continue;
}
store_string[num] = (char*) malloc(20 * sizeof (char));
int i;
int flag = 0;
for (i = 0; i < num; i++) {
if (strcmp(store_string[i], out_string) == 0) {
flag = 1;
count_array[i]++;
}
}
if (flag != 1) {
strcpy(store_string[num], out_string);
count_array[num] = 1;
} else {
num--;


}
}
} while (end_of_file != EOF);
fclose(in_file);
printf("All the words appeared without repeatation:\n");
int p;
for(p=0;p<=num;p++){
printf("%s ", store_string[p]);
}
printf("\n");
BubbleSort(count_array, store_string, num + 1);
int k1, k2;
printf("20 least words:\n");
for (k1 = 0; k1 < 20; k1++) {
printf("%s %d\n", store_string[k1], count_array[k1]);
}
printf("20 most words:\n");
for (k2 = num; k2 > num - 20; k2--) {
printf("%s %d\n", store_string[k2], count_array[k2]);
}
}
return 0;
}



各位大神,帮我找下错误吧,。。文件打不开。有些地方也不知道有没有写对
[解决办法]
没仔细看你的程序内容,但如果是文件打不开,无非是文件名错误或路径不对,你核对一下。另外 ,程序有以下几个小建议
for (i1 = 0, i2 = 0; i1 < strlen(original_string); i1++)
中的strlen(original_string)运行次数太多,影响效率,建议另增加一个变量存放字符串长度;
同理 processed_string[i2++] = original_string[i1] - 'A' + 'a';
中'a' - 'A' 用一个变量代替;
in_file = fopen("F:\textfile.txt", "r"); 打开方式最好写明"rt"或"r+t"
store_string = (char**) malloc(500 * sizeof (char*)); 这个好像存不了什么数据,要用就直接用char二维数组,char*是指针的意思,其实是一个整形的占位长度,32位编译结果就是4个字节,500*4就是2000字节,应该不符合原意。
[解决办法]
LZ 我会跟你说用DEV-C++帮你debug了一下我蓝屏了吗 - -


重启后又运行了一下:

结果如下:

20 least words:
whatthefuck 1
bluescreen 1
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
(null) 0
20 most words:
bluescreen 1
whatthefuck 1


之后弹出错误,我就懒得继续看了…… 目测是文件读取代码不安全造成的

读书人网 >C语言

热点推荐