Caculater_1
Main
package caculater;
import java.util.ArrayList;
import java.util.List;
public class Caculater_1 {
public static void main(String[] args) {
Double res = 0.0;
while(true){
List<Double> Num = new ArrayList<Double>();
List<Integer> Ope = new ArrayList<Integer>();
Object[] Inp = {null,-1};
boolean NorO = true;
while(true){
Inp = Input_1.Inp();
if(Inp[1].equals(-2)){
System.out.println("Error input");
continue;
}
if(NorO==true){
if(Inp[1].equals(-1)){
Num.add((Double) Inp[0]);
NorO = false;
}else{
if(Ope.size()==0){
Ope.set(0, (Integer) Inp[1]);
if(Ope.get(Ope.size()-1)==0){
break;
}
}else{
Ope.set(Ope.size()-1, (Integer) Inp[1]);
if(Ope.get(Ope.size()-1)==0){
break;
}
}
}
}else{if(Inp[1].equals(-1)){
Num.removeAll(Num);
Num.add((Double) Inp[0]);
}else{
Ope.add((Integer) Inp[1]);
if(Ope.get(Ope.size()-1)==0){
break;
}
NorO = true;
}
}
}
for(int i =0;i<Ope.size()-1;i++){
if(Ope.get(i)==3&&Ope.get(i)==4){
if(Ope.get(i)==3){
Num.set(i,Compute_1.Mul(Num.get(i),Num.get(i+1)));
Ope.remove(i);
Num.remove(i+1);
i--;
}else if(Ope.get(i)==0){
break;
}else{
Num.set(i,Compute_1.Div(Num.get(i),Num.get(i+1)));
Ope.remove(i);
Num.remove(i+1);
i--;
}
}
}
for(int i =0;i<Ope.size()-1;i++){
if(Ope.get(i)==1){
Num.set(i,Compute_1.Add(Num.get(i),Num.get(i+1)));
Ope.remove(i);
Num.remove(i+1);
i--;
}else if(Ope.get(i)==0){
break;
}else{
Num.set(i,Compute_1.Sub(Num.get(i),Num.get(i+1)));
Ope.remove(i);
Num.remove(i+1);
i--;
}
}
res = Num.get(0);
System.out.println(res);
}
}
}
输入检测
package caculater;
import java.util.Scanner;
public class Input_1 {
public static Object[] Inp(){
Double num = null;
Object[] Inp ={num,-1};
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String Input = sc.nextLine();
String[]opt ={"=","+","-","*","/"};
try {
Inp[0]=Double.parseDouble(Input);
} catch (NumberFormatException e) {
for(int i=0;i<5;i++){
if(Input.equals(opt[i])){
Inp[1] = i;
break;
}else{
Inp[1] = -2;
}
}
}
return Inp;
}
}
计算
package caculater;
public class Compute_1 {
public static double Add(double a,double b){
return a+b;
}
public static double Sub(double a,double b){
return a-b;
}
public static double Mul(double a,double b){
return a+b;
}
public static double Div(double a,double b){
return a+b;
}
}