如何将从文本文件中读取的元素压入或弹出到C++中的数组中,并按快速转换顺序输出堆栈?

如何将从文本文件中读取的元素压入或弹出到C++中的数组中,并按快速转换顺序输出堆栈?

问题描述:

嗨,我是新来的c + +,并无法理解如何推动和弹出元素读取从文本文件到数组,并显示这些元素以相反的顺序,例如,如果我有一个文本文件名为hero.txt与元素悟空路飞火影忍者我想输出是路飞火影忍者悟空 这是我迄今为止如何将从文本文件中读取的元素压入或弹出到C++中的数组中,并按快速转换顺序输出堆栈?

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 <:string> mystack'后,你可以使用它作为'mystack.push(mystring);'。顺便说一句,你有'text'和'mytext',并且你以混合的方式使用它们。当然,它应该只是一个。 – patatahooligan

+0

你好,谢谢,但我想避免使用模板类,因为我仍然是新的C++ – DaLOLZ

+0

不幸的是,这是一个堆栈的标准实现。事实并非如此。声明是您必须编写的模板实例的唯一行。对于其他任何线路,它都会像其他任何类别一样运行。如果你真的不想使用它,你可以借此机会编写你自己的'StringStack'类。 – patatahooligan

通常情况下,一个堆栈将index = -1开始表明堆栈是空的。所以,你需要更换

int count = 0 

int count = -1 

后你做所有的推,你的筹码看起来就像这样:

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; 
} 

好的,我们的馅饼,但只是改变它的顺序是这样的

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; 

     } 

标准库中定义的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; 
    } 
} 

#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; 
}