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日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。