注册 登录
编程论坛 C++教室

如何改进这个问题???

orleans 发布于 2010-04-10 03:37, 525 次点击
题目是,读取一个文件夹的字母,然后把每个字母的频率计算出来,还有write space, 大小写,总数都得计算。
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");
}
8 回复
#2
orleans2010-04-10 09:34
ding~~~
#3
yyblackyy2010-04-10 10:20
int Count=0;
cout<<"提示。。。。";                    //这个功能好像我在帖子里注释过哦
cin>>Count;                         //字符出现的个数大于Count时才显示该字符的个数      
cout<<"Letter Frequencies:"<<endl;            
for(int i=0;i<26;i++)                     
{    if(fre[i]>Count)   
#4
orleans2010-04-10 12:44
回复 3楼 yyblackyy
还是不可以

我的意思是cout只输入一次文件名

然后直接出来output, output只要显示有字母的,不要26个字母都显示

谢谢
改如何改进?
#5
pangding2010-04-11 10:34
没仔细看你的程序。没出的不显示,你输出之前看看个数是不是 0 不就行了吗?
#6
orleans2010-04-11 11:12
回复 5楼 pangding
如何做出 那样的效果?
请给代码
#7
yyblackyy2010-04-11 13:51
楼主真的运行了代码吗???不负责任啊
#8
orleans2010-04-12 00:58
回复 7楼 yyblackyy
我有学习。。。。
#9
judking2010-04-12 19:58
ding ge~
1