add all variants for 'Sum' task (hw2)
This commit is contained in:
40
java/sum/SumBigIntegerOctal.java
Normal file
40
java/sum/SumBigIntegerOctal.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package sum;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class SumBigIntegerOctal {
|
||||
public static void main(String[] args) {
|
||||
BigInteger res = new BigInteger("0");
|
||||
for (String arg : args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (char c : arg.toCharArray()) {
|
||||
if (!Character.isWhitespace(c)) {
|
||||
builder.append(c);
|
||||
} else {
|
||||
res = res.add(compute(builder.toString()));
|
||||
|
||||
builder = new StringBuilder();
|
||||
}
|
||||
}
|
||||
res = res.add(compute(builder.toString()));
|
||||
}
|
||||
System.out.println(res);
|
||||
}
|
||||
|
||||
static BigInteger compute(String num) {
|
||||
BigInteger res = new BigInteger("0");
|
||||
if (num.isEmpty()) {
|
||||
res = res.add(BigInteger.ZERO);
|
||||
} else if (num.endsWith("o") || num.endsWith("O")) {
|
||||
res = res.add(new BigInteger(num.substring(0, num.length() - 1), 8));
|
||||
} else {
|
||||
res = res.add(new BigInteger(num));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user