forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.java
75 lines (75 loc) ยท 1.7 KB
/
calc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
class BasicCalculator
{
public static void main (String args[])
{
System.out.println ("Enter the string expression: (like- 2-1 + 2 )");
Scanner sc = new Scanner (System.in);
// String input
result (sc.nextLine ());
}
public static void result (String s)
{
// Initialization
int[] opArray = new int[30]; //for making a operation stack
int top = -1; //for stack
int operand = 0;
int answer = 0; // For the on-going answer
int sign = 1; // 1 means positive, -1 means negative (for easy to calcutale)
int i = 0;
char ch;
while (i < s.length ())
{
ch = s.charAt (i);
if (ch == '+')
{
answer += sign * operand;
sign = 1;
operand = 0;
}
else if (ch == '-')
{
answer += sign * operand;
sign = -1;
operand = 0;
}
else if (ch == '(')
{
// (answer outside parenthesis) + (sign * (anwswer from parenthesis))
if (top > 27)
{
System.out.println ("sorry, too many ()");
return;
}
opArray[++top] = answer;
opArray[++top] = sign;
sign = 1;
answer = 0;
}
else if (ch == ')')
{
// (answer outside parenthesis) + (sign * (answer from parenthesis))
answer += sign * operand;
answer *= opArray[top--];
answer += opArray[top--];
operand = 0;
}
else if (ch == ' ')
{
// for " " -space nothing to do
}
else if (Character.isDigit (ch))
{
//operand can be more 9 i.e. more than 1 digit
operand = 10 * operand + (int) (ch - '0');
}
else
{
System.out.println ("invaild input");
return;
}
i++;
}
System.out.println (answer + (sign * operand));
}
}