鱼C论坛

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

关于 后缀表达式的运算问题

[复制链接]
发表于 2013-4-23 22:31:37 | 显示全部楼层 |阅读模式
5鱼币
我想计算3+6的和 怎么结果是105呢?
  1. #include <iostream.h>
  2. #define MAX 100
  3. typedef  struct
  4.   {
  5.         int  data[MAX];
  6.     int  top;
  7.   }SeqStack;      

  8. SeqStack *Init_SeqStack()   
  9. {     
  10.   SeqStack  *s;
  11.   s=new SeqStack;
  12.   if (!s)
  13.     {
  14.       return NULL;   
  15.     }
  16.   else
  17.     {
  18.           s->top=-1;     
  19.       return s;     
  20.     }
  21. }

  22. int Empty(SeqStack *s){  
  23.   if (s->top==-1)
  24.     return 1;   
  25.   else
  26.     return 0;
  27. }

  28. int Push_SeqStack(SeqStack *s,int  x){   
  29.   if (s->top==MAX-1)
  30.     return 0;
  31.   else
  32.     { s->top++;
  33.       s->data[s->top]=x;
  34.       return 1;
  35.     }
  36. }

  37. int Pop_SeqStack(SeqStack *s,int *x){     
  38.   if (Empty(s))
  39.     return 0;  
  40.   else
  41.     { *x=s->data[s->top];  
  42.        s->top--;   
  43.        return 1;      
  44.     }
  45. }

  46. int GetTop(SeqStack *s)   
  47. {  
  48.   
  49.     return(s->data[s->top]);  
  50. }

  51. /*****/
  52. double calcul_exp(char *A)
  53. {
  54.         SeqStack *s;
  55.        
  56.         char ch ;
  57.         s = Init_SeqStack();
  58.         int a = 0,b = 0,c = 0;
  59.         while(ch != '#')
  60.         {
  61.                 if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )
  62.                
  63.                         Push_SeqStack(s,ch);
  64.                         else
  65.                         {
  66.                                 Pop_SeqStack(s,&b);
  67.                                 Pop_SeqStack(s,&a);
  68.                                 switch(ch)
  69.                                 {
  70.                                         case '+': c = a + b;break;
  71.                                         case '-': c = a - b;break;
  72.                                         case '*': c = a * b;break;
  73.                                         case '/': c = a / b;break;

  74.                                 }
  75.                                 Push_SeqStack(s,c);
  76.                         }
  77.                         ch = *A++;
  78.                
  79.         }
  80.         Pop_SeqStack(s,&c);
  81.         return c;
  82. }

  83. void main()
  84. {
  85.     char a[10] = "36+#";
  86.         double mn = calcul_exp(a);
  87.         cout << mn;
  88. }
复制代码
QQ截图20130423223044.jpg

最佳答案

查看完整内容

有几处问题,在你的基础上改了一下,仅供参考:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-4-23 22:31:38 | 显示全部楼层
有几处问题,在你的基础上改了一下,仅供参考:
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 100
  4. typedef  struct
  5. {
  6.         int  data[MAX];
  7.         int  top;
  8. }SeqStack;      

  9. SeqStack *Init_SeqStack()   
  10. {     
  11.         SeqStack  *s;
  12.         s=new SeqStack;
  13.         if (!s)
  14.         {
  15.                 return NULL;   
  16.         }
  17.         else
  18.         {
  19.                 s->top=-1;     
  20.                 return s;     
  21.         }
  22. }

  23. int Empty(SeqStack *s){  
  24.         if (s->top==-1)
  25.                 return 1;   
  26.         else
  27.                 return 0;
  28. }

  29. int Push_SeqStack(SeqStack *s,int  x){   
  30.         if (s->top==MAX-1)
  31.                 return 0;
  32.         else
  33.         {
  34.                 s->top++;
  35.             s->data[s->top]=x;
  36.             return 1;
  37.         }
  38. }

  39. int Pop_SeqStack(SeqStack *s,int *x){     
  40.         if (Empty(s))
  41.                 return 0;  
  42.         else
  43.         {
  44.                 *x=s->data[s->top];
  45.                 s->top--;
  46.                 return 1;      
  47.         }
  48. }

  49. int GetTop(SeqStack *s)   
  50. {  

  51.         return(s->data[s->top]);  
  52. }

  53. /*****/
  54. double calcul_exp(char *A)
  55. {
  56.         SeqStack *s;

  57.         char ch ;
  58.         s = Init_SeqStack();
  59.         int a = 0,b = 0,c = 0;
  60.         ch=*A;
  61.         while(ch != '#')
  62.         {
  63.                 if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )
  64.                 {
  65.                         int i = atoi((const char *)&ch);
  66.                         Push_SeqStack(s,i);
  67.                 }
  68.                 else
  69.                 {
  70.                         Pop_SeqStack(s,&b);
  71.                         Pop_SeqStack(s,&a);
  72.                         switch(ch)
  73.                         {
  74.                         case '+': c = a + b;break;
  75.                         case '-': c = a - b;break;
  76.                         case '*': c = a * b;break;
  77.                         case '/': c = a / b;break;

  78.                         }
  79.                         Push_SeqStack(s,c);
  80.                 }
  81.                 A++;
  82.                 ch = *A;

  83.         }
  84.         Pop_SeqStack(s,&c);
  85.         return c;
  86. }

  87. void main()
  88. {
  89.         char a[10] = "36+#";
  90.         double mn = calcul_exp(a);
  91.         cout << mn;
  92.         getchar();
  93. }
复制代码
1.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-4-25 12:51:24 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-4-25 12:57:01 | 显示全部楼层
小新110 发表于 2013-4-24 13:36
有几处问题,在你的基础上改了一下,仅供参考:
  1. if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )
复制代码
哦 我知道了 把 改成
  1. if (ch != 字符)
复制代码
就可以了吧。。。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 21:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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