如何在 C++ 中将从文本文件中读取的元素推送和弹出到数组中,并按 Revserse 顺序输出堆栈?
how to push and pop elements read from a textfile to an array in c++ and output the stack in revserse order?
嗨,我是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
的值以0
。Pop
你需要做的只是减少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;
}
相关文章:
- 指向 c++ 中的向量数组并将其发送到函数的指针
- 将浮点数转换为无符号字符数组并打印出来
- 将对象的字节复制到数组并再次复制回来是否安全
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 拆分字符数组并存储为向量
- 在 Cython 中创建一个连续数组并将其传递给C++
- 将值更改为 c++ 的数组并保存文件
- 附加动态数组并在完成后将其大小加倍
- BitBlt转换为字节数组并从c ++解析为c#
- 如何创建一个带锁的指针数组并检查指针是否空闲?
- C++ 如何从文本文件中扫描二维数组并对行求和?
- C++ 循环逐行写入文件还是添加到数组并在循环后写入文件?
- 如何初始化可以设置为默认数组并将输入/输出作为数组的类函数?
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 如何在 VBA/Excel 中创建字符串数组并将其发送到C++ DLL,以便可以在 DLL 中循环访问它
- 初始化数组并出现堆栈溢出错误
- 我正在尝试修改一个字节数组并通过 JNI 将其从 c++ 发送到 java
- 编写一个递归功能,该功能采用数组并以相反顺序显示元素,而无需在末尾启动数组的索引
- 按引用传递数组并修改值C++