C++ 如何获得正确的堆栈排序输出 #包括 #包括 #包括 #包括 使用名称空间std; 无效索茨塔克(烟囱和烟囱){ int tempVal; int topElement; 如果(s.size()>文件号){ s、 推送(文件编号); 索茨塔克(s);; } 对于(int i=0;i
而言,问题在于您的输出过程:C++ 如何获得正确的堆栈排序输出 #包括 #包括 #包括 #包括 使用名称空间std; 无效索茨塔克(烟囱和烟囱){ int tempVal; int topElement; 如果(s.size()>文件号){ s、 推送(文件编号); 索茨塔克(s);; } 对于(int i=0;i,c++,C++,而言,问题在于您的输出过程: #include <iostream> #include <fstream> #include <stack> #include <vector> using namespace std; void sortStack (stack <int>& s){ int tempVal; int topElement; if (s.size()<2){ retu
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
void sortStack (stack <int>& s){
int tempVal;
int topElement;
if (s.size()<2){
return;
}
else{
topElement=s.top();
s.pop();
if(topElement < s.top()){
s.push(topElement);
return;
}
else {
tempVal=s.top();
s.top()= topElement;
topElement=tempVal;
sortStack (s);
s.push(topElement);
}
}
}
int main (int argc, char * argv[]){
try {
ifstream inFS;
int fileNumbers;
stack <int> s;
if(argc < 2){
throw runtime_error ("an error occured: no input file name given");
}
inFS.open (argv[1]);
string fileName = argv[1];
if (!inFS.is_open()){
throw runtime_error ("an error occured: could not open input file " + fileName);
}
while (inFS >> fileNumbers){
s.push(fileNumbers);
sortStack(s);
}
for (int i=0; i<=s.size(); i++){
cout << s.top() << " ";
s.pop();
}
inFS.close();
}
catch (runtime_error & excpt){
cout << excpt.what() << endl;
}
return 0;
}
了解“发生了什么”的好工具调试器允许你单步执行变量,并查看变量的值。请用调试会话的结果编辑你的帖子。如何用C++调试?你使用的IDE是什么?依赖于你的平台和工具集。VisualStudio有一个很好的调试器。Linux有“代码> GDB < /Cord>。你可以用C来设置Eclipse。ode>gdb或其他调试工具。@sweenish这更像是一个学术问题。学习算法。
for (int i=0; i <= s.size(); i++) {
cout << s.top() << " ";
s.pop();
}
while (s.size()) {
cout << s.top() << " ";
s.pop();
}