第13周-项目1-小玩文件-用键盘输入文件名,统计输出类似下面的数据

来源:互联网 发布:720ccav新域名是什么 编辑:程序博客网 时间:2024/06/04 18:39
问题及代码:

/*   *Copyright (c)2015,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:File.cpp   *作    者:单昕昕   *完成日期:2015年6月8日   *版 本 号:v1.0   *问题描述:用键盘输入文件名,统计输出类似下面的数据。*程序输入:文件读取。*程序输出:文件中各种字符出现的次数。*/ #include <iostream>#include <cstdio>#include <cstdlib>//为了使用exit()#include <fstream>using namespace std;int main(){    char ch;    int count1=0,count2=0,count3=0;//分别用来统计字符、空格、中文    FILE *fp=NULL;    char fname[50];//用于存放文件名    cout<<"请输入文件名(请注意加上文件名后缀):";    gets(fname);    fp=fopen(fname,"r");//只供读取    if(fp==NULL)//如果失败了    {        cout<<"文件打开错误!";        exit(1);//中止程序    }    while((ch=getc(fp))!=EOF)    {        putchar(ch);        count1++;        if(ch==' ')            count2++;        else if(ch>0xA0)            count3++;    }    fclose(fp);//关闭文件    fp=NULL;//需要指向空,否则会指向原打开文件地址    cout<<endl;    cout<<"统计信息————————————————————————"<<endl;    cout<<"字符数(不计空格):"<<count1<<endl;    cout<<"字符数(计空格):"<<count1+count2<<endl;    cout<<"中文字符:"<<count3<<endl;    return 0;}


运行结果:


知识点总结:
中文字符是从0xA0开始的。


学习心得:

不知道为什么中文字符出不来阿!!!!!!

I need help!!!!!!


修正后的代码:

/*   *Copyright (c)2015,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:File.cpp   *作    者:单昕昕   *完成日期:2015年6月8日   *版 本 号:v2.0   *问题描述:用键盘输入文件名,统计输出类似下面的数据。*程序输入:文件读取。*程序输出:文件中各种字符出现的次数。*/ #include <iostream>#include <cstdio>#include <cstdlib>//为了使用exit()#include <fstream>using namespace std;int main(){    unsigned ch;    int count1=0,count2=0,count3=0;//分别用来统计字符、空格、中文    FILE *fp=NULL;    char fname[50];//用于存放文件名    cout<<"请输入文件名(请注意加上文件名后缀):";    gets(fname);    fp=fopen(fname,"r");//只供读取    if(fp==NULL)//如果失败了    {        cout<<"文件打开错误!";        exit(1);//中止程序    }    while((ch=getc(fp))!=EOF)    {        putchar(ch);        count1++;        if(ch==' ')            count2++;        else if(ch>0xA0&&ch!=0)            count3++;    }    fclose(fp);//关闭文件    fp=NULL;//需要指向空,否则会指向原打开文件地址    cout<<endl;    cout<<"统计信息————————————————————————"<<endl;    cout<<"字符数(不计空格):"<<count1<<endl;    cout<<"字符数(计空格):"<<count1+count2<<endl;    cout<<"中文字符:"<<count3<<endl;    return 0;}

运行结果:



错误点:

ch类型不正确。


修正代码:

/**Copyright (c)2015,烟台大学计算机与控制工程学院*All rights reserved.*文件名称:File.cpp*作    者:单昕昕*完成日期:2015年6月8日*版 本 号:v3.0*问题描述:用键盘输入文件名,统计输出类似下面的数据。*程序输入:文件读取。*程序输出:文件中各种字符出现的次数。*/#include <iostream>#include <cstdio>#include <cstdlib>//为了使用exit()#include <fstream>using namespace std;int main(){    int ch,count1=0,count2=0,count3=0;//分别用来统计字符、空格、中文    FILE *fp=NULL;    char fname[50];//用于存放文件名    cout<<"请输入文件名(请注意加上文件名后缀):";    gets(fname);    fp=fopen(fname,"r");//只供读取    if(fp==NULL)//如果失败了    {        cout<<"文件打开错误!";        exit(1);//中止程序    }    while((ch=getc(fp))!=EOF)    {        putchar(ch);        count1++;        if(ch==' ')            count2++;        else if(ch>0xA0&&ch!=0)            count3++;    }    fclose(fp);//关闭文件    fp=NULL;//需要指向空,否则会指向原打开文件地址    cout<<endl;    cout<<"统计信息————————————————————————"<<endl;    cout<<"字符数(不计空格):"<<count1<<endl;    cout<<"字符数(计空格):"<<count1+count2<<endl;    cout<<"中文字符:"<<count3/2<<endl;    return 0;}

运行结果:



错误原因:

一个汉字占两个字节,所以要除2.


0 0
原创粉丝点击