Untitled
unknown
java
4 years ago
1.4 kB
8
Indexable
import java.math.BigInteger;
public class HelloWorld{
// str1 < str2 -1
// str1 > str2 1
// str1 == str2 0
static public int compare(BigInteger so1, BigInteger so2){
String str1 = so1.toString();
String str2 = so2.toString();
int quote = 1;
if (str1.contains("-") && str2.contains("-")) {
quote = -1;
}
if (str1.contains("-") && !str2.contains("-"))
return -1;
else if (!str1.contains("-") && str2.contains("-"))
return 1;
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if(n1 < n2)
return -1;
else if(n1 > n2)
return 1;
else {
for(int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i))
return -1*quote;
else if (str1.charAt(i) > str2.charAt(i))
return 1*quote;
}
}
return 0;
}
public static void main(String []args){
BigInteger b1, b2,result,b3, b4, b5;
b1 = new BigInteger("-5");
b2 = new BigInteger("5");
b3 = new BigInteger("-5");
b4 = new BigInteger("0");
b5 = new BigInteger("11");
System.out.println(compare(b3,b1));
System.out.println(compare(b5,b2));
System.out.println(compare(b1,b2));
System.out.println(compare(b1,b3));
}
}Editor is loading...