Upload files to "java/sum"
This commit is contained in:
28
java/sum/Sum.java
Normal file
28
java/sum/Sum.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class Sum {
|
||||||
|
|
||||||
|
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 += (!builder.toString().isEmpty())
|
||||||
|
? Integer.parseInt(builder.toString())
|
||||||
|
: 0;
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += (!builder.toString().isEmpty())
|
||||||
|
? Integer.parseInt(builder.toString())
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
63
java/sum/SumBigDecimalHex.java
Normal file
63
java/sum/SumBigDecimalHex.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
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();
|
||||||
|
int idxOfS = -1;
|
||||||
|
for (char c : arg.toCharArray()) {
|
||||||
|
if (!Character.isWhitespace(c)) {
|
||||||
|
if (c == 's' || c == 'S') {
|
||||||
|
idxOfS = builder.length();
|
||||||
|
}
|
||||||
|
builder.append(c);
|
||||||
|
} else {
|
||||||
|
res = res.add(compute(builder.toString(), idxOfS));
|
||||||
|
idxOfS = -1;
|
||||||
|
builder = new StringBuilder();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res = res.add(compute(builder.toString(), idxOfS));
|
||||||
|
}
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static BigDecimal compute(String num, int idxOfS) {
|
||||||
|
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 (idxOfS != -1) {
|
||||||
|
idxOfS -= 2;
|
||||||
|
String mantissaHex = num.substring(0, idxOfS);
|
||||||
|
String exponentHex = num.substring(idxOfS + 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
45
java/sum/SumBigIntegerOctal.java
Normal file
45
java/sum/SumBigIntegerOctal.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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");
|
||||||
|
int numLength = num.length();
|
||||||
|
if (num.isEmpty()) {
|
||||||
|
res = res.add(BigInteger.ZERO);
|
||||||
|
} else if (
|
||||||
|
num.charAt(numLength - 1) == 'o' || num.charAt(numLength - 1) == 'O'
|
||||||
|
) {
|
||||||
|
res = res.add(
|
||||||
|
new BigInteger(num.substring(0, num.length() - 1), 8)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
res = res.add(new BigInteger(num));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
java/sum/SumDouble.java
Normal file
28
java/sum/SumDouble.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package sum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikita Doschennikov (me@fymio.us)
|
||||||
|
*/
|
||||||
|
public class SumDouble {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double res = 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
java/sum/package-info.java
Normal file
7
java/sum/package-info.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* Tests for <a href="https://www.kgeorgiy.info/courses/prog-intro/homeworks.html#sum">Sum</a> homework
|
||||||
|
* of <a href="https://www.kgeorgiy.info/courses/prog-intro/">Introduction to Programming</a> course.
|
||||||
|
*
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
package sum;
|
||||||
Reference in New Issue
Block a user