| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 410 人关注过本帖
标题:【求助】有道题目想让大家忙忙查一下错
只看楼主 加入收藏
dony989
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-12-1
收藏
 问题点数:0 回复次数:0 
【求助】有道题目想让大家忙忙查一下错
题目是
   设计一个文件拼写检查器(必须使用类来设计),给定一个单词字典(我会提供),程序需要把字典读入(可以使用string的数组或者string的vector),并把一篇文本文件中不认识的单词都打印出来,更好的做法为文本中不认识的单词找到正确的单词并替换掉原来的单词。测试用的文本我也提供。报告包括: 问题描述,类定义及其说明,类的实现,测试程序,程序运行结果。
    提供的字典在:dict.txt中,而测试用的文本为bigpal.txt。
然后写的头文件CDictionary.h
#include<iostream>
using namespace std;
class CDictionary
{
    private:
        int number;
        string *dict;
        inline int abso(int a) { return a < 0 ? -a : a; }
        inline int mini(int a, int b) { return a > b ? a : b; }
        int Unsame(string a, string b)
        {
            int uns = 0;
            for (int i = 0; i < mini(a.length(), b.length()); i++)
            {
                if (a[i] - b[i]) uns++;
            }
            return uns + abso(a.length() - b.length());
        }
        inline char uc(char src)
        {
            if (src >= 'a' && src <= 'z') return src - 'a' + 'A';
            return src;
        }
        string u(string src)
        {
            string ret = src;
            for (int i = 0; i < ret.length(); i++)
            {
                ret[i] = uc(ret[i]);
            }
            return ret;
        }
    public:
        CDictionary(int dn, string *d)
        {
            number = dn;
            dict = new string [dn];
            for (int i = 0; i < dn; i++) { dict[i] = u(d[i]); }
        }
        ~CDictionary() { delete[]dict; }
        bool InDict(string wrd)
        {
            string word = u(wrd);
            for (int i = 0; i < number; i++)
            {
                if (word == dict[i]) return true;
            }
            return false;
        }
};
最后是cpp  CDictionarySample.cpp
#include "CDictionary.h"
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string dict_words[] = { "i", "am", "a", "good", "sheep", "she", "it", "he", "is" };
    CDictionary *dict = new CDictionary(9, dict_words);
    string str = "It is a goood sheeps.";
    char *st = new char [str.length() + 1];
    strcpy(st, str.c_str());
    for (int i = 0; st[i]; i++)
    {
        if (!((st[i] >= 'a' && st[i] <= 'z') || (st[i] >= 'A' && st[i] <= 'Z')))
        {
            st[i] = ' ';
        }
    }
    stringstream ss(st);
    while (ss >> str)
    {
        if (! dict->InDict(str))
        {
            cout << "错误的拼写: " << str << endl;
        }
    }
    system("pause");
    delete[]st;
    delete dict;
    return 0;
}


有问题  大家帮忙查下错好吗?或者大概在cpp 或者  h  中标下内容的含义  谢谢
搜索更多相关主题的帖子: 文本文件 字典 
2009-12-01 18:11
快速回复:【求助】有道题目想让大家忙忙查一下错
数据加载中...
 
   



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

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