2016-04-23 66 views
0

名字和RUID從read.txt閱讀,如果它在 read.txt找到字符串「RUID」,你需要創建一個新的LinkedList存儲以下 RUID和名稱(有多個RUID在字符串),讀完 所有read.txt元素,使用操作符重載+連接 linkedlists在一起如何通過C++檢查文本文件中每行的特定單詞?

這是什麼正在問我的。我已經知道如何打開文本文件並設法做其他事情,包括操作符重載。我只是困惑,我應該如何去嘗試從文本文件中獲取信息並將其存儲到單獨的鏈接列表中。

這是我的僞代碼(我只是不知道如何相當實現它)

while (!File.eof()) 
{ 
if (a string in a line = RUID) 
{ 
make a new LinkedList and set as current LinkedList 
jump to next line 
read line into the LinkedList 
} 
} 

這是我的文本文件(忽略空格之間每行)

RUID名稱

4325名1

RUID名稱

5432名2

6530 NAME3

RUID名稱

1034 NAME4

2309 NAME5

,這是我的代碼迄今

#include "LinkedList.h" 
#include <string> 
#include <algorithm> 
#include <iostream> 
#include <time.h> 
#include "stdlib.h" 
#include <cmath> 
#include <cstdlib> 
#include <fstream> 
using namespace std; 

int main() 
{ 
    LinkedList x; 
    string line; 
    ifstream ReadFile; 
    ReadFile.open("read.txt"); 
    int counter = 0; 
    if (ReadFile.is_open()) 
    { 
     while (!ReadFile.eof()) 
     { 

     } 
    } 

    ofstream WriteFile("Write.txt"); 
    if (WriteFile.is_open()) 
    { 
     Node* extra; 

     int inf = 9; 
     while (inf != 10) 
     { 
      cout << "Select the following options:" << endl; 
      cout << "1. Add node to the list" << endl; 
      cout << "2. Remove node from list" << endl; 
      cout << "3. Print list" << endl; 
      cout << "4. Print element of the list" << endl; 
      cout << "5. Sort the list" << endl; 
      cout << "6. Quit" << endl; 
      int option; 
      cin >> option; 
      cout << "Selection: " << option << endl; 

      if (option == 1) 
      { 
       cout << "Enter student name: "; 
       string insert; 
       cin.ignore(); 
       getline(cin, insert); 

       int temp = rand() % 9000 + 1000; 

       extra = new Node(insert, temp); 
       x.addNode(extra); 
       cout << endl; 
       x.printList(); 
      } 
      else if (option == 2) 
      { 
       cout << "Enter the RUID to be remove: "; 
       int remove; 
       cin >> remove; 
       cout << endl; 

       x.removeNode(remove); 
       x.printList(); 
      } 
      else if (option == 3) 
      { 
       x.printList(); 
      } 
      else if (option == 4) 
      { 
       cout << "Enter the index: "; 
       int index; 
       cin >> index; 
       cout << endl; 

       x.printElement(index); 
      } 
      else if (option == 5) 
      { 
       x.sortList(); 
       x.printList(); 
      } 
      else if (option == 6) 
      { 
       break; 
      } 
      else 
      { 
       cout << "Invalid output" << endl; 
      } 
     } 
    } 

    system("pause"); 
    return 0; 
} 
+0

是「名稱」總是一個字,也可以是多個單詞(像一個「真正」的名字)? –

+1

此外,你可能想閱讀[「爲什麼iostream :: eof內循環條件認爲是錯誤的?」](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-條件考慮的,是錯誤的)。 –

+0

名稱將永遠是一個單詞(在我的情況下) – CodingPoding

回答

0

只需在一個循環中讀取的兩個字符串。如果第一個等於"RUID",則需要執行任何需要開始新列表的特殊處理,否則將convert it to an integer添加到當前列表中。

例(包括列表處理僞代碼):

ListType* current_list = nullptr; 
std::string ruid_string, name; 
while (ReadFile >> ruid_string >> name) 
{ 
    if (ruid_string == "RUID") 
    { 
     // Logic to create a new list or whatever 
     if (current_list != nullptr) 
     { 
      // A list already exists, add it to the "master" list 
      master_list.AddSubList(current_list); 
     } 

     current_list = CreateList(); 
    } 
    else 
    { 
     int ruid = std::stoi(ruid_string); 

     // Add node to current list 
     if (current_list != nullptr) 
      current_list->AddNode(ruid, name); 
    } 
} 

if (current_list != nullptr) 
{ 
    // Add the last list to the master list 
    master_list.AddSubList(current_list); 
} 
+0

嗯......但是,在第一個if語句中,該列表只存在於一個循環中,然後我甚至可以使用它,否則該節點將不得不被添加到列表中,然後該列表必須是添加到主列表中 – CodingPoding

+0

@CodingPoding添加了一些僞代碼來顯示如何處理列表。 –

+0

噢好吧,這絕對有幫助。但你是什麼意思current_list = CreateList(); – CodingPoding

相關問題