| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 558 人关注过本帖
标题:字典程序,但是还有一个小问题,希望大家帮帮小弟,先谢谢大家了。。。
只看楼主 加入收藏
jinmyan
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-1-6
结帖率:0
收藏
 问题点数:0 回复次数:2 
字典程序,但是还有一个小问题,希望大家帮帮小弟,先谢谢大家了。。。
#include "iostream.h"
#include "fstream.h"
#include "string.h"
#define MAX 100
struct WORD
{
    char english[10];
    char chinese[20];
};
class dic
{
private:
    WORD word[MAX];
public:
    int n;
    dic();
    void input();
    void check();
    void alter();
    void show();
};
dic::dic()
{
    n=1;
}
void dic::input()
{
    cout<<"english:";
    cin>>word[n].english;
    cout<<"chinese:";
    cin>>word[n].chinese;
    ofstream file("dictionary.dat",ios::out|ios::app);
    file.write((char*)&word[n],sizeof(WORD));
    n++;
}
void dic::check()
{
    int j=1;
    bool isfind=false;
    char checkname[10];
    cout<<"checkname;";
    cin>>checkname;
    ifstream checkfile("dictionary.dat");
    if(!checkfile)cout<<"open error!";
    while(checkfile.read((char*)&word[j],sizeof(WORD)))
    {
        if(strcmp(checkname,word[j].english)==0)
        {
            cout<<"chinese:"<<word[j].chinese;
            isfind=true;
        }
        else j++;
    }
    if(!isfind)cout<<"没有找到!"<<endl;
}
void dic::alter()
{
    int i;
    char alterword[10],alterchinese[20];
    bool isfind=false;
    cout<<"请输入你想修改的单词:";
    cin>>alterword;
    ifstream checkfile("dictionary.dat");
    if(!checkfile)cout<<"error!"<<endl;
    while(checkfile.read((char*)&word[i],sizeof(WORD)))
    {
        if(strcmp(alterword,word[i].english)==0)
        {
            cout<<"你修改的单词中文意思:";
            cin>>alterchinese;
            strcpy(alterchinese,word[i].chinese);
            isfind=true;
        }
        i++;
    }
    if(!isfind)cout<<"没有这个单词!"<<endl;
}
void dic::show()
{
    int j=1;
    ifstream infile("dictionary.dat");
    if(!infile)cout<<"open error!"<<endl;
    while(infile.read((char*)&word[j],sizeof(WORD)))
    {
        cout<<word[j].english<<word[j].chinese<<endl;
        j++;
    }
}
void main()
{
    dic dictionary;
    int N,m=1;
    cout<<"...............字典..........................."<<endl;
    cout<<"1..............输入单词......................."<<endl;
    cout<<"2..............查找单词......................."<<endl;
    cout<<"3..............修改单词......................."<<endl;
    cout<<"4..............显示字典库....................."<<endl;
    cout<<"5..............退出..........................."<<endl;
    while(m)
    {
        cin>>N;
        switch(N)
        {
           case 1:  dictionary.input();break;
           case 2:  dictionary.check();break;
           case 3:  dictionary.alter();break;
           case 4:  dictionary.show();break;
           case 5:  m=0;break;
        }
    }
}
程序编译的时候没有问题,但是在运行的时候有这样的错误。运行入下:
1
english: go
chinese: 拜拜
1
english:work
chinese:工作
4
go拜拜
work工作
3
请输入你想修改的单词:go
你修改的单词中文意思:走
4
go拜拜
work工作

我们可以看到:我修改了go的中文意思,但是在显示的时候go的中文意思没有改变。我这点不清楚,为什么会这个样子呢?谢谢大家指点!
搜索更多相关主题的帖子: 字典 
2010-01-15 20:15
jinmyan
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-1-6
收藏
得分:0 
快来人帮我解决吧,呜呜,,
2010-01-17 16:19
jinmyan
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-1-6
收藏
得分:0 
哈哈,我自己解决了,呼呼。。
#include "fstream.h"
#include "string.h"
#define MAX 1000
struct WORD
{
    char english[10];
    char chinese[20];
};
class dic
{
private:
    WORD word[MAX];
public:
    int n;
    dic();
    void input();
    void input(char english[10]);
    void alter();
    void check();
    void show();
};
dic::dic()
{
    n=1;
}
void dic::input(char english[10])
{
    ofstream file("dictionary.dat",ios::in|ios::app);
    if(!file)return;
    WORD word;
    strcpy(word.english,english);
    cout<<"chinese:";
    cin>>word.chinese;
    file.write((char*)&word,sizeof(WORD));
    n++;
}
void dic::input()
{
    int i;
    bool isfind=false;
    ofstream file("dictionary.dat",ios::in|ios::app);
    if(!file)return;
    cout<<"english:";
    cin>>word[n].english;
    for(i=1;i<n;i++)
    {
        if(strcmp(word[n].english ,word[i].english )==0)isfind=true;
    }
    if(isfind)
    {
        cout<<"已经有这个单词!"<<endl;
        return;
    }
    else
    {
        cout<<"chinese:";
        cin>>word[n].chinese;
        file.write((char*)&word[n],sizeof(WORD));
        n++;
    }
    file.close();
}
void dic::check()
{
    bool isfind=false;
    int i=1;
    char checkenglish[10];
    cout<<"check english:";
    cin>>checkenglish;
    ifstream file("dictionary.dat",ios::in);
    if(!file)return;
    while(file.read((char*)&word[i],sizeof(WORD)))
    {
        if(strcmp(checkenglish,word[i].english)==0)
        {
            isfind=true;
            cout<<"chinese:"<<word[i].chinese<<endl;
        }
        i++;
    }
    if(!isfind)
    {
        cout<<"没有这个单词!单词将输入字典库!"<<endl;
        input(checkenglish);
    }
}
void dic::alter()
{
    int i,item;
    long offset;
    WORD alter;
    cout<<"请输入英文:";
    cin>>alter.english;
    cout<<"请输入修该的中文:";
    cin>>alter.chinese;
    for(i=1;i<=n;i++)
    {
        if(strcmp(alter.english,word[i].english)==0)item=i;
    }
    fstream file("dictionary.dat",ios::in|ios::out);
    if(!file)return;
    offset=(item-1)*sizeof(WORD);
    file.seekp(offset);
    file.write((char*)&alter,sizeof(WORD));
}
void dic::show()
{
    int i=1;
    ifstream file("dictionary.dat",ios::out);
    if(!file)return;
    cout<<"english"<<".................."<<"chinese"<<endl;
    while(file.read((char*)&word[i],sizeof(WORD)))
    {
        cout<<word[i].english<<"................."<<word[i].chinese<<endl;
        i++;
    }
}
void main()
{
    dic dictionary;
    cout<<"............................字典..................................."<<endl;
    cout<<"1..........................输入单词................................"<<endl;
    cout<<"2..........................查找单词................................"<<endl;
    cout<<"3..........................显示词典................................"<<endl;
    cout<<"4..........................修改单词................................"<<endl;
    cout<<"5...........................退出..................................."<<endl;
    int i,j=1;
    while(j)
    {
       cout<<"请选择操作:";
       cin>>i;
       switch(i)
       {
         case 1:dictionary.input();break;
        case 2:dictionary.check();break;
        case 3:dictionary.show();break;
        case 4:dictionary.alter();break;
        case 5:j=0;break;
       }
    }
}
2010-01-20 12:10
快速回复:字典程序,但是还有一个小问题,希望大家帮帮小弟,先谢谢大家了。。。 ...
数据加载中...
 
   



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

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