求c语言大神帮忙
2个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
static char interval[] = { ' ', '\t', '\n', ',', '.', '?', '(', ')', '"', ':' };
static int word_num[10] = {0};
#define INTER_SIZE sizeof(interval)/sizeof(char)
int read_passage_word(FILE* fp);
bool is_interval(char check_charactor);
int main(){
int all_num = 0;
/*此处需要进行修改,fopen("你建立的文本的路径","r");
注意windows环境下文本路径需要使用双斜杠如:C://A.txt */
FILE* fp = fopen("A.txt", "r");
read_passage_word(fp);
fclose(fp);
for (int i = 0; i < 10; ++i){
all_num += word_num[i];
printf("#%-4d:%8d\n", i + 1, word_num[i]);
}
printf("The word number of the passage is :\t%d", all_num);
return 0;
}
/*用来统计文本中不同字数的单词数量*/
int read_passage_word(FILE* fp){
char ch;
int char_num = 0;
while (!feof(fp)){
ch = fgetc(fp);
if (is_interval(ch)){
if (char_num != 0){
word_num[char_num - 1]++;
char_num = 0;
}
continue;
}
char_num++;
}
return 0;
}
/*判断是不是单词间的间隔*/
bool is_interval(char check_charactor){
for (int i = 0; i < INTER_SIZE; ++i){
if (check_charactor == interval[i])
return true;
}
return false;
}
#include<stdlib.h>
static char interval[] = { ' ', '\t', '\n', ',', '.', '?', '(', ')', '"', ':' };
static int word_num[10] = {0};
#define INTER_SIZE sizeof(interval)/sizeof(char)
int read_passage_word(FILE* fp);
bool is_interval(char check_charactor);
int main(){
int all_num = 0;
/*此处需要进行修改,fopen("你建立的文本的路径","r");
注意windows环境下文本路径需要使用双斜杠如:C://A.txt */
FILE* fp = fopen("A.txt", "r");
read_passage_word(fp);
fclose(fp);
for (int i = 0; i < 10; ++i){
all_num += word_num[i];
printf("#%-4d:%8d\n", i + 1, word_num[i]);
}
printf("The word number of the passage is :\t%d", all_num);
return 0;
}
/*用来统计文本中不同字数的单词数量*/
int read_passage_word(FILE* fp){
char ch;
int char_num = 0;
while (!feof(fp)){
ch = fgetc(fp);
if (is_interval(ch)){
if (char_num != 0){
word_num[char_num - 1]++;
char_num = 0;
}
continue;
}
char_num++;
}
return 0;
}
/*判断是不是单词间的间隔*/
bool is_interval(char check_charactor){
for (int i = 0; i < INTER_SIZE; ++i){
if (check_charactor == interval[i])
return true;
}
return false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询