设计了一个一个数组存储两个栈的功能,但是输出时的顺序不对,请大侠过目~

floatagfly 2008-10-15 05:00:21
#include <iostream>
using namespace std;
template <class Elem>
class double_stack
{
private:
int maxsize;
int top1;
int top2;
Elem *listarray;
public:
double_stack(int size)
{
maxsize=size;
top1=-1;
top2=maxsize;
listarray= new Elem[maxsize];

}
bool push_top1(Elem number)
{
if (top1+(maxsize-top2)+1==maxsize)
{
cout<<"full!";
return false;
}

listarray[++top1]=number;


return true;
}
bool push_top2(Elem number)
{
if (top1+(maxsize-top2)+1==maxsize)
{
cout<<"full!";
return false;
}
listarray[--top2]=number;

return true;
}

Elem pop_top1()
{
if (top1==-1)
{
cout<<"empty!";
return Elem(-1);
}
else
{
return listarray[top1--];
}
}
Elem pop_top2()
{
if (top2==maxsize)
{
cout<<"empty!";
return Elem(-1);
}
else
return listarray[top2++];
}

};
int main()
{
double_stack<int> s(4);
s.push_top1(1);
s.push_top1(2);
s.push_top2(3);
s.push_top2(4);
cout<<s.pop_top1()<<","<<s.pop_top1()<<","<<s.pop_top2()<<","<<s.pop_top2();
system("pause");
return 0;
}
请问各位大侠在cout的时候,输出顺序不对,请问错哪了,谢谢

...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kkndciapp 2008-10-15
  • 打赏
  • 举报
回复
cout计算的时候是从右到左
taodm 2008-10-15
  • 打赏
  • 举报
回复
“运算符求值顺序不确定”问题
floatagfly 2008-10-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taodm 的回复:]
兄弟,cout拆成1行1个。
[/Quote]
感谢这位大哥,不解的是 为什么拆开写,顺序就对了呢?请问里面具体的原因 ~谢谢
taodm 2008-10-15
  • 打赏
  • 举报
回复
兄弟,cout拆成1行1个。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧