int Count(int cou[]) //計算輸入的字母的個數
{
char word;
char letter[27]={' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'};
fstream SourceFile;
SourceFile.open("SourceFile.txt",ios::in);
if (!SourceFile)
{
cout<<"Can't open this file"<<endl;
}
while (!SourceFile.eof())
{
SourceFile.get(word);
if (word>='A'&&word<='Z')
{
word=word+32;
}
int i=word-96;
cou[i]++;
}
SourceFile.close();
cout<<"letter"<<'\t'<<'\t'<<"頻率為"<<endl;
for (int j=1;j<=26;j++)
{
if (cou[j]!=0)
{
cout<<letter[j]<<'\t'<<'\t'<<cou[j]<<endl;
}
}
cout<<"讀入完事兒!"<<endl;
return 0;
}
int Count(int cou[]) //計算輸入的字母的個數
{
char word;
char letter[27]={' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'}; //純屬多余
fstream SourceFile;
SourceFile.open("SourceFile.txt",ios::in); 從SourceFile.txt讀取
if (!SourceFile)
{
cout<<"Can't open this file"<<endl;
}
while (!SourceFile.eof()) 循環直到文件結束
{
SourceFile.get(word);
if (word>='A'&&word<='Z') //如果是大寫字母,統一轉換為小寫
{
word=word+32; //32是 'a' - 'A' ascii差
}
int i=word-96; // 96是'a'的ascii
cou[i]++; // cou保存你對應字母的統計數字,+1
}
SourceFile.close(); //關閉文件
cout<<"letter"<<'\t'<<'\t'<<"頻率為"<<endl; //輸出
for (int j=1;j<=26;j++) //依次輸出a~z字符的出現頻率
{
if (cou[j]!=0) //如果沒有這個字母出現,就不輸出
{
cout<<letter[j]<<'\t'<<'\t'<<cou[j]<<endl; //letter[j]可以用 (char)('A' + i) 代替
}
}
cout<<"讀入完事兒!"<<endl;
return 0;
}