#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int i, j, k, t, a[200] = { 0 }, b[200] = { 0 };
cout << "请输入文本:" ;
cin >> str;
t = str.size();
for (i = 0; i<t; i++)
{
if (str[i] >= 'a'&& str[i] <= 'z')
{
a[str[i] - 97 + 26]++;
}
else if (str[i] >= 'A'&& str[i] <= 'Z')
{
a[str[i] - 65]++;
}
else if (str[i] > -127 && str[i]<-96 || str[i]>-86 && str[i] < -2)
{
a[str[i] + 179]++;
i++;
}
}
for (i = 0; i < 200; i++)
{
b[i] = a[i];
}
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200 - i; j++)
{
if (b[j] < b[j + 1])
{
k = b[j + 1];
b[j + 1] = b[j];
b[j] = k;
}
}
}
if (b[0] == 0)
{
cout << "出现频次最高的是:";
cout << "无频次最高的字母或字 "<<endl;
cout << "出现频次第二高的是:";
cout << "无频次第二高的字母或字 "<<endl;
cout << "出现频次第三高的是:";
cout << "无频次第三高的字母或字 "<<endl;
}
if (b[0] > 0)
{
int x, y;
for (i = 0; i < 200; i++)
{
if (b[i] < b[0])
{
x = b[i];
break;
}
}
if (x == 0)
{
y = 0;
}
if (x > 0)
{
for (i = 0; i < 200; i++)
{
if (b[i] < x)
{
y = b[i];
break;
}
}
}
cout << "出现频次最高的是:";
for (i = 0; i < 200; i++)
{
if (a[i] == b[0] && i >= 26 && i < 52)
{
cout << (char)(65 + 6 + i) << "共出现" << b[0] << "次" << " ";
}
if (a[i] == b[0] && i < 26)
{
cout << (char)(65 + i) << "共出现" << b[0] << "次" << " ";
}
if (a[i] == b[0] && i >= 52)
{
for (j = 0; j < t; j++)
{
if (str[j] + 179 == i)
{
cout << str[j] << str[j + 1] << "共出现" << b[0] << "次" << " ";
j = j + 1;
break;
}
if (str[j] + 179 != i && (str[j] > -127 && str[j]<-96 || str[j]>-86 && str[j] < -2))
{
j = j + 1;
}
}
}
}
cout << endl;
cout << "出现频次第二高的是:";
for (i = 0; i < 200; i++)
{
if (a[i] == x && x > 0 && i >= 26 && i < 52)
{
cout << (char)(65 + 6 + i) << "共出现" << x << "次" << " ";
}
if (a[i] == x && x > 0 && i < 26)
{
cout << (char)(65 + i) << "共出现" << x << "次" << " ";
}
if (a[i] == x && x > 0 && i >= 52)
{
for (j = 0; j < t; j++)
{
if (str[j] + 179 == i)
{
cout << str[j] << str[j + 1] << "共出现" << x << "次" << " ";
j = j + 1;
break;
}
if (str[j] + 179 != i && (str[j] > -127 && str[j]<-96 || str[j]>-86 && str[j] < -2))
{
j = j + 1;
}
}
}
if (x == 0)
{
cout << "无频次第二高的字母或字 ";
break;
}
}
cout << endl;
cout << "出现频次第三高的是:";
for (i = 0; i < 200; i++)
{
if (a[i] == y && y > 0 && i >= 26 && i < 52)
{
cout << (char)(65 + 6 + i) << "共出现" << y << "次" << " ";
}
if (a[i] == y && y > 0 && i < 26)
{
cout << (char)(65 + i) << "共出现" << y << "次" << " ";
}
if (a[i] == y && y > 0 && i >= 52)
{
for (j = 0; j < t; j++)
{
if (str[j] + 179 == i)
{
cout << str[j] << str[j + 1] << "共出现" << y << "次" << " ";
j = j + 1;
break;
}
if (str[j] + 179 != i && (str[j] > -127 && str[j]<-96 || str[j]>-86 && str[j] < -2))
{
j = j + 1;
}
}
}
if (y == 0 || x == 0)
{
cout << "无频次第三高的字母或字 ";
break;
}
}
cout << endl;
}
return 0;
}
Qt实现统计文本出现最高频次的字符
需积分: 50 107 浏览量
2017-12-04
11:22:11
上传
评论
收藏 2.1MB RAR 举报
奥本海姆的小徒孙
- 粉丝: 4
- 资源: 11
最新资源
- 重启进行BIOS快捷方式,不需要开机按BIOS键
- 威纶通触摸屏编程软件Easy builder pro V6.09.01.556安装包(2024.04).txt
- WindowsAdminCenter
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈