Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

中缀表达式到后缀表达式的转换要把表达式从中缀表达式的形式转换成用后缀表示法
表示的等价表达式

C# Code:

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

  System.Console.WriteLine(S); //后缀

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

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

   if (c == '+' || c == '-' || c == '*' || c == '/')
   {
    if ((Operand != ""))
    {
     Operands[++Top] = System.Convert.ToDouble(Operand) ; //push Operands
     Operand = "";
    }
    y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
    x = Operands[Top--]; //pop 双目运算符的第一操作数
    switch (c)
    {
     case '+' :
      v = x + y;
      break;
     case '-' :
      v = x - y;
      break;
     case '*' :
      v = x * y;
      break;
     case '/' :
      v = x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
      break;
     default :
      v = 0;
      break;
    }
    Operands[++Top] = v; //push 中间结果再次入栈
   }
  }
  v = Operands[Top--]; //pop 最终结果
  System.Console.WriteLine(v);
  System.Console.ReadLine();
 }
}

 

Java Code:

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

  System.out.println(S); //后缀

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

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

   if (c == '+' || c == '-' || c == '*' || c == '/')
   {
    if ((Operand != ""))
    {
     Operands[++Top] = java.lang.Double.parseDouble(Operand) ; //push Operands
     Operand = "";
    }
    y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
    x = Operands[Top--]; //pop 双目运算符的第一操作数
    switch (c)
    {
     case '+' :
      v = x + y;
      break;
     case '-' :
      v = x - y;
      break;
     case '*' :
      v = x * y;
      break;
     case '/' :
      v = x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
      break;
     default :
      v = 0;
      break;
    }
    Operands[++Top] = v; //push 中间结果再次入栈
   }
  }
  v = Operands[Top--]; //pop 最终结果
  System.out.println(v);
 }
}




相关文章

相关软件