在线
客服

发布
需求

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

偽代碼和C++的轉換

设从键盘输入一整数的序列:a1,a2,a3,..,an,试编写算法实现:用栈结构存储输入的整数,当ai≠-1时,将aⅰ进栈;当aⅰ=-1时,输出栈顶整数并出栈。算法应对异常情况(入栈满等)给出相应的信息。
请分别用伪代码和C++完成。

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

伪代码:
while getEleFromStream -> ele
if ele != -1
  if fullStack
   logout
  else
   push ele
else
  if emptyStack
   logout
  else
   print pop
C++:
#include
#include
using namespace std;
typedef stack Stack;
const int stack_limit = 20;
void logout(const char *info);
int getEleFromStream(Stack &stk); // return errno_t [0 == normal, 1 == full, -1 == empty]
                            // process will exit while the empty condition take place
int main()
{
Stack stk;
while (getEleFromStream(stk) != -1);
return 0;
}
void logout(const char *info)
{
cout  ele;
if (ele != -1)
  if (stk.size() == limit)
  {
   logout("Full Stack!");
   return 1;
  } else
   stk.push_back(ele);
else
  if (stk.size() == 0)
  {
   logout("Empty Stack!");
   return -1;
  } else

   cout

使用道具 举报

发新帖
国内首家创新型IT技术需求众包服务平台,软件需求就上天盟网! 立即登录 立即注册