算术表达式求值演示——严蔚敏版数据结构实习题1

该算法目前还只能计算个位数的四则运算,
如 5-9+8*7/1
二25+9*8/74则不能计算
改进中……

Stack.calss

/**
* ***********CopyRight**************
*-------Powered by QianXunNet-----
*-----Version 1.00 2009-01-17-----
*----- Design BY NiChao -----
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
package yanweimin.StackOpertor;

public class Stack {

/*
* 将数据num存入栈
* @param num 带存入的数据或运算符
*/
public void push(int num)
{
if(this.p == maxsize)
System.out.println("栈已满");
else
{
this.p++;
this.stackvalue[p] = num;
}
}

/*
* 从栈中取出一个数据或运算符
* @return temp 取出的值
*/
public int pop()
{
int temp = -1;
//栈已经空
if(this.p != -1)
{
temp = this.stackvalue[p];
this.p--;
}
return temp;
}

/*
* 检验是否是运算符
* @param num 待检验的字符
* @ true/false
*/
public boolean isOperator(int num)
{
if(num == 42 || num == 43 || num ==45 || num ==47)
return true;
else
return false;
}
/*
* 对两个数进行计算
* @param a b 两个操作数
* @param operator 运算符]
* @return jieguo 运算结果
*/
public int twoJieGuo(int a, int operator, int b)
{
switch(operator)
{
case 42 : return (a * b);
case 43 : return (a + b);
case 45 : return (a -b);
case 47 : return (a/b);
}
return 0;
}
/*
* 判断运算符的优先顺序
* @ param operator 带判断的运算符
* @ return youxianma
*/
public int youxianMa(int operator)
{
switch(operator)
{
case 42: return 2;
case 47: return 2;
case 43: return 1;
case 45: return 1;
}
return 0;
}

//栈的最大容量
int maxsize =20;
//栈数组
int[] stackvalue = new int[maxsize];
// 栈内的指针 孔栈的时候为-1
int p = -1;
}



YunSuanFu.java

/**
* ***********CopyRight**************
*-------Powered by QianXunNet-----
*-----Version 1.00 2009-01-17-----
*----- Design BY NiChao -----
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/

package yanweimin.StackOpertor;

import yanweimin.StackOpertor.Stack;
import java.util.Scanner;
public class YunSuanFu {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义变量
Stack yunsuanfu = new Stack(); //运算符栈
Stack shu = new Stack(); //操作数栈
String ex = ""; //带运算的表达式
int zhizhen =0; //字符串上指示的指针
int operator = 0; //操作符
int operated1=0,operated2 = 0; //操作数

Scanner cin = new Scanner(System.in);
System.out.println("input a biaodashi:");
ex = cin.nextLine();
while(true)
{
//判断是否为运算符
if(yunsuanfu.isOperator( (int) ex.charAt(zhizhen) ) )
{
//需要进行运算符的比较
if(yunsuanfu.p != -1)
{
//先运算栈内的
if(yunsuanfu.youxianMa( (int) ex.charAt(zhizhen) ) <= yunsuanfu.youxianMa((int) yunsuanfu.stackvalue[yunsuanfu.p] ))
{
operated1 = shu.pop();
operated2 = shu.pop();
operator = yunsuanfu.pop();
shu.push( shu.twoJieGuo(operated2, operator,operated1) );
}
}
yunsuanfu.push((int) ex.charAt(zhizhen) );

}
//为运算数时
else
{
shu.push((int) (ex.charAt(zhizhen)-48));
}
zhizhen++;
if(zhizhen >=ex.length())
break;
}
//将剩余运算
while(yunsuanfu.p != -1)
{
operated1 = shu.pop();
operated2 = shu.pop();
operator = yunsuanfu.pop();
shu.push( shu.twoJieGuo(operated2,operator,operated1));
}
//取出表达式的最终结果

int last = shu.pop();
System.out.print("你输入的表达式:"+ex+"=");
System.out.println(last);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值