import java.io.*;
import java.util.Stack;
public class Calculator {
public static void main(String[] args) throws IOException{
new Calculator().run();
}
void run() throws IOException{ //程序主要运行部分(字符串的输入以及调用相应的函数加以计算结果)
Stack equation=new Stack();
String inf=new String();
System.out.print("请输入计算的表达式(请勿输错,输入时键盘建议为:美式键盘): ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
inf=in.readLine();
if(judge_input(inf)){
String[] expt=fun(inf);
System.out.println("\t\t结果为"+jjcc(expt));
}
else
{
System.out.println("\n\t\\你的输入有误,请仔细检查你的输入\\");
}
inf=in.readLine();
}
boolean judge_input(String inf){ //判断输入正确与否的函数
char[] infi=inf.toCharArray(); //将string转化为chararray
int length=infi.length;
int count1=0;
int count2=0;
int i;
boolean flag=true;
for(i=0;i<length;i++){ //输入字符只能为:0~9,+,-,*,/以及(,)和小数点
if(infi[i]=='(')
count1++;
else if(infi[i]==')')
count2++;
else if(infi[i]=='0'||infi[i]=='1'||infi[i]=='2'
||infi[i]=='3'||infi[i]=='4'||infi[i]=='5'
||infi[i]=='6'||infi[i]=='7'||infi[i]=='8'
||infi[i]=='9'||infi[i]=='.'||infi[i]=='+'
||infi[i]=='-'||infi[i]=='/'||infi[i]=='*')
; //不加以处理,空语句而已
else{ //含有其他的字符,输入有误
System.out.println("。。。含有非法字符(如:字母,@,#,¥等等),请仔细检查你的输入以及括号的输入。。。");
flag=false;
break;}
if(count1<count2){
flag=false;
System.out.println("。。。有")"与"("之前的情况,不匹配。。。");
break;
}
}
if(count1!=count2){
System.out.println("。。。左右括号不匹配,左括号的个数不等于右括号。。。");
flag=false;}
//输入字符串的第一个字符不能为+,-,*,/,)和小数点
if(infi[0]=='*'||infi[0]=='/'
||infi[0]=='-'||infi[0]=='.'
||infi[0]=='+'||infi[0]==')') {
System.out.println("。。。所输入的第一个字符不能为* , / , ) , . 。。。");
System.out.println("。。。若要输入负数请对该负数两边打上括号。。。。。。");
flag=false;
}
//输入字符串的最后一个字符不能为+,-,*,/,(和小数点
if(infi[length-1]=='+'||infi[length-1]=='-'
||infi[length-1]=='*'||infi[length-1]=='/'
||infi[length-1]=='.'||infi[length-1]=='('){
System.out.println("。。。所输入的最后一个字符不能为+,-,*,/,(,。。。");
flag=false;
}
//在1~length-2之间的字符不能有连续的加减乘除小数点符号
for(i=1;i<length-2;i++){
if(infi[i]=='+'||infi[i]=='-'||infi[i]=='*'||infi[i]=='/'||infi[i]=='.'){
if(infi[i+1]=='+'||infi[i+1]=='-'||infi[i+1]=='*'||infi[i+1]=='/'||infi[i+1]=='.'){
System.out.println("。。。所输入的字符串之中有连续的运算符。。。");
flag=false;
break;
}
}
if(infi[i]=='.'){ //此处代码只是对于1.1.1这种情况加以判断处理而已,
//不具有通用性:比如输入1.324.1无法运行,出现异常
if(infi[i+2]=='.'||infi[i+3]=='.')
{
System.out.println("。。。请检查你的小数部分的输入。。。");
flag=false;
break;
}
}
}
return flag;
}
int isp(char c) //判断运算符的优先级
{ //括号的优先级最高,乘除次之,加减再次之,其它末之
if(c == '+' || c == '-' )
return 1;
else if(c== '*' || c== '/' )
return 2;
else if(c == '(' || c == ')' )
return 3;
else
return 0;
}
String [] fun(String inf) //中缀表达式转后缀表达式(infix转为exp)
{
Stack<Character> s=new Stack<Character>();
char[] infi=inf.toCharArray(); //string转化为CharArray
int length=infi.length;
char[] infix=new char [length+1];
int w;
for(w=0;w<infi.length; w++)
{
infix[w]=infi[w];
}
infix[w]='\0';
String[] exp=new String[100];
char [] temp=new char [100];
char ch, y;
int i=0, j=0;
ch = infix[i++];
while(ch!='\0')
{
if( ch>='0' && ch<='9' )
{
int k=0;
while((ch>='0' && ch<='9') || ch=='.' )
{
temp[k++]=ch;
ch = infix[i++];
}
i--;
temp[k]='\0';
char [] temp2=new char [k];
int p=0;
for(p=0; p<k;p++)
{
temp2[p]=temp[p];
}
exp[j++] = new String(temp2) ;
}
else if(ch == ')')
{
for ( y = s.pop().charValue(); y != '('; y = s.pop().charValue())
exp[j++] = Character.toString (y) ;
}
else
{
if(s.empty() == true)
{
s.push(new Character(ch));
}
else if(s.empty() != true )
{
for( y = s.pop().charValue(); (isp(y) >= isp(ch)) && y != '(' ; y =s.pop().charValue() )
{
exp[j++] = Character.toString (y) ;
if(s.empty() == true)
break;
}
if(isp(y) < isp(ch) || y=='(')
s.push(new Character(y));
s.push(new Character(ch));
}
}
ch = infix[i++];
}
while( s.empty()!= true)
{
y = s.pop().charValue();
exp[j++] = Character.toString (y) ;
}
exp[j] = "";
String []expt=new String [j+1];
for(w=0;w<j;w++)
{
expt[w]=exp[w];
}
expt[j]="";
return expt;
}
Double jjcc(String [] s) //进行加减乘除的计算
{
Stack<Double> t=new Stack<Double>();
int i=0;
String ch;
ch = s[i++];
while(ch != "")
{
if(ch.equals("+") )
{
Double a,b;
a=t.pop();
b=t.pop();
a+=b;
t.push(a);
}
else if(ch.equals("-"))
{
Double a,b;
a=t.pop();
b=t.pop();
b-=a;
t.push(b);
}
else if(ch.equals("*"))
{
Double a,b;
a=t.pop();
b=t.pop();
a*=b;
t.push(a);
}
else if(ch.equals("/"))
{
Double a,b;
a=t.pop();
b=t.pop();
if(a==0){
System.out.println("进行除法运算时,发现除数为0!");
}
b=b/a;
t.push(b);
}
else
{
t.push(new Double(ch));
}
ch=s[i++];
}
if(t.empty() != true)
return t.pop();
else
return null;
}
}