![]() |
#2
orleans2010-04-10 09:34
|
EX:
Letter Frequencies:
A 10 B 13
C 20 D 0
E 17 F 4
Rest of Letters
Upper Case 50 Lower Case 17 White Space 10
问题是,结果是26个字母都计算了,出来26次。 但是我只想要有字母的次数,没有提到的字母不写出来。。。。怎么改进
做出来的结果如下:(此程序由yyblackyy原创)

#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<cstdlib>
#define Maxpath 80 // The maximum name of the file
using namespace std;
int fre[26]; // The times of each char
const char str[]="abcdefghijklmnopqrstuvwxyz"; // Alphabet
static int up=0,low=0,wsp=0,total=0;
void frequency(char ch);
void count(char file_name[]);
void print();
int main()
{
char filename[Maxpath];
cout<<"Please enter the file_name"<<endl;
cin.getline (filename,Maxpath); // get a file name
count(filename);
print();
return 0;
}
//************************************************
void frequency(char ch)
{
for(int i=0;i<26;i++)
if(ch==str[i])
{
fre[i]++;
break;
}
}
//************************************************************
void count(char filename[])
{
fstream istr;
char ch,ch2;
istr.open (filename,ios::in);
if(istr.fail ())
{
cout<<"Can't open!"<<endl;
exit(1);
}
do
{
ch=istr.get ();
if(isspace(ch)) // judge the write space or not
wsp++;
else if(isalpha(ch)) // judge the Alphabet or not
{
if(isupper(ch)) // judge the upper case
up++;
else // judge the lower case
low++;
ch2=tolower(ch); // To lower case
frequency(ch2);
}
else
;
total++;
}while(!istr.eof());
}
//*****************************************************************
void print()
{
int Count=0;
cout<<"Letter Frequencies:"<<endl;
for(int i=0;i<26;i++)
{ if(fre[i]>Count)
cout<<char(toupper(str[i]))<<setw(36)<<fre[i]<<endl;
cout<<"Rest of Letters"<<endl;
cout<<"Upper Case"<<endl;
cout<<up<<endl;
cout<<"Lower Case"<<endl;
cout<<low<<endl;
cout<<"White Space"<<endl;
cout<<wsp<<endl;
cout<<"Total char"<<endl;
cout<<total<<endl;
}
system("pause");
}
#include<fstream>
#include<cctype>
#include<iomanip>
#include<cstdlib>
#define Maxpath 80 // The maximum name of the file
using namespace std;
int fre[26]; // The times of each char
const char str[]="abcdefghijklmnopqrstuvwxyz"; // Alphabet
static int up=0,low=0,wsp=0,total=0;
void frequency(char ch);
void count(char file_name[]);
void print();
int main()
{
char filename[Maxpath];
cout<<"Please enter the file_name"<<endl;
cin.getline (filename,Maxpath); // get a file name
count(filename);
print();
return 0;
}
//************************************************
void frequency(char ch)
{
for(int i=0;i<26;i++)
if(ch==str[i])
{
fre[i]++;
break;
}
}
//************************************************************
void count(char filename[])
{
fstream istr;
char ch,ch2;
istr.open (filename,ios::in);
if(istr.fail ())
{
cout<<"Can't open!"<<endl;
exit(1);
}
do
{
ch=istr.get ();
if(isspace(ch)) // judge the write space or not
wsp++;
else if(isalpha(ch)) // judge the Alphabet or not
{
if(isupper(ch)) // judge the upper case
up++;
else // judge the lower case
low++;
ch2=tolower(ch); // To lower case
frequency(ch2);
}
else
;
total++;
}while(!istr.eof());
}
//*****************************************************************
void print()
{
int Count=0;
cout<<"Letter Frequencies:"<<endl;
for(int i=0;i<26;i++)
{ if(fre[i]>Count)
cout<<char(toupper(str[i]))<<setw(36)<<fre[i]<<endl;
cout<<"Rest of Letters"<<endl;
cout<<"Upper Case"<<endl;
cout<<up<<endl;
cout<<"Lower Case"<<endl;
cout<<low<<endl;
cout<<"White Space"<<endl;
cout<<wsp<<endl;
cout<<"Total char"<<endl;
cout<<total<<endl;
}
system("pause");
}