题目1019:简单计算器
<strong><span style="font-size:14px;">代码如下:先分割加法,再分割减法,最后分割乘法递归(将除法换算成乘法)</span></strong>
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.nextLine();
if(s.equals("0")) break;
s = s.replace(" ", "");
String[] s1 = s.split("\\+");
double total = 0;
for(int i=0;i<s1.length;i++)
{
total += Min(s1[i]);
}
System.out.printf("%.2f\n",total);
}
}
private static double Min(String string) {
String[] s = string.split("\\-");
double total = 0;
for(int i=0;i<s.length;i++)
{
if(i==0)
total = mul(s[i]);
else total -= mul(s[i]);
}
return total;
}
private static double mul(String string) {
string=string.replace("/", "*A");
String[] s = string.split("[*]");
double total = 1;
for(int i=0;i<s.length;i++)
{
Double x;
if(s[i].charAt(0)=='A')
{
s[i]=s[i].replace("A", "");
x = new Double(s[i]);
x = 1.0/x;
}
else {
x = new Double(s[i]);
}
total *= x;
}
return total;
}
}