创建翻译器;需要从文本文件正确插入

发布于 2024-12-16 01:41:50 字数 3266 浏览 1 评论 0原文

对于我的计算机科学课程,我们被分配了使用此处的文本文件创建英语到西班牙语翻译器的作业:http://www.ilovelanguages.com/IDP/files/Spanish.txt

到目前为止,我已经插入了内容,但是当我运行该程序时 - 单词会混合在一起并且我是打赌txt文件每行都有多个单词这一事实才是破坏它

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class transl
      { private:
            class word
                  { public:
                        string eng_word,
                               spa_word;
                        word * next,
                             * prev;    

                        word(string engl_word, string span_word, word * next1 = NULL, word * prev1 = NULL)
                        {   eng_word = engl_word;
                            spa_word = span_word;
                            next = next1;
                            prev = prev1;   }
                  };

            word * head,
                 * tail;

        public:
            transl::transl()
            {   head = NULL;
                tail = NULL;    }

            transl::~transl()
            { word * wordPtr, * nextWord; 
                     wordPtr = head;

                     if(head == NULL)
                        return;

                     while(wordPtr != NULL)
                          { nextWord = wordPtr->next;
                            delete wordPtr;
                            wordPtr = nextWord; }
            }

            void insert(string engl_word, string span_word)
            {   if(head == NULL)
                   head = new word(engl_word, span_word);

                    else
                    {   word * wordPtr;
                        wordPtr = head;

                        while(wordPtr->next != NULL)
                        {     wordPtr->next->prev = wordPtr;
                              wordPtr = wordPtr->next;  }

                        wordPtr->next = new word(engl_word, span_word);
                        cout << wordPtr->next->eng_word << "    " << wordPtr->next->spa_word << endl; } 
            }

            string search(string engl_word)
            {   return "Hello"; }

            };

int main()
    {   ifstream inFile;    inFile.open("dictionary.txt");
        string engl_word, span_word;
        transl test;

            if (!inFile)
            {   cout << "ERROR! CANNOT LOCATE DICTIONARY!" << endl;
                system("pause");
                return 1;   }

            cout << "Loading Dictionary:  ";

                while(inFile >> engl_word >> span_word)
                {   test.insert(engl_word, span_word);  }

            cout << "Done!" << endl;
            system("pause");
            system("CLS");

            cout << "Translate: ";
             cin >> engl_word;

            cout << "The translation of " << engl_word << " is " << test.search(engl_word) << '.' << endl;

            system("pause");
        return 0;   }

的一个例子,它看起来像这样:

peor    wound
la    herida
wounded    herida
Done!
Press any key to continue . . .

有人知道正确插入值的过程吗

For my CS class, we've been given an assignment to create an english to spanish translator using the text file from here: http://www.ilovelanguages.com/IDP/files/Spanish.txt

So far, I've got the the insertion down, but when I run the program - words get mingled up and I'm betting that the fact the txt file has multiple words for each line is what's ruining it

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class transl
      { private:
            class word
                  { public:
                        string eng_word,
                               spa_word;
                        word * next,
                             * prev;    

                        word(string engl_word, string span_word, word * next1 = NULL, word * prev1 = NULL)
                        {   eng_word = engl_word;
                            spa_word = span_word;
                            next = next1;
                            prev = prev1;   }
                  };

            word * head,
                 * tail;

        public:
            transl::transl()
            {   head = NULL;
                tail = NULL;    }

            transl::~transl()
            { word * wordPtr, * nextWord; 
                     wordPtr = head;

                     if(head == NULL)
                        return;

                     while(wordPtr != NULL)
                          { nextWord = wordPtr->next;
                            delete wordPtr;
                            wordPtr = nextWord; }
            }

            void insert(string engl_word, string span_word)
            {   if(head == NULL)
                   head = new word(engl_word, span_word);

                    else
                    {   word * wordPtr;
                        wordPtr = head;

                        while(wordPtr->next != NULL)
                        {     wordPtr->next->prev = wordPtr;
                              wordPtr = wordPtr->next;  }

                        wordPtr->next = new word(engl_word, span_word);
                        cout << wordPtr->next->eng_word << "    " << wordPtr->next->spa_word << endl; } 
            }

            string search(string engl_word)
            {   return "Hello"; }

            };

int main()
    {   ifstream inFile;    inFile.open("dictionary.txt");
        string engl_word, span_word;
        transl test;

            if (!inFile)
            {   cout << "ERROR! CANNOT LOCATE DICTIONARY!" << endl;
                system("pause");
                return 1;   }

            cout << "Loading Dictionary:  ";

                while(inFile >> engl_word >> span_word)
                {   test.insert(engl_word, span_word);  }

            cout << "Done!" << endl;
            system("pause");
            system("CLS");

            cout << "Translate: ";
             cin >> engl_word;

            cout << "The translation of " << engl_word << " is " << test.search(engl_word) << '.' << endl;

            system("pause");
        return 0;   }

an example of what it looks like is this:

peor    wound
la    herida
wounded    herida
Done!
Press any key to continue . . .

Would anyone know the procedures to insert the values properly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-12-23 01:41:50

wordPtr->next->prev = wordPtr; 是多余且令人困惑的。您正在搜索列表的末尾,但在此过程中对其进行修改。

您忘记在插入中写入新单词的上一个指针。

然而,真正的问题是在读字典上。当您执行inFile>>时engl_word>> span_word,读入两个空格限制的列表。但是,西班牙语条目不受空格限制,而是占据该行的其余部分。虽然您可以使用 >> 读取英语单词,但您需要使用 getline( cin, span_word ) 来解析西班牙语表达式。

wordPtr->next->prev = wordPtr; is superfluous and confusing. You are searching the end of the list, but modifying it in the process.

You are forgetting to write the prev pointer of the new word in insert.

However, the real problem is in reading the dictionary. When you perform inFile >> engl_word >> span_word, two whitespace-limited lists are read in. However, the spanish entries are not whitespace-limited, but take up the rest of the line. While you can read the english word with >>, you'll need something like getline( cin, span_word ) to parse the spanish expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文