add all variants for 'Sum' task (hw2)
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package sum;
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
public class Sum {
|
public class Sum {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|||||||
57
java/sum/SumBigDecimalHex.java
Normal file
57
java/sum/SumBigDecimalHex.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumBigDecimalHex {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BigDecimal res = new BigDecimal("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 BigDecimal compute(String num) {
|
||||||
|
BigDecimal res = BigDecimal.ZERO;
|
||||||
|
|
||||||
|
if (num == null || num.isEmpty()) {
|
||||||
|
return res;
|
||||||
|
} else if (num.startsWith("0x") || num.startsWith("0X")) {
|
||||||
|
num = num.toLowerCase();
|
||||||
|
num = num.substring(2);
|
||||||
|
if (num.contains("s")) {
|
||||||
|
|
||||||
|
int sIndex = num.indexOf('s');
|
||||||
|
|
||||||
|
String mantissaHex = num.substring(0, sIndex);
|
||||||
|
String exponentHex = num.substring(sIndex + 1);
|
||||||
|
|
||||||
|
BigInteger mantissa = new BigInteger(mantissaHex, 16);
|
||||||
|
BigInteger exponent = new BigInteger(exponentHex, 16);
|
||||||
|
|
||||||
|
res = res.add(new BigDecimal(mantissa).scaleByPowerOfTen(exponent.negate().intValueExact()));
|
||||||
|
} else {
|
||||||
|
res = res.add(new BigDecimal(new BigInteger(num, 16)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res = new BigDecimal(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
java/sum/SumDouble.java
Normal file
23
java/sum/SumDouble.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumDouble {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double res = 0.0; // 0_0
|
||||||
|
for (String arg : args) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (char c : arg.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
builder.append(c);
|
||||||
|
} else {
|
||||||
|
res += (!builder.toString().isEmpty()) ? Double.parseDouble(builder.toString()) : 0;
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += (!builder.toString().isEmpty()) ? Double.parseDouble(builder.toString()) : 0;
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
37
java/sum/SumDoubleHex.java
Normal file
37
java/sum/SumDoubleHex.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumDoubleHex {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double res = 0;
|
||||||
|
for (String arg : args) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (char c : arg.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
builder.append(c);
|
||||||
|
} else {
|
||||||
|
res += compute(builder.toString());
|
||||||
|
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += compute(builder.toString());
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static double compute(String num) {
|
||||||
|
double res = 0;
|
||||||
|
if (num.isEmpty()) {
|
||||||
|
res += 0;
|
||||||
|
} else if (num.startsWith("0x") || num.startsWith("0X")) {
|
||||||
|
res += (num.contains(".")) ? Double.parseDouble(num) : Long.decode(num);
|
||||||
|
} else {
|
||||||
|
res += Double.parseDouble(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
37
java/sum/SumHex.java
Normal file
37
java/sum/SumHex.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumHex {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int res = 0;
|
||||||
|
for (String arg : args) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (char c : arg.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
builder.append(c);
|
||||||
|
} else {
|
||||||
|
res += compute(builder.toString());
|
||||||
|
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += compute(builder.toString());
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int compute(String num) {
|
||||||
|
int res = 0;
|
||||||
|
if (num.isEmpty()) {
|
||||||
|
res += 0;
|
||||||
|
} else if (num.startsWith("0x") || num.startsWith("0X")) {
|
||||||
|
res += Long.decode(num);
|
||||||
|
} else {
|
||||||
|
res += Integer.parseInt(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
java/sum/SumLongOctal.java
Normal file
41
java/sum/SumLongOctal.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumLongOctal {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
long res = 0;
|
||||||
|
for (String arg : args) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (char c : arg.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
builder.append(c);
|
||||||
|
} else {
|
||||||
|
res += compute(builder.toString());
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += compute(builder.toString());
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long compute(String num) {
|
||||||
|
if (num.isEmpty()) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num.endsWith("o") || num.endsWith("O")) {
|
||||||
|
String oct = num.substring(0, num.length() - 1);
|
||||||
|
|
||||||
|
BigInteger bi = new BigInteger(oct, 8);
|
||||||
|
|
||||||
|
return bi.longValue();
|
||||||
|
} else {
|
||||||
|
return Long.parseLong(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user