结对对象:石莉静  

 

  • [必做 1] 基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 (第一阶段初稿完成该要求)

 

源程序:

#include <iostream>
#include <vector>
#include <algorithm> 
#include <string>
#include <fstream>
using namespace std;

struct WORD
{
    string word;
    int num;
};

vector<WORD> a;  //创建vector对象,a[]

int&value(const string&s)
{
    for(int i=0;i<a.size();i++)
        if(s==a[i].word)
            return a[i].num;
        WORD p;
        p.word=s;
        p.num=0;
        a.push_back(p);  //在数组a最后添加数据
        return a[a.size()-1].num;
}

int main()
{
    string str;
    cout << "输入字符串:\n";
    char c;
    while(c=cin.get())
    {
        if((c>='a' && c<='z') || (c>='A' && c<='Z') || c==' ' || c=='\n')  
            str+=c;   //去除符号
        if(c=='\n')
            break;
    }
//输出去掉非英文字符的字符串
        
    for(int j=0;str[j]!='\0';j++)
    {
        if(str[j]>='A'&&str[j]<='Z')
        {
            str[j]+= 32;  //大写字母Ascll码+32转换为小写
        }
    }
 //输出转换为小写的字符串

    string buf;
    ofstream fout("D:\\123.txt");  //把转换好的字符串写入文件
    fout<<str;
    fout.close ();
    ifstream fin("D:\\123.txt");  //读出写入的字符串并排序
    while(fin>>buf){
        value(buf)++;
    }
    vector<WORD>::const_iterator p;  //迭代器访问元素
    ofstream output("D:\\Result1.txt");  //定义输出文件名
    for(p=a.begin();p!=a.end();++p)
        output<<p->word<<":"<<p->num<<'\n';  //将结果输出保存到Result1.txt中
    return 0;
}

运行结果:

(只截取了部分Result1文件内容)

 

结对编程方便了我们对于问题的讨论,让学习更有动力,两个人可以互相督促,但同时也增加了问题,意见的分歧以及对对方的无法理解都是需要我们去沟通去努力解决的。

虽然我们在这个程序上花了不少时间,也查阅了不少资料,但是仍然没能完整的解决这个问题。对于排序问题我们想用冒泡排序,也查阅了资料,虽然加了代码之后没有错误也可以运行,但是它并没有排序……然后改了半天也没啥反应,无奈放弃了。

 

posted on 2016-03-22 22:08  陈晖丶  阅读(194)  评论(4编辑  收藏  举报