...超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及...
发布网友
发布时间:2023-11-24 18:15
我来回答
共1个回答
热心网友
时间:2024-04-02 06:10
//四则运算,转:
package nostop;
import java.io.*;
import java.util.Scanner;
import java.math.*;
import java.text.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner ( System.in );
BigInteger a,b;
int c;
char op;
String s;
while( cin.hasNext() )
{
a = cin.nextBigInteger();
s = cin.next();
op = s.charAt(0);
if( op == '+')
{
b = cin.nextBigInteger();
System.out.println(a.add(b));
}
else if( op == '-')
{
b = cin.nextBigInteger();
System.out.println(a.subtract(b));
}
else if( op == '*')
{
b = cin.nextBigInteger();
System.out.println(a.multiply(b));
}
else
{
BigDecimal a1,b1,eps;
String s1,s2,temp;
s1 = a.toString();
a1 = new BigDecimal(s1);
b = cin.nextBigInteger();
s2 = b.toString();
b1 = new BigDecimal(s2);
c = cin.nextInt();
eps = a1.divide(b1,c,4);
//System.out.println(a + " " + b + " " + c);
//System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);
System.out.print( a.divide(b) + " " + a.mod(b) + " ");
if( c != 0)
{
temp = "0.";
for(int i = 0; i < c; i ++) temp += "0";
DecimalFormat gd = new DecimalFormat(temp);
System.out.println(gd.format(eps));
}
else System.out.println(eps);
}
}
}
}