2018年9月8日 星期六

itsa [C_AR030-中]文字頻率分析

註: tUp英文大寫次數 tLow英文小寫次數 tw字次數

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
int c, w, count = 0, x = 0, y = 0, z = 0, g = 0, a = 0, b = 0, check = 0;
int tUp[26] = { 0 }, tLow[26] = { 0 }, tw[100] = { 0 };
string prefix, suffix, s, str[100], pre[100], suf[100], wo[100];
cin >> prefix;
cin >> suffix;
cin >> c;
cin >> w;
getline(cin, s);
getline(cin, s);
stringstream ss(s);
while (ss >> str[x])
x++;
for (int i = 0; i < x; i++)
{
check = 0;
count = 0;
if (str[i].length() >= prefix.length())
{
for (int j = 0; j < prefix.length(); j++)
{
if (str[i][j] == prefix[j])
count++;
else
break;
}
if (count == prefix.length())
{
for (int k = 0; k < y; k++)
if (pre[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
pre[y++] = str[i];
}
}
}
for (int i = 0; i < x; i++)
{
a = 0;
count = 0;
check = 0;
if (str[i].length() >= suffix.length())
{
for (int j = str[i].length() - suffix.length(); j < str[i].length(); j++)
{
if (str[i][j] == suffix[a++])
count++;
else
break;
}
if (count == suffix.length())
{
for (int k = 0; k < z; k++)
if (suf[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
suf[z++] = str[i];
}
}
}
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < s.length(); j++)
{
if (s[i] == s[j])
{
if (s[i] >= 97 && s[i] <= 122)
tLow[s[i] - 'a']++;
else if (s[i] >= 65 && s[i] <= 90)
tUp[s[i] - 'A']++;
s[j] = ' ';
}
}
}
for (int i = 0; i < x; i++)
{
b = 0;
check = 0;
for (int j = 0; j < x; j++)
{
count = 0;
if (str[i].length() == str[j].length())
{
for (int k = 0; k < str[i].length(); k++)
{
if (str[i][k] == str[j][k])
count++;
else
break;
}
}
if (count == str[i].length())
b++;
}
if (b >= w)
{
for (int k = 0; k < g; k++)
if (wo[k].compare(str[i]) == 0)
check = 1;
if (check == 0)
{
wo[g] = str[i];
tw[g] = b;
g++;
}
}
}
for (int i = 0; i < g - 1; i++)
{
for (int j = i + 1; j < g; j++)
{
if (wo[i].compare(wo[j]) == 1)
{
string tmp = wo[i];
wo[i] = wo[j];
wo[j] = tmp;
int temp = tw[i];
tw[i] = tw[j];
tw[j] = temp;
}
}
}
cout << "prefix of " << prefix << ":" << endl;
for (int i = 0; i < y; i++)
cout << pre[i] << endl;
cout << "suffix of " << suffix << ":" << endl;
for (int i = 0; i < z; i++)
cout << suf[i] << endl;
cout << "character frequency over " << c << ":" << endl;
for (int i = 0; i < 26; i++)
{
if (tUp[i] >= c)
cout << (char)(i + 'A') << ',' << tUp[i] << endl;
}
for (int i = 0; i < 26; i++)
{
if (tLow[i] >= c)
cout << (char)(i + 'a') << ',' << tLow[i] << endl;
}
cout << "word frequency over " << w << ":" << endl;
for (int i = 0; i < g; i++)
cout << wo[i] << ',' << tw[i] << endl;
return 0;
}

1 則留言: