鱼C论坛

 找回密码
 立即注册
查看: 2609|回复: 3

一个简单的栈完成中缀转后缀。。哪里不对。。。。。

[复制链接]
发表于 2016-3-30 13:07:00 | 显示全部楼层 |阅读模式
1鱼币
本帖最后由 mupa 于 2016-3-30 18:47 编辑
  1. #include <iostream>
  2. using namespace std;

  3. class stack
  4. {
  5.         public:
  6.                 void clearstack();
  7.                 int isempty();
  8.                 int length();
  9.                 char gettop();
  10.                 int push(char);
  11.                 int pop(char*);
  12.                 int traverse();
  13.         protected:
  14.                 char *top;
  15.                 char *base;
  16. };

  17. void stack::clearstack()
  18. {
  19.         top=base;
  20. }

  21. int stack::isempty()
  22. {
  23.         if(top==base)
  24.         {
  25.                 return 1;
  26.         }
  27.         else
  28.         {
  29.                 return 0;
  30.         }
  31. }

  32. int stack::length()
  33. {
  34.         return top-base;
  35. }

  36. char stack::gettop()
  37. {
  38.         return *(top-1);
  39. }

  40. int stack::push(char c)
  41. {
  42.         *top=c;
  43.         top++;
  44.         return 1;
  45. }

  46. int stack::pop(char *x)
  47. {
  48.         *x=*--top;
  49.         return 1;
  50. }

  51. int stack::traverse()
  52. {
  53.         if(top!=0)
  54.         {
  55.                 cout<<*(--top);
  56.         }
  57.         return 1;
  58. }

  59. int main()
  60. {
  61.         stack s;
  62.         char c,e;

  63.         s.clearstack();
  64.         cout<<"输入中缀表达式,#作结束标志:"<<endl;
  65.         cin>>c;

  66.         while(c != '#')
  67.         {
  68.                 while(c>='0' && c<='9')
  69.                 {
  70.                         cout<<c;
  71.                         cin>>c;
  72.                         if(c<'0'||c>'9')
  73.                         {
  74.                                 cout<<" ";
  75.                         }
  76.                 }

  77.                 if(')'==c)
  78.                 {
  79.                         s.pop(&e);
  80.                         while('('!=e)
  81.                         {
  82.                                 cout<<e;
  83.                                 s.pop(&e);
  84.                         }
  85.                 }
  86.                 else if('+'==c || '-'==c)
  87.                 {
  88.                         if(!s.length())
  89.                         {
  90.                                 s.push(c);
  91.                         }
  92.                         else
  93.                         {
  94.                                 do
  95.                                 {
  96.                                         s.pop(&e);
  97.                                         if('('==e)
  98.                                         {
  99.                                                 s.push(e);
  100.                                         }
  101.                                         else
  102.                                         {
  103.                                                 cout<<e;
  104.                                         }
  105.                                 }while(s.length() && '('!=e );
  106.                                 s.push(c);
  107.                         }
  108.                 }
  109.                 else if('*'==c || '/'==c || '('==c)
  110.                 {
  111.                         s.push(c);
  112.                 }
  113.                 else if('#'==c)
  114.                 {
  115.                         break;
  116.                 }
  117.                 else
  118.                 {
  119.                         cout<<"输入有误"<<endl;
  120.                         return -1;
  121.                 }

  122.                 cin>>c;
  123.         }
  124.         
  125.         while(s.length())
  126.         {
  127.                 s.pop(&e);
  128.                 cout<<e;
  129.         }
  130.         return 0;
  131. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-3-31 12:18:05 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-3 12:11:29 | 显示全部楼层
laikankan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-5-27 09:53:49 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-2 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表