這就是我到目前爲止;我想在文本文件中有一個所有字符和空格的概率的數組,但是我對數據類型有問題。C++中的頻率表
int main()
{
float x[27];
unsigned sum = 0;
struct Count {
unsigned n;
void print(unsigned index, unsigned total) {
char c = (char)index;
if (isprint(c)) cout << "'" << c << "'";
else cout << "'\\" << index << "'";
cout << " occured " << n << "/" << total << " times";
cout << ", propability is " << (double)n/total << "\n";
}
Count() : n() {}
} count[256];
ifstream myfile("C:\\text.txt"); // one \ masks the other
while (!myfile.eof()) {
char c;
myfile.get(c);
if (!myfile) break;
sum++;
count[(unsigned char)c].n++;
}
for (unsigned i = 0; i<256; i++)
{
count[i].print(i, sum);
}
x[0] = count[33];
int j=68;
for(int i=1;i<27;i++)
{
x[i]=count[j];
j++;
}
return 0;
}
您是否正在計算文件c:\ text.txt中的字母?或者是「c:\ text.txt」只是一個令人困惑的示例字符串? – Unimportant
yes @ user1320881我也對此感到困惑。您目前正在嘗試計算文件字符串本身的字符「C:\ text.txt」。 –
是的這是文件的位置 –