在Vc环境下分类统计各类字符个数
时间: 2023-12-10 10:39:35 浏览: 24
在VC环境下,可以使用以下方法来分类统计各类字符个数:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// 打开文件
ifstream fin("input.txt");
if (!fin) {
cerr << "无法打开文件" << endl;
return 1;
}
// 统计字符个数
int count[128] = { 0 };
char ch;
while (fin.get(ch)) {
if (ch >= 0 && ch <= 127) {
count[ch]++;
}
}
// 输出结果
for (int i = 0; i < 128; i++) {
if (count[i] > 0) {
cout << (char)i << ": " << count[i] << endl;
}
}
// 关闭文件
fin.close();
return 0;
}
```
这段代码会打开一个名为"input.txt"的文本文件,并统计其中出现的各类字符的个数。最后,程序会输出结果,格式为"字符: 个数"。请注意,由于本程序使用的是ASCII码,因此只能统计ASCII码范围内(即0~127)的字符。如果需要统计更多字符,需要使用更高级的字符编码。