package a2;
import java.io.*;
public class Calc
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要计算的式子");
String expression=br.readLine();
int index=0;//字符串的下标索引
StringBuffer sb=new StringBuffer(expression);//将字符串类型转换成stringBuffer类型
if(sb.charAt(0)!='+'||sb.charAt(0)!='-')//如果字符串首位没有符号位,则自动添加上’+'
{
sb.insert(0, '+');
}
sb.append('+');
while(index<sb.length())//循环遍历字符串的下标
{
if(sb.charAt(index)=='(')//每当遇到字符串中包含‘('
{
sb.insert(index+1, '0');//就在’('的下一个位置添加一个0
if(sb.charAt(index+2)!='-')//如果刚才添加的’0‘的下一个位置不是’-‘号的话,则自动添加上去一个’+‘
{
sb.insert(index+2, '+');
}
}
if(sb.charAt(index)=='[')//将'['替换成’('
{
sb.replace(index, index+1, "(");
}
if(sb.charAt(index)==']')//将‘]'替换成')'
{
sb.replace(index, index+1, ")");
}
if(sb.charAt(index)=='{')//将’{‘替换成’('
{
sb.replace(index, index+1, "(");
}
if(sb.charAt(index)=='}')//将‘}’替换成‘)'
{
sb.replace(index, index+1, ")");
}
index++;
}
Comput com=new Comput(sb);
double result=com.compute();
System.out.println("结果:"+result);
}
}
package a2;
import java.util.ArrayList;
import java.util.Arrays;
public class Comput
{
//表达式
private StringBuffer expression;
//运算符栈
private MyStack<Character> op=new MyStack();
//运算数栈
private MyStack<Double> oprands=new MyStack();
//运算符压站方案
double a=0,b=0;
public void op_Push(Oparation p)
{
//如果运算符栈为空
if(oprands.isEmpty())
{
oprands.push(a);
oprands.push(b);
//将结果压入继续压入栈中
oprands.push(a+b);
}
if(op.isEmpty())
{
//待入站的运算符直接入栈
op.push(p);
}
else
{
//带入站的和为入站的优先级比较
//查看栈顶
Oparation top=(Oparation)op.peek();
//比较将要入栈的与栈顶的优先级别
int com=p.getPro()-top.getPro();
//如果待入站的运算符级别高,直接入站
// if(com>0)
// {
// //直接压站
// op.push(p);
// }
// //如果待入栈的运算符级别低,则运算符出栈计算(同时二个数需要出栈)
// else
// {
// //弹出二个数
// //声明运算数栈顶的二个数
// int a=(Integer)oprands.pop();
// int b=(Integer)oprands.pop();
// //声明运算符栈顶的运算符
// Oparation t=(Oparation) op.pop();
// //出栈的二个数进行计算
// int result=t.computting(b,a);
// //将结果压入继续压入栈中
// oprands.push(result);
// //运算符出栈
// op_Push(p);
// }
if(com>0)
{
//入栈
op.push(p);
}
else
{
if(top.getType()=='(')
{
//入栈
op.push(p);
}
else
{
if(com>-10)
{
//出栈
op_Pop();
op_Push(p);
}
else
{
//一直出栈至‘(
while(((Oparation)op.peek()).getType()!='(')
{
op_Pop();
}
//将栈顶’(‘扔出
op.pop();
}
}
}
}
}
private void op_Pop()
{
//声明运算数栈顶的二个数
a=oprands.pop();
b=oprands.pop();
//声明运算符栈顶的运算符
Oparation t=op.pop();
//出栈的二个数进行计算
double result=t.computting(b,a);
//将结果压入继续压入栈中
oprands.push(result);
}
public Comput(StringBuffer sb)
{
super();
this.expression = sb;
}
//解析运算
public double compute()
{
int index=0;
int length=expression.length();
char ch='\0';
String num ="";
while(index<length)//循环遍历表达式的每一位
{
ch=expression.charAt(index);//循环字符变量
switch(ch)
{
case '+':
{
op_Push(Oparation.ADD);//将加号的优先级压入运算符栈中
index++;
break;
}
case '-':
{
op_Push(Oparation.DEC);//将减号的优先级压入运算符栈中
index++;
break;
}
case '*':
{
op_Push(Oparation.RIDE);//将乘号的优先级压入运算符栈中
index++;
break;
}
case '/':
{
op_Push(Oparation.DIV);//将除号的优先级压入运算符栈中
index++;
break;
}
case '%':
{
op_Push(Oparation.qy);//将求余号的优先级压入运算符栈中
index++;
break;
}
case '(':
{
op_Push(Oparation.lp);//将左括号的优先级压入运算符栈中
index++;
break ;
}
case ')':
{
op_Push(Oparation.rp);//将右括号的优先级压入运算符栈中
index++;
break;
}
default://合并数字位使其成为一个实数
{
while(ch>='0'&&ch<='9'||ch=='.')
{
num+=ch;
index++;
if(index<length)
{
ch=expression.charAt(index);
}
else
{
break;
}
}
oprands.push(Double.parseDouble(num));//将数字直接压入运算数栈中
num="";//清空,便于下次存放内容
}
}
}
while(!op.isEmpty())
{
double a=0,b=0;//声明运算数栈顶的二个数
if(op.equals('('))
{
op.pop();//如果栈顶是‘('号,直接将'('弹出栈
}
else//运算符出栈时,需要弹出二个数进行运算(但是’('出栈时不参与运算符的计算)
{
a=oprands.pop();//声明运算数栈顶的一个数(用于出栈运算)
b=oprands.pop();//声明运算数栈顶的另外一个数(用于出栈运算)
Oparation t= op.pop();//声明运算符栈顶的运算符
double result=t.computting(b,a);//出栈的二个数进行计算
oprands.push(result);//将结果压入继续压入栈中
}
}
return oprands.pop();//返回运算数栈顶的最后一个数,即是最后的运算结果
}
}
package a2;
import java.util.ArrayList;
import java.util.LinkedList;
public class MyStack<T>
{
//存储空间
//private LinkedList ll=new LinkedList();
private ArrayList ll=new ArrayList();
//出栈 弹栈
public <T> T pop()
{
//return ll.removeLast();
return (T) ll.remove(ll.size()-1);
}
//入栈 压栈
public void push(Object o)
{
//ll.addLast(o);
ll.add(o);
}
//查看栈顶
public <T> T peek()
{
//return ll.getLast();
return (T) ll.get(ll.size()-1);
}
//为空?
public boolean isEmpty()
{
return ll.isEmpty();
}
}
package a2;
public class Oparation
{
public static final Oparation ADD=new Oparation('+',0);
public static final Oparation DEC=new Oparation('-',0);
public static final Oparation RIDE=new Oparation('*',1);
public static final Oparation DIV=new Oparation('/',1);
public static final Oparation qy=new Oparation('%',1);
public static final Oparation lp=new Oparation('(',10);
public static final Oparation rp=new Oparation(')',-10);
//运算符类型
private char type;
//运算符优先级
private int pro;
public char getType()
{
return type;
}
public int getPro()
{
return pro;
}
private Oparation(char type, int pro)
{
super();
this.type = type;
this.pro = pro;
}
//运算方案
public double computting(double a,double b)
{
switch(this.type)
{
case '+':
{
return a+b;
}
case '-':
{
return a-b;
}
case '*':
{
return (a)*(b);
}
case '/':
{
return (a)/(b);
}
case '%':
{
return a%b;
}
default:
{
System.out.println("运算符有误");
System.exit(-1);
return 0;
}
}
}
}