| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 788 人关注过本帖
标题:如何实现下面的程序
只看楼主 加入收藏
mcdog
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-10-29
收藏
 问题点数:0 回复次数:2 
如何实现下面的程序
1. 用面向对象语言实现:
(1)类Dictionary:从文件dictionary.txt 中读入给定的英文单词并保存在其数据成员中;dictionary.txt 中每行有多个单词,用“;”加以分隔,每个单词最多16个字符;单词不跨行,因此每行的长度可能不一样,最多为n 个字符;该文件最多有m 行,实际行数可以不等于m;n 和m 均在产生Dictionary 的实例时由调用方给出;Dictionary 对外提供成员函数boolean search(String w),使得w 在这些单词中时返回true,否则返回false。
(2)一段测试程序:从命令行给出n 和m;产生Dictionary 的一个实例dic;从键盘读入单词word;输出dic.search(word) 的结果。(选做:改从指定文件中读入多个单词,分别输出相应的search 结果)

刚刚接触编程没几天啊,可是老师竟然催着交作业,在规定的时间不交就没有成绩了,可是现在一点思路都没有啊!!高手帮帮忙吧!!
搜索更多相关主题的帖子: Dictionary 面向对象 单词 dictionary 
2006-10-29 23:17
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
Dictionary.h

#ifndef H_DICTIONARY
#define H_DICTIONARY

#include <string>
#include <set>
#include <fstream>
#include <algorithm>

#define FILTER ";"//用于分隔单词的字符,可以更改,比如添加空格"; "

class Dictionary
{
public:
    Dictionary(){};

    //直接从词典文件构造
    explicit Dictionary(const std::string& filename,const std::string& filter=FILTER)
    {
        readfile(filename,filter);
    }
    
    //读取词典文件
    void read(const std::string& filename,const std::string& filter=FILTER)
    {
        dict.empty();
        readfile(filename,filter);
    }

    bool search(const std::string& word);

private:
    void readfile(const std::string& filename,const std::string& filter);//由于read和构造函数内容相近,提取出来作为一个单独的函数供其调用

private:
    std::set<std::string> dict;//使用set确保没有重复的单词
};

bool Dictionary::search(const std::string& word)//使用标准库find算法查找
{
    return (std::find(dict.begin(),dict.end(),word)!=dict.end());
}

void Dictionary::readfile(const std::string& filename,const std::string& filter)
{

    std::ifstream file(filename.c_str(),std::ios::out);

    //打开文件失败,退出
    if(!file)
    {
        std::cerr<<"Can't read dictionary from "<<filename<<std::endl;
        exit(-1);
    }

    std::string line;
    while(std::getline(file,line))//读取一行
    {
        std::string::size_type pos=0;
        std::string::size_type pre_pos=pos;

        std::string word;

        //查找分隔符,以界定单词
        while((pos=line.find_first_of(filter,pos))!=std::string::npos)
        {
            word=line.substr(pre_pos,pos-pre_pos);
            if(word.size())
                dict.insert(word);
            pre_pos=++pos;
        }

        //处理行末单词之后没有分隔符的情况
        word=line.substr(pre_pos,std::string::npos);
        if(word.size())
            dict.insert(word);
    }
}


#endif

main.cpp


#include <iostream>
#include "dictionary.h"

int main(int argc, char* argv[])
{
    std::string filename;
    std::cout<<"Please input the dictionary file name:";
    std::cin>>filename;

    //直接从文件生成词典
    Dictionary dic(filename);

    std::string word;
    std::cout<<"Please input the word to be searched:";
    std::cin>>word;

    if(dic.search(word))
    {
        std::cout<<'\"'<<word<<"\" have been found in the dictionary.\n";
    }
    else
    {
        std::cout<<"Can't find the word in the dictionary.\n";
    }

    return 0;
}

[此贴子已经被作者于2006-10-30 17:41:51编辑过]


2006-10-30 17:24
ztj111
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-7-18
收藏
得分:0 

好像和C++ primer上的第六章的程序差不多哦

2006-10-31 13:58
快速回复:如何实现下面的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023710 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved