Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
added */trojclenka* (or */Ro3*) function to advanced functions
  • Loading branch information
hafihaf123 authored May 12, 2023
1 parent c8bb12f commit 4a8ce7d
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions calculator_5,0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import java.util.*;

// Compiler version JDK 11.0.2

class calculator
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an expression: ");
String ex = sc.nextLine();
String res = "";
if (ex.toCharArray()[0] == '/') {
//System.out.println("work in progress");
Adv adv = new Adv();
switch (ex) {
case "/":
System.out.println("options:");
System.out.println("1. prism (p)");
System.out.println("2. quadratic function (q)");
System.out.println("3. trojclenka - rule of three (3)");
String option = sc.nextLine();

switch (option) {
case "1":
case "p":
case "prism":
res = adv.prism();
break;

case "2":
case "q":
case "quadratic":
res = adv.quadratic();
break;

case "3":
case "trojclenka":
case "Ro3":
res = adv.trojclenka();
break;
}
break;

case "/prism":
res = adv.prism();
break;

case "/quadratic":
case "/quadr":
res = adv.quadratic();
break;

case "/trojclenka":
case "/Ro3":
res = adv.trojclenka();
break;

default:
res = "Error";
}
}
else {
Expression expression = new Expression();
res = expression.calc(ex);
}
System.out.println(res);
}
}

class Expression
{
public String calc(String ex) {
/*Scanner sc = new Scanner(System.in);
System.out.print("Enter an expression: ");
String ex = sc.nextLine();*/

// Use regular expression to extract all numbers and operators
//String[] tokens = ex.split("(?<=[\\d.])(?=[^\\d.])|(?<=[^\\d.])(?=[\\d.])");
// tokens array now contains all numbers and operators as separate strings

String[] tokens = ex.split("(?<=[\\d.])(?=[^\\d.()])|(?<=[^\\d.()])(?=[\\d.()])");


// Stack to hold numbers and operators
ArrayDeque<Double> nums = new ArrayDeque<>();
ArrayDeque<Character> ops = new ArrayDeque<>();

for (String token : tokens) {
// If the token is a number, push it onto the nums stack
if (token.matches("\\d+(\\.\\d+)?")) {
nums.push(Double.parseDouble(token));
}
// If the token is an operator, push it onto the ops stack
else if (token.matches("[+\\-*/^]")) {
char op = token.charAt(0);

// Pop numbers and perform arithmetic operations based on operator precedence
while (!ops.isEmpty() && hasPrecedence(ops.peek(), op)) {
double num2 = nums.pop();
double num1 = nums.pop();
nums.push(applyOp(num1, num2, ops.pop()));
}
ops.push(op);
}
else {
return ("Invalid token: " + token);
}
}

// Pop remaining operators and perform arithmetic operations
while (!ops.isEmpty()) {
double num2 = nums.pop();
double num1 = nums.pop();
nums.push(applyOp(num1, num2, ops.pop()));
}

// Print the result
return (ex + " = " + nums.pop());
}

// Helper function to check if op1 has higher or equal precedence than op2
private static boolean hasPrecedence(char op1, char op2) {
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return true;
}
if ((op1 == '^') && (op2 == '*' || op2 == '/' || op2 == '+' || op2 == '-')) {
return true;
}
return false;
}

// Helper function to apply arithmetic operations
private static double applyOp(double num1, double num2, char op) {
switch (op) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 == 0) {
System.out.println("Cannot divide by zero.");
System.exit(1);
}
return num1 / num2;
case '^':
return Math.pow(num1, num2);
}
return 0;
}
}

class Adv
{
public String prism()
{
Scanner sc = new Scanner(System.in);
System.out.print("side a: ");
double a = sc.nextDouble();
System.out.print("side b: ");
double b = sc.nextDouble();
System.out.print("side c: ");
double c = sc.nextDouble();
double volume = a*b*c;
double surface = 2*(a*b + b*c + a*c);
return ("volume: " + volume + "\nsurface: " + surface);
}
public String quadratic()
{
Scanner sc = new Scanner(System.in);
System.out.print("a*x^2 + b*x + c = 0\na: ");
double a = sc.nextDouble();
System.out.print("b: ");
double b = sc.nextDouble();
System.out.print("c: ");
double c = sc.nextDouble();
double D = b*b - (4*a*c);
if (D<0) return "no real solution";
else if (D == 0) {
double x = (-b)/(2*a);
return ("one real solution: x = " + x);
}
else {
double D_sqrt = Math.sqrt(D);
double x1 = (-b + D_sqrt)/(2*a);
double x2 = (-b - D_sqrt)/(2*a);
return ("two real solutions: x1 = " + x1 + " ; x2 = " + x2);
}
}
public String trojclenka()
{
Scanner sc = new Scanner(System.in);
System.out.print("a ... b\nc ... x\na:");
double a = sc.nextDouble();
System.out.print("b: ");
double b = sc.nextDouble();
System.out.print("c: ");
double c = sc.nextDouble();
System.out.println("priama alebo nepriama umernost? (p/n)");
char umernost = sc.next().charAt(0);
if (umernost == 'p') {
double x = (b*c)/a;
return ("x = " + x + " (priama umernost)");
}
else if (umernost == 'n') {
double x = (a*b)/c;
return ("x = " + x + " (nepriama umernost)");
}
else return "Err_incorrect_input";
}
}

0 comments on commit 4a8ce7d

Please sign in to comment.