oop-labs-collection/labs/1/Main.java

145 lines
5.5 KiB
Java
Raw Permalink Normal View History

2023-03-11 08:52:16 +02:00
import java.util.Objects;
public class Main {
public static void main(String[] args) {
byte a = 1, b = 1, n = 10, m = 10;
boolean simplify_math = true;
int iteration_amount = 1;
byte verbosity = 1;
int p = 0;
boolean run_safe = false;
while (p < args.length) {
try {
if (Objects.equals(args[p], "-h")) {
System.out.print("Usage: java Main [options]\nOptions:\n -v <byte> Sets verbosity level (0-2, default is 1\n -a, -b, -n, -m <byte> Sets default values for a, b, n and m variables respectively\n -s <bool> Use simplified algorithm to calculate the desired value (true/false, default is true)\n -t <int> Enables benchmarking mode and performs selected calculation <int> times\n");
System.exit(0);
}
else if (Objects.equals(args[p], "-v")) {
verbosity = (byte) Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-a")) {
a = (byte) Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-b")) {
b = (byte) Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-n")) {
n = (byte) Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-m")) {
m = (byte) Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-t")) {
iteration_amount = Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "-s")) {
simplify_math = Boolean.parseBoolean(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "--safe")) {
run_safe = true;
p++;
}
else {
if (verbosity >= 1) {
System.err.println("Unknown argument found: " + args[p] + "\nPlease, use 'java Main -h' for help");
}
System.exit(1);
}
}
catch (Exception e) {
System.err.println("Exception while parsing CLI arguments: " + e);
System.err.println("Aborting execution.");
System.exit(1);
}
}
if (verbosity >= 2) {
System.out.println(String.format("Using arguments: a=%d, b=%d, n=%d, m=%d", a, b, n, m));
}
if (simplify_math == true) {
if (b <= 0 && m >= 0) {
System.err.println("Preventing division by zero!\nAborting calculations.");
System.exit(1);
}
else
{
if (a <= 0 && n >= 0) {
System.err.println("Bypassing division by zero with fast math; expect wrong calculations");
if (run_safe) {
System.err.println("--safe flag is set, preventing further execution");
System.exit(1);
}
}
for (int g = 0; g < iteration_amount; g++) {
double result = calc_quick(verbosity, a, n, b, m);
if (iteration_amount == 1) {
if (verbosity >= 1) {
System.out.println(String.format("Result is %.3f", result));
}
else
{
System.out.print(result);
}
}
}
}
}
else
{
if ((a <= 0 && n >= 0) || (b <= 0 && m >= 0)) {
System.err.println("Preventing division by zero!\nAborting calculations.");
System.exit(1);
}
else
{
for (int g = 0; g < iteration_amount; g++) {
double result = calc_generic(verbosity, a, n, b, m);
if (iteration_amount == 1) {
if (verbosity >= 1) {
System.out.println(String.format("Result is %.3f", result));
}
else
{
System.out.print(result);
}
}
}
}
}
}
private static double calc_generic(byte verbosity, byte a, byte n, byte b, byte m) {
double result = 0;
for (byte i = a; i <= n; i++) {
for (byte j = b; j <= m; j++) {
if (verbosity >= 3) {
System.out.print(result);
}
result += ((double) i / (double) j) / ((double) i - (double) 0);
if (verbosity >= 3) {
System.out.println(" + " + (((double)i / (double)j) / ((double)i-(double)0)) + " = " + result);
}
}
}
return result;
}
private static double calc_quick(byte verbosity, byte a, byte n, byte b, byte m) {
double t = 0;
for (byte j = b; j <= m; j++) {if (verbosity >= 3) {System.out.print(t);}; t += (double)1/(double)j; if (verbosity >= 3) {System.out.println(" + " + (double)1/(double)j + " = " + t);} }
return (double) (t * ((double)n-(double)a + (double) 1));
}
}