第14周-输入输出流,文本文件-项目1 - 小玩文件(2)

来源:互联网 发布:超市扫码枪软件费用 编辑:程序博客网 时间:2024/05/17 20:11

/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作    者:刘畅* 完成日期:2015 年 6  月  6 日 * 版 本 号:v1.0 * * 问题描述:(3)用键盘输入文件名,统计输出文件中每个字母、数字字符出现的次数;            (4)用键盘输入文件名,统计输出类似下面的数据(每个汉字2字节,每字节最高位为1)* 输入描述:;* 程序输出::

(3)代码如下:

#include <iostream>#include <cstdlib>#include <fstream>#include <cstring>using namespace std;int main(){    fstream file;    file.open("abc.txt", ios::in);    if(!file)    {        cout<<"abc.txt can‘t open."<<endl;        exit(1);    }    int n=0,i,a[129];    char m;    memset(a,-1,sizeof(a));    file.get(m);    while(!file.eof())    {        a[(int)m]++;        n++;        file.get(m);    }    for (i=0; i<129; i++)    {        if (a[i]!=-1)            cout<<(char)i<<":"<<(a[i]+1)<<endl;    }    file.close();    return 0;}

运行结果:




(4)

#include <iostream>#include <cstdlib>#include <fstream>#include <cstring>#include <cstdio>using namespace std;int main(){    ifstream file;    file.open("abc.txt", ios::in);    if(!file)    {        cout<<"abc.txt can‘t open."<<endl;        exit(1);    }    int n=0,k=0;    char m;    file.get(m);    while(!file.eof())    {        n++;        if (m==' ')            k++;        file.get(m);    }    file.close();    cout<<"字符数(含空格:):"<<n<<endl;    cout<<"空格数:"<<k<<endl;    cout<<"字符数(不含空格:):"<<n-k<<endl;    return 0;}

学习心得:

(3)自己做的时候出了点问题,最后一个空字符对数据统计造成了影响,请教了贺老师后修改了读取方式,对了。

(4)并没有做全,汉字的读取虽然现在了解了一点,但还是没有选择去统计,而不知道是什么原因,cout中引号内的中文全变成了乱码,而在别人的电脑上运行则可以,猜想可能是编译器的原因。


1 0
原创粉丝点击