package calculator;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
class MyException extends Exception{
public MyException(String error) {
super(error);
}
public MyException() {
}
}
class Calculates{
private String expression;
public Calculates(String expression) {
this.expression=expression;
}
public void check() throws MyException{
int x=0;
char[] chars=expression.toCharArray();
for(int i=0;i<chars.length;i++){
switch(chars[i]) {
case'(':if(i==chars.length-1||chars[i+1]<'0'||chars[i+1]>'9')
throw new MyException("運算式不合法");break;
case')':if(i==0||chars[i+1]<'0'||chars[i+1]>'9')
throw new MyException("運算式不合法");break;
case'0':if(chars[i+1]=='/')
throw new MyException("除數不可為0");break;
default:if((chars[i]<'0'||chars[i]>'9')&&((chars[i]!='.'&&chars[i]!='+'&&chars[i]!='-'&&chars[i]!='*'&&chars[i]!='/'&&chars[i]!='#')))
throw new MyException("出現非法字符");}
}
for(int i=0;i<chars.length;i++){
switch(chars[i]){
case'(':x=x+1;break;
case')':x=x-1;break;
case'+':
case'/':
case'-':
case'*':
if(i==0||i==chars.length-1)throw new MyException("運算式不合法");
if(chars[i+1]=='-'||chars[i+1]=='+'||chars[i+1]=='/'||chars[i+1]=='*')throw new MyException("運算式不合法");break;
}
if(x!=0)throw new MyException("運算式不合法");}
}
public Stack<String> change() {
Stack<Character> s1=new Stack<>();s1.push('#');
Stack<String> s2=new Stack<>();
char[] chars=expression.toCharArray();
for(int i=0;i<chars.length;i++) {
System.out.print("改變中"+chars[i]);
if(Character.isDigit(chars[i])) {
StringBuffer sB=new StringBuffer();
while(Character.isDigit(chars[i])||chars[i]=='.')
{
sB.append(chars[i]);i++;
} s2.push(sB.toString());
}
else {
switch(chars[i]){
case'(':s1.push(chars[i]);break;
case')':while(!s1.peek().equals('(')){
s2.push(toString());
}
s1.pop();break;
case'+':
case'-':
case'/':
case'*':
if(i==0) s1.push(chars[i]);
else if(get(s1.push(chars[i]))>get(s1.push(chars[i-1])))
s1.push(chars[i]);
else
s2.push(s1.peek().toString());
case'#':while(!s1.empty()){
s2.push(s1.peek().toString());}
}//Switch
}//else
} //for
return s2;
}
public int get(char c)
{ int m=0;
switch(c) {
case'+':
case'-':m=2;break;
case'/':
case'*':m=3;break;
case'(':
case')':m=1;break;
case'#':m=0;break;}
return m;
}
public void count() {
Stack <String> s=change();int y=0;
while(!s.isEmpty()) {
String string=s.pop();
if(Character.isDigit(string.charAt(0))){
s.push(string);
}else {
int b=Integer.parseInt(s.pop());
int a=Integer.parseInt(s.pop());
if(string.equals("+")) {
y=a+b;
}else if(string.equals("-")) {
y=a-b;
}else if(string.equals("*")) {
y=a*b;
}else {
y=a/b;
}
s.push(String.valueOf(y));
} //else
}//while
System.out.print("結果是"+s.pop());
}//count
}
public class Texttwo {
public static void main(String[] args)throws MyException{
System.out.print("請輸入算式(以“#”作為結束標志):");
Scanner sc=new Scanner(System.in);
String formula=sc.nextLine();
Calculates c=new Calculates(formula);
c.check();c.count();sc.close();
}
}

uj5u.com熱心網友回復:
點上斷點,一步步的除錯一下吧,這個技術總是要學習的。uj5u.com熱心網友回復:
這個代碼,沒法看。。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/138020.html
標籤:Eclipse
