2017-07-31 6 views
1

こんにちは私はC++を初めて使っていて、要素をテキストファイルから配列に読み込んでポップアップする方法と、逆の順序で要素を表示する方法を理解するのに問題があります。例えば、要素があるhero.txtというテキストファイル悟空ルフィナルトが、私はこれは私がこれまでテキストファイルから読み込んだ要素をC++で配列にプッシュしてポップし、スタックをrevserse順に出力する方法はありますか?

string hero[100]; // array to store elements 
    int count=0; 

    int main() 
    { 
     fstream myfile; 
     string nameOffile; 
     string text; 
     string mytext; 
     cout << "Enter name of file" << endl; 
     cin >> nameOffile 
     myfile.open(nameOffile.c_str()); 
      if (!myfile) 
      { 
       cerr << "error abort" << endl; 
       exit(1); 
      } 
      while (myfile >> text) 
      { 

       Push(mytext); //Note I know this is wrong I just don't know how to write it in a manner that will push the first element of the textfile to the top 


      } 

     myfile.close(); 

     while(hero[count]=="") 
     { 
//Again I know these two lines are incorrect just don't know how to implement in correct manner 


      cout <<hero[0] << " " <<endl; 
      Pop(mytext); 


     } 

    } 

// Function for push 
void Push(string mytext) 
{ 
    count = count + 1; 
    hero[count] = mytext; 

} 

void Pop(string mytext) 
{ 
    if(count=0) 
    { 
     mytext = " "; 

    } 
    else 
    { 
     mytext = hero[count]; 
     count = count - 1; 
    } 

} 
+0

はhttp://en.cppreference.com/w/cpp/container/stackを参照してください。テンプレート化されたクラスなので、 'std :: stack mystack'と宣言すると、それを' mystack.push(mystring); 'として使うことができます。ところで、あなたは 'text'と' mytext'を持っていて、それらを混在して使いました。確かにそれはただのものだったはずです。 – patatahooligan

+0

こんにちはありがとうが、私はまだC++に新しいので、テンプレートクラスを使用しないようにしたい – DaLOLZ

+0

悲しいことに、これはスタックの標準実装です。実際にはそれほど難しいことではありません。宣言は、書かなければならないテンプレートインスタンス化の唯一の行です。他のすべての行について、他のクラスと同様に動作します。実際に使用したくない場合は、独自の 'StringStack'クラスを書く機会があります。 – patatahooligan

答えて

0

通常は持っているものである出力はナルトルフィ悟空 になりたい、スタックはスタックが空であることを示すためにindex = -1で始まります。だから、あなたがした後

int count = -1 

int count = 0 

を交換する必要があるすべて押し、あなたのスタックが次のようになります。

hero[0] = "Goku" 
hero[1] = "Luffy" 
hero[2] = "Naruto" 

は、逆の順序で、それをプリントアウトします最後のインデックスから最初のインデックスにループすることができます。すべてのヒーローストリングを押した後、countは2になります。最後のヒーローはindex = 0になります。したがって、ループを書き換えることができます

while(count >= 0) 
{ 
    cout << hero[count] << " " <<endl; 
    Pop(); 
} 

Popの機能も正しくありません。 ifステートメントでは、countの値を0に置き換えます。 Popで行う必要があるのは、ちょうどcountの値を減らすことです。 だから、

プッシュ機能は、良い作品あなたの機能を改善することにより

void Pop() 
{ 
    count = count - 1; 
} 
0

OKレッツ・タルトとしてそれを書き換えることができますが、ちょうどこの

void Push(string mytext) 
{ 
    hero[count] = mytext; //now you will start at index 0 
    count = count + 1; 
} 

ポップ機能のようなものにすることの順序を変更する必要がありますこのようになります

文字列の値を返す必要があり、パラメータを渡す必要はありません

string Pop() 
{ 
    if(count == 0) 
    { 
     return ""; 
    } 
    else 
    { 

     count = count - 1; 
     mytext = hero[count]; 
     return mytext; 
    } 

} 

今あなたは関数が我々はそれがあるべき出力

を表示中に変更する必要があなたの主な

に正しくプッシュ機能を使用している彼らに

を使用してみましょう準備ができていますこのように

while(true) 
     { 
      tempText = pop(); // this function will get you the last element and then remove it 
      if (tempText == "") // now we are on top of the stack 
       break; 

      cout <<tempText << " " <<endl; 

     } 
0

vector標準ライブラリで定義されているクラスは、staのように動作しますck。たとえば :

// include the library headers 
#include <vector> 
#include <string> 
#include <iostream> 

// use the namespace to make the code less verbose 
using namespace std; 

int main() 
{ 
    // declare the stack 
    vector<string> heroStack; 

    // insert the elements 
    heroStack.push_back("Goku"); 
    heroStack.push_back("Luffy"); 
    heroStack.push_back("Naruto"); 

    // print elements in reverse order 
    while(!heroStack.empty()) 
    { 
     // get the top of the stack 
     string hero = heroStack.back(); 
     // remove the top of the stack 
     heroStack.pop_back(); 

     cout << hero << endl; 
    } 
} 
0
#include "stdafx.h" 
    #include <fstream> 
    #include <stack> 
    #include <string> 
    #include <iostream> 

    class ReadAndReversePrint 
    { 
     std::stack<std::string> st; 
     std::ifstream file; 
     public: 
     ReadAndReversePrint(std::string path) 
     { 
      file.open(path); 
      if (file.fail()) 
      { 
       std::cout << "File Open Failed" << std::endl; 
       return; 
     } 
     std::string line; 
     while (!file.eof()) 
     { 
      file >> line; 
      st.push(line); 
     } 
     file.close(); 

     std::cout << "Reverse printing : " << std::endl; 
     while (!st.empty()) 
     { 
      std::cout << st.top().c_str() << "\t"; 
      st.pop(); 
     } 
     std::cout << std::endl; 
    } 
}; 


int main() 
{ 
    ReadAndReversePrint rrp("C:\\awesomeWorks\\input\\reverseprint.txt"); 
    return 0; 
} 
関連する問題