请你编写程序实现能处理两个数的+、-、*、/、%的表达式程序。数据的输入/输出全部使用标准输入/输出,输入数据的第一行为你需要计算表达式的个数,从第2行开始,每一行为你计算的一个表达式,每行数据中没有空格,除%运算外的输入数据皆为float数,除%运算结果为整数外,其他都保留两位小数,每个表达式的结果使用一行输出。
输入测试用例:
4
1+2
3.12*4
7%3
1/3
用例输出结果:
3.00
12.48
1
0.33
import java.util.Scanner;
public class sum {
public static void main(String[] args) {
int n,j, i,len;
String str;
double num1, num2, num = 0;
Scanner in = new Scanner(System.in);
//in.nextLine();
while (in.hasNext()) {
//in.nextLine();
n = in.nextInt();
for (i = 0; i < n; i++) {
str=in.next();
len=str.length();
for(j=1;j<len;j++)
{
if(str.charAt(j)=='-'||str.charAt(j)=='+'||str.charAt(j)=='*'||str.charAt(j)=='/'||str.charAt(j)=='%')
break;
}
String s1=str.substring(0,j);
String s2=str.substring(j+1,len);
num1=Double.parseDouble(s1);
num2=Double.parseDouble(s2);
if(str.charAt(j)=='-')
{
System.out.printf("%.2f\n",num1-num2);
}
if(str.charAt(j)=='+')
{
System.out.printf("%.2f\n",num1+num2);
}
if(str.charAt(j)=='*')
{
System.out.printf("%.2f\n",num1*num2);
}
if(str.charAt(j)=='/')
{
System.out.printf("%.2f\n",num1/num2);
}
if(str.charAt(j)=='%')
{
System.out.printf("%.0f\n",num1%num2);
}
}
}
}
}