如何在 C++ 中将从文本文件中读取的元素推送和弹出到数组中,并按 Revserse 顺序输出堆栈?



嗨,我是c++ 的新手,很难理解如何将从文本文件读取的元素推送和弹出到数组中并以相反的顺序显示这些元素,例如,如果我有一个名为 hero 的文本文件.txt 带有元素悟空 悟空 路飞火影忍者 我希望输出是火影忍者 路飞悟空 这就是我到目前为止所拥有的

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

通常,堆栈将以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的值以0Pop你需要做的只是减少count的价值。 所以你可以把它改写成

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

标准库中定义的vector类就像一个堆栈。 例如:

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

好吧,让我们通过改进您的功能来酸

推送功能效果很好,但只需将其顺序更改为这样

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

应该是这样的

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

最新更新