-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathQuestion92.java
More file actions
134 lines (129 loc) · 3.13 KB
/
Copy pathQuestion92.java
File metadata and controls
134 lines (129 loc) · 3.13 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Scanner;
public class Question92{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
char seq[] = input.toCharArray();
int outflag=0;
for(int i=0; i<seq.length; i++){
seq[i]=gui_map(seq[i]);
if (seq[i]=='R' || seq[i]=='S' || seq[i]=='F' ||seq[i]=='C')
break;
}
//Print Mapped GUI (remove comment to see the mapped sequence input)
/*
for(int i=0; i<seq.length; i++){
System.out.print(seq[i]);
}
*/
// Use double type of values for entire calculation
double operand1=0.0;
String o1="";
double operand2=0.0;
String o2="";
double output=0.0;
// Perform calculaton operations
outerloop:
for(int i=0; i<seq.length; i++){
if(seq[i]=='C'){ //Clear
operand1=0.0;
operand2=0.0;
output=0.0;
outflag=0;
break outerloop;
}else if(seq[i]=='R'){ //Square Root
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.sqrt(operand1);
outflag=1;
break outerloop;
}
else if(seq[i]=='S'){ //Square
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.pow(operand1,2);
outflag=1;
break outerloop;
}else if(seq[i]=='F'){ //Inverse
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
output=Math.pow(operand1,-1);
outflag=1;
break outerloop;
}else{
int r=0;
if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='*'||seq[i]=='='){
for(int j=0; j<i; j++){
o1+=Character.toString(seq[j]);
}
operand1=Double.parseDouble(o1);
for(int k=i+1; k<seq.length; k++){
if(seq[k]=='='){
outflag=1;
operand2=Double.parseDouble(o2);
if(seq[i]=='+'){
output=operand1+operand2;
}else if(seq[i]=='-'){
output=operand1-operand2;
}else if(seq[i]=='/'){
output=operand1/operand2;
}else if(seq[i]=='*'){
output=operand1*operand2;
}
break outerloop;
}else{
o2+=Character.toString(seq[k]);
}
}
}
}
}
// Check if output is available and print the output
if(outflag==1)
System.out.print(output);
}// The main() method ends here.
// A method that takes a character as input and returns the corresponding GUI character
static char gui_map(char in){
char out = 'N';// N = Null/Empty
char gm[][]={{'a','R'}
,{'b','0'}
,{'c','.'}
,{'d','='}
,{'e','1'}
,{'f','2'}
,{'g','3'}
,{'h','+'}
,{'i','4'}
,{'j','5'}
,{'k','6'}
,{'l','-'}
,{'m','7'}
,{'n','8'}
,{'o','9'}
,{'p','*'}
,{'q','S'}
,{'r','F'}
,{'s','C'}
,{'t','/'}};
/*
R = Square root
C = Clear/Restart
F = Fraction
S = Square
*/
// Checking for maps
for(int i=0; i<gm.length; i++){
if(gm[i][0]==in){
out=gm[i][1];
break;
}
}
return out;
}
}