seiraansai 2022-06-15 14:54 采纳率: 50%
浏览 45
已结题

大伙我这代码为什么读取不了结果啊(把文件里的内容放到链表里进行查询)

#include
#include
#include
using namespace std;
class ChainNode {
friend class dictionary;
private:
string data[2];
ChainNode* next;
};
class dictionary
{
public:
dictionary() { first = NULL; }
~dictionary() {};
void chazhaoyingwen();
void chazhaozhongwen();
void charu();
void shanchu();
private:
ChainNode* first; //指向第一个结点的指针
static int counts; //记录存储的元素个数
};
int dictionary::counts = 0;
void dictionary::charu()
{
string chinese[100];
string english[100];
fstream myFile;
myFile.open("小型英汉词典.txt", ios::out | ios::app);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
while (1) {
cout << "请输入所要插入的单词的中文(输入退出即可退出):" << endl;
cin >> chinese[counts];
if (chinese[counts] == "退出")
{
break;
}
cout << "请输入所要插入的单词的英文:" << endl;
cin >> english[counts];
myFile << chinese[counts] << "\t"
<< english[counts] << endl;
counts++;
}
myFile.close();
}
void dictionary::chazhaoyingwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
myFile.seekg(0);

ChainNode* p;
string a;
string b;
string c;
first = new ChainNode;
first->next = NULL;
while (0) {
    myFile >> a;
    myFile >> b;
    p = new ChainNode;
    p->data[0] = a;
    p->data[1] = b;
    p->next = first->next;
    first->next = p;
    counts = counts - 1;
    if (counts == 0)
    {
        break;
    }
}
myFile.close();
p = first;
cout << "请输入要查找的单词的中文:" << endl;
cin >> c;

while (p->next != NULL)
{
if (p->data[0] ==c)
{
cout << "查找的单词的英文为:" << p->data[1] << endl;
break;
}
p = p->next;
}
if (p->next == NULL)
{
cout << "未查找到该单词" << endl;
}
}
void dictionary::chazhaozhongwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
myFile.seekg(0);

ChainNode* p;
string a;
string b;
string c;
first = new ChainNode;
first->next = NULL;
while (0) {
    myFile >> a>>b;
    p = new ChainNode;
    p->data[0] = a;
    p->data[1] = b;
    p->next = first->next;
    first->next = p;
    counts = counts - 1;
    if (counts == 0)
    {
        break;
    }
}
myFile.close();
p = first;
cout << "请输入要查找的单词的英文:" << endl;
cin >> c;
while (p->next != NULL)
{
    if (p->data[1] == c)
    {
        cout << "查找的单词的中文为:" << p->data[0] << endl;
        break;
    }
    p = p->next;
}
if (p->next == NULL)
{
    cout << "未查找到该单词" << endl;

}
}

void main()
{
dictionary a;
a.charu();
a.chazhaoyingwen();
}

  • 写回答

1条回答 默认 最新

  • 暴风雨中的白杨 2022-06-15 17:38
    关注

    查找英文 和查找中文 那里写成了 while(0)

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 赞助了问题酬金5元 6月16日
  • 创建了问题 6月15日

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)