You are on page 1of 7

import java.util.

*;
import java.lang.String;
import javax.swing.*;
public class Calculator {
static Scanner reader = new Scanner(System.in);
//‫םינתשמ‬
static String input, inp;
static int output;
static String[] numbers, oprt;
static int[] square_p_start, square_p_end, p_start, p_end, oprt_place;

//##########‫םינמיס תלבק‬#############
public static void calculator(){
//‫םירפסמה לכ תלבק‬
numbers = input.split("[+\\-\\/\\*\\^]");
//‫םינמיסה לכ תלבק‬
set_spec();
//‫םיירגוס ימוקימ תלבק‬
square_parentheses_start();
square_parentheses_end();
parentheses_start();
parentheses_end();
}
//‫בושיח ינמיס תלבק‬
public static void set_spec(){
String character = "~";
for (int i = 0; i<numbers.length; i++){
inp = inp.replaceAll(numbers[i], character);
}
inp = inp.substring(1);
//‫םירוטרפואה לכ תלבק‬
oprt = inp.split("["+character+"]");
}
public static int spec_num (){
int x = 0;
for (int i = 0; i<inp.length(); i++){
if (! Character.isDigit(inp.charAt(i))){
x++;
}
}
return x;
}
//‫םיירגוס יימוקימ תלבק‬
public static int square_p_start_num (){
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("[")){
x++;
}
}
return x;
}
public static void square_parentheses_start(){
square_p_start = new int[square_p_start_num()];
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("[")){
square_p_start[x] = i;
x++;
}
}
}
public static int square_p_end_num (){
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("]")){
x++;
}
}
return x;
}
public static void square_parentheses_end (){
square_p_end = new int[square_p_end_num()];
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("]")){
square_p_end[x] = i;
// System.out.println("ans["+x+"] = "+ans[x]);
x++;
}
}
}
public static int p_start_num (){
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("(")){
x++;
}
}
return x;
}
public static void parentheses_start (){
p_start = new int[p_start_num()];
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals("(")){
p_start[x] = i;
//System.out.println("ans["+x+"] = "+ans[x]);
x++;
}
}
}
public static int p_end_num (){
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals(")")){
x++;
}
}
return x;
}
public static void parentheses_end (){
p_end = new int[p_end_num()];;
int x = 0;
for (int i = 0; i<oprt.length; i++){
if (oprt[i].equals(")")){
p_end[x] = i;
//System.out.println("ans["+x+"] = "+ans[x]);
x++;
}
}
}
//#########‫םינותנ בושיח‬###########
public static double calculate_oprator (String oprator, double num1, dou
ble num2){
double sum;
if (oprator.equals("+")) {
sum = num1+num2;
}
else if (oprator.equals("-")){
sum = num1-num2;
}
else if (oprator.equals("*")){
sum = num1*num2;
}
else if (oprator.equals("/")){
sum = num1/num2;
}
else if (oprator.equals("^")){
sum = Math.pow(num1,num2);
}
else{
sum = 0;
}
//System.out.println(sum);
return sum;
}
public static double calculate_basic (String[] oprators, String[] number
s){
double calc=0, sum = 0, num_1,num_2;
String[] numbers_copy = numbers, oprators_copy = oprators;
int i = 0, j = 0, c, check = 0, elementToDelete;

//###########calculating ^
for (c=0; oprators.length > 0 && check == 0;c++){
num_1 = num_2 = i = j = check = elementToDelete = 0;
if (oprators.length == 1){
c = 0;
}
if (oprators[c].equals("^")){
num_1 = Double.parseDouble(numbers[c]);
num_2 = Double.parseDouble(numbers[c+1]);
calc = calculate_oprator(oprators[c], num_1, num
_2);
//‫םירפסמ תפלחהו תקיחמ‬
numbers_copy = new String[numbers.length];
numbers_copy = numbers;
numbers = new String[numbers_copy.length-1];
elementToDelete = c;
while (i < numbers_copy.length && j < numbers.le
ngth) {
if (i == elementToDelete) {
i++;
}
else {
numbers[j] = numbers_copy[i];
i++;
j++;
}
}
numbers[elementToDelete] = Double.toString(calc)
;
//‫םירוטורפוא תקיחמ‬
i=0;
j=0;
oprators_copy = new String[oprators.length];
oprators_copy = oprators;
oprators = new String[oprators_copy.length-1];
elementToDelete = c;
while (i < oprators_copy.length && j < oprators.
length) {
if (i == elementToDelete) {
i++;
}
else {
oprators[j] = oprators_copy[i];
i++;
j++;
}
}
c=0;
}else if (oprators.length == 1 || c == oprators.length-1
){
check = 1;
}
}
//################# Calculating / and *\
num_1 = num_2 = i = j = check = elementToDelete = 0;
for (c=0; oprators.length > 0 && check == 0;c++){
num_1 = num_2 = i = j = check = elementToDelete = 0;
if (oprators.length == 1){
c = 0;
}
if (oprators[c].equals("*") || oprators[c].equals("/")){
num_1 = Double.parseDouble(numbers[c]);
num_2 = Double.parseDouble(numbers[c+1]);
calc = calculate_oprator(oprators[c], num_1, num
_2);
//‫םירפסמ תפלחהו תקיחמ‬
numbers_copy = new String[numbers.length];
numbers_copy = numbers;
numbers = new String[numbers_copy.length-1];
elementToDelete = c;
while (i < numbers_copy.length && j < numbers.le
ngth) {
if (i == elementToDelete) {
i++;
}
else {
numbers[j] = numbers_copy[i];
i++;
j++;
}
}
numbers[elementToDelete] = Double.toString(calc)
;
//‫םירוטורפוא תקיחמ‬
i=0;
j=0;
oprators_copy = new String[oprators.length];
oprators_copy = oprators;
oprators = new String[oprators_copy.length-1];
elementToDelete = c;
while (i < oprators_copy.length && j < oprators.
length) {
if (i == elementToDelete) {
i++;
}
else {
oprators[j] = oprators_copy[i];
i++;
j++;
}
}
c=0;
}else if (oprators.length == 1 || c == oprators.length-1
){
check = 1;
}
}
//############calculating - and +
num_1 = num_2 = i = j = check = elementToDelete = 0;
for (c=0; oprators.length > 0 && check == 0;c++){
num_1 = num_2 = i = j = check = elementToDelete = 0;
if (oprators.length == 1){
c = 0;
}
if (oprators[c].equals("+") || oprators[c].equals("-")){
num_1 = Double.parseDouble(numbers[c]);
num_2 = Double.parseDouble(numbers[c+1]);
calc = calculate_oprator(oprators[c], num_1, num
_2);
//‫םירפסמ תפלחהו תקיחמ‬
numbers_copy = new String[numbers.length];
numbers_copy = numbers;
numbers = new String[numbers_copy.length-1];
elementToDelete = c;
while (i < numbers_copy.length && j < numbers.le
ngth) {
if (i == elementToDelete) {
i++;
}
else {
numbers[j] = numbers_copy[i];
i++;
j++;
}
}
numbers[elementToDelete] = Double.toString(calc)
;
//‫םירוטורפוא תקיחמ‬
i=0;
j=0;
oprators_copy = new String[oprators.length];
oprators_copy = oprators;
oprators = new String[oprators_copy.length-1];
elementToDelete = c;
while (i < oprators_copy.length && j < oprators.
length) {
if (i == elementToDelete) {
i++;
}
else {
oprators[j] = oprators_copy[i];
i++;
j++;
}
}
c=0;
}else if (oprators.length == 1 || c == oprators.length-1
){
check = 1;
}
}
sum = Double.parseDouble(numbers[0]);
return sum;
}
//##########‫ןובשח תולועפ רדס‬########
//##‫םיירגוס‬##

//#############‫ישאר‬#############
public static void main(String[] args){
//‫טלק‬
//String y_n = "y";
int ans = JOptionPane.YES_OPTION;
//‫טלק תוניקת תקידב‬
//System.out.println(calculate_oprator("+", 3, 2));
//while (y_n.equals("y")) {
while (ans == JOptionPane.YES_OPTION) {
//‫טלק‬
//System.out.println("‫ליגרת םושר‬: ");
//inp = input = reader.next();
inp = input = JOptionPane.showInputDialog("‫ליגרת םושר‬:");
//‫םינותנ תרדגה‬
calculator();
//‫טלק תוניקת תקידב‬
//‫בושיח‬
//System.out.println("----> ‫האצות‬: "+calculate_basic(oprt, numbers
));
JOptionPane.showMessageDialog(null, "----> ‫האצות‬: "+calculate_basi
c(oprt, numbers));
//System.out.println("‫(ךישמהל םאה‬y/n)? ");
//y_n = reader.next();
ans = JOptionPane.showConfirmDialog(null, "Want more?",
"‫"?בושיח דוע‬, JOptionPane.YES_NO_OPTION);
/*while (! y_n.equals("y") && ! y_n.equals("n")){
System.out.println("‫(ךישמהל םאה‬y/n)? ");
y_n = reader.next();
}*/
}
//System.out.println("############‫המייתסה תינכות‬#############");
JOptionPane.showMessageDialog(null, "############‫המייתסה תינכות‬############

}
}

You might also like