2

对于我的 CS 课程,我们被分配了使用此处的文本文件创建英语到西班牙语翻译的任务: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 . . .

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

4

1 回答 1

2

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.

于 2011-11-15T18:51:29.553 回答