数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)

C# Code:
ContractedBlock.gif ExpandedBlockStart.gif
None.gif//using System;
None.gif
class Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif 
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  System.Console.WriteLine(
"Hello World!");
InBlock.gif  
//中缀 => 后缀表达式
InBlock.gif
  string s = "(  1.9 +(20 +41)/ (25*11) -3 ) * 2"//中缀; //中缀
InBlock.gif
  string S = ""//后缀
InBlock.gif
  char[] Operators = new char[s.Length];
InBlock.gif  
int Top = -1;
InBlock.gif  
for (int i = 0; i < s.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
char C = s[i];
InBlock.gif   
switch (C)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
case ' ' : //忽略空格
InBlock.gif
     break;
InBlock.gif    
case '+' : //操作符
InBlock.gif
    case '-' :
InBlock.gif     
while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       Operators[
++Top] = c; //push Operator
InBlock.gif
       break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       S 
= S + c;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     Operators[
++Top] = C; //push Operator
InBlock.gif
     S += " ";
InBlock.gif     
break;
InBlock.gif    
case '*' : //忽略空格
InBlock.gif
    case '/' :
InBlock.gif     
while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       Operators[
++Top] = c; //push Operator
InBlock.gif
       break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
if (c == '+' || c == '-')
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        Operators[
++Top] = c; //push Operator
InBlock.gif
        break;
ExpandedSubBlockEnd.gif       }

InBlock.gif       
else
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        S 
= S + c;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     Operators[
++Top] = C; //push Operator
InBlock.gif
     S += " ";
InBlock.gif     
break;
InBlock.gif    
case '(' :
InBlock.gif     Operators[
++Top] = C;
InBlock.gif     S 
+= " ";
InBlock.gif     
break;
InBlock.gif    
case ')' :
InBlock.gif     
while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       S 
= S + c;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     S 
+= " ";
InBlock.gif     
break;
InBlock.gif    
default :
InBlock.gif     S 
= S + C;
InBlock.gif     
break;
InBlock.gif    
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
while (Top >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   S 
= S + Operators[Top--]; //pop Operator
ExpandedSubBlockEnd.gif
  }

InBlock.gif
InBlock.gif  System.Console.WriteLine(S); 
//后缀
InBlock.gif
InBlock.gif  
//后缀表达式计算
InBlock.gif
  double[] Operands = new double[S.Length];
InBlock.gif  
double x, y, v;
InBlock.gif  Top 
= - 1;
InBlock.gif  
string Operand = "";
InBlock.gif  
for (int i = 0; i < S.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
char c = S[i];
InBlock.gif   
if ((c >= '0' && c <= '9'|| c == '.')
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Operand 
+= c;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
if ((c == ' ' || i == S.Length - 1&& Operand != ""//Update
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    Operands[
++Top] = System.Convert.ToDouble(Operand) ; //push Operands
InBlock.gif
    Operand = "";
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
if (c == '+' || c == '-' || c == '*' || c == '/')
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if ((Operand != ""))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     Operands[
++Top] = System.Convert.ToDouble(Operand) ; //push Operands
InBlock.gif
     Operand = "";
ExpandedSubBlockEnd.gif    }

InBlock.gif    y 
= Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
InBlock.gif
    x = Operands[Top--]; //pop 双目运算符的第一操作数
InBlock.gif
    switch (c)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
case '+' :
InBlock.gif      v 
= x + y;
InBlock.gif      
break;
InBlock.gif     
case '-' :
InBlock.gif      v 
= x - y;
InBlock.gif      
break;
InBlock.gif     
case '*' :
InBlock.gif      v 
= x * y;
InBlock.gif      
break;
InBlock.gif     
case '/' :
InBlock.gif      v 
= x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
InBlock.gif
      break;
InBlock.gif     
default :
InBlock.gif      v 
= 0;
InBlock.gif      
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif    Operands[
++Top] = v; //push 中间结果再次入栈
ExpandedSubBlockEnd.gif
   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  v 
= Operands[Top--]; //pop 最终结果
InBlock.gif
  System.Console.WriteLine(v);
InBlock.gif  System.Console.ReadLine();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

Java Code:

ContractedBlock.gif ExpandedBlockStart.gif
None.gifclass Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif 
public static void main(String[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  System.
out.println("Hello World!");
InBlock.gif  
//中缀 => 后缀表达式
InBlock.gif
  String s = "(  1.9   +  (20 +  41)    / (25 * 11) -     3          )              * 2"//中缀
InBlock.gif
  String S = ""//后缀
InBlock.gif
  char[] Operators = new char[s.length()];
InBlock.gif  
int Top = -1;
InBlock.gif  
for (int i = 0; i < s.length(); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
char C = s.charAt(i);
InBlock.gif   
switch(C)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
case ' ' :
InBlock.gif     
break;
InBlock.gif    
case '+' : //操作符
InBlock.gif
    case '-' :
InBlock.gif     
while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       Operators[
++Top] = c; //push Operator
InBlock.gif
       break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       S 
= S + c;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     Operators[
++Top] = C; //push Operator
InBlock.gif
     S += " ";
InBlock.gif     
break;
InBlock.gif    
case '*' : //操作符
InBlock.gif
    case '/' :
InBlock.gif     
while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       Operators[
++Top] = c; //push Operator
InBlock.gif
       break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
if (c == '+' || c == '-')
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        Operators[
++Top] = c; //push Operator
InBlock.gif
        break;
ExpandedSubBlockEnd.gif       }

InBlock.gif       
else
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        S 
= S + c;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     Operators[
++Top] = C; //push Operator
InBlock.gif
     S += " ";
InBlock.gif     
break;
InBlock.gif    
case '(' : //操作符
InBlock.gif
     Operators[++Top] = C;
InBlock.gif     S 
+= " ";
InBlock.gif     
break;
InBlock.gif    
case ')' : //操作符
InBlock.gif
     while (Top >= 0//栈不为空时
ExpandedSubBlockStart.gifContractedSubBlock.gif
     dot.gif{
InBlock.gif      
char c = Operators[Top--]; //pop Operator
InBlock.gif
      if (c == '(')
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       S 
= S + c;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     S 
+= " ";
InBlock.gif     
break;
InBlock.gif    
default : //操作数
InBlock.gif
     S = S + C;
InBlock.gif     
break;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
while (Top >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   S 
= S + Operators[Top--]; //pop Operator
ExpandedSubBlockEnd.gif
  }

InBlock.gif
InBlock.gif  System.
out.println(S); //后缀
InBlock.gif
InBlock.gif  
//后缀表达式计算
InBlock.gif
  double[] Operands = new double[S.length()];
InBlock.gif  
double x, y, v;
InBlock.gif  Top 
= - 1;
InBlock.gif  String Operand 
= "";
InBlock.gif  
for (int i = 0; i < S.length(); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
char c = S.charAt(i);
InBlock.gif   
if ((c >= '0' && c <= '9'|| c == '.')
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Operand 
+= c;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
if ((c == ' ' || i == S.length() - 1&& Operand != ""//Update
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    Operands[
++Top] = java.lang.Double.parseDouble(Operand) ; //push Operands
InBlock.gif
    Operand = "";
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
if (c == '+' || c == '-' || c == '*' || c == '/')
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if ((Operand != ""))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     Operands[
++Top] = java.lang.Double.parseDouble(Operand) ; //push Operands
InBlock.gif
     Operand = "";
ExpandedSubBlockEnd.gif    }

InBlock.gif    y 
= Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
InBlock.gif
    x = Operands[Top--]; //pop 双目运算符的第一操作数
InBlock.gif
    switch (c)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
case '+' :
InBlock.gif      v 
= x + y;
InBlock.gif      
break;
InBlock.gif     
case '-' :
InBlock.gif      v 
= x - y;
InBlock.gif      
break;
InBlock.gif     
case '*' :
InBlock.gif      v 
= x * y;
InBlock.gif      
break;
InBlock.gif     
case '/' :
InBlock.gif      v 
= x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
InBlock.gif
      break;
InBlock.gif     
default :
InBlock.gif      v 
= 0;
InBlock.gif      
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif    Operands[
++Top] = v; //push 中间结果再次入栈
ExpandedSubBlockEnd.gif
   }

ExpandedSubBlockEnd.gif  }

InBlock.gif  v 
= Operands[Top--]; //pop 最终结果
InBlock.gif
  System.out.println(v);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/grandydong/archive/2007/03/02/661973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值