是
时间: 2024-02-19 18:03:11 浏览: 14
好的,这里是一个简单的C语言程序,可以完成您的任务:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX_WORD_LENGTH 50 // 单词的最大长度
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("用法: %s [文件路径] [模式]\n", argv[0]);
return 1;
}
char *filepath = argv[1];
char *mode = argv[2];
// 打开文件
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", filepath);
return 1;
}
// 初始化计数器
int word_count = 0;
int letter_count = 0;
int digit_count = 0;
int space_count = 0;
int newline_count = 0;
int punct_count = 0;
int other_count = 0;
// 统计字符个数和单词个数
char word[MAX_WORD_LENGTH];
int in_word = 0;
int len = 0;
char c;
while ((c = fgetc(fp)) != EOF) {
if (isalpha(c)) {
letter_count++;
if (!in_word) {
in_word = 1;
len = 0;
}
word[len++] = c;
if (len >= MAX_WORD_LENGTH) {
printf("单词 %s 过长\n", word);
return 1;
}
} else if (isdigit(c)) {
digit_count++;
if (!in_word) {
in_word = 1;
len = 0;
}
word[len++] = c;
if (len >= MAX_WORD_LENGTH) {
printf("单词 %s 过长\n", word);
return 1;
}
} else {
if (in_word) {
in_word = 0;
word[len] = '\0';
word_count++;
len = 0;
}
if (isspace(c)) {
space_count++;
if (c == '\n') {
newline_count++;
}
} else if (ispunct(c)) {
punct_count++;
} else {
other_count++;
}
}
}
// 关闭文件
fclose(fp);
// 输出结果
if (strcmp(mode, "detail") == 0) {
printf("单词个数:%d\n", word_count);
printf("字母个数:%d\n", letter_count);
printf("数字个数:%d\n", digit_count);
printf("空格个数:%d\n", space_count);
printf("回车个数:%d\n", newline_count);
printf("标点个数:%d\n", punct_count);
printf("其他个数:%d\n", other_count);
} else if (strcmp(mode, "summary") == 0) {
char *result_file = "result.txt";
FILE *fp_out = fopen(result_file, "w");
if (fp_out == NULL) {
printf("无法创建结果文件 %s\n", result_file);
return 1;
}
fprintf(fp_out, "单词个数:%d\n", word_count);
fprintf(fp_out, "字母个数:%d\n", letter_count);
fprintf(fp_out, "数字个数:%d\n", digit_count);
fprintf(fp_out, "空格个数:%d\n", space_count);
fprintf(fp_out, "回车个数:%d\n", newline_count);
fprintf(fp_out, "标点个数:%d\n", punct_count);
fprintf(fp_out, "其他个数:%d\n", other_count);
fclose(fp_out);
printf("已完成“%s”文件的统计,统计结果保存在文件“%s”中!\n", filepath, result_file);
} else {
printf("不支持的模式 %s\n", mode);
return 1;
}
return 0;
}
```
这个程序会根据命令行参数传入的文件路径和处理模式统计该文件中的字符个数、单词个数、数字个数、空格个数、回车个数、标点个数和其他字符个数,并将结果输出到屏幕上和一个结果文件中。
在细节模式下,程序会详细输出每个统计项的个数;在概要模式下,程序仅在屏幕上输出一条简短的信息,告知结果文件的路径和文件名。
该程序使用了标准库函数和C语言字符串处理函数,同时通过文件操作函数将结果保存在一个结果文件中。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)