-
Notifications
You must be signed in to change notification settings - Fork 102
/
Question92.java
92 lines (85 loc) · 2.13 KB
/
Question92.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;
// Start the mapping process for each input character
for(int i=0; i<seq.length; i++){
seq[i]=gui_map(seq[i]);
}
//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++){
int r=0;
if(seq[i]=='+'||seq[i]=='-'||seq[i]=='/'||seq[i]=='X'||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]=='X'){
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','.'}
,{'b','0'}
,{'c','='}
,{'d','+'}
,{'e','1'}
,{'f','2'}
,{'g','3'}
,{'h','-'}
,{'i','4'}
,{'j','5'}
,{'k','6'}
,{'l','X'}
,{'m','7'}
,{'n','8'}
,{'o','9'}
,{'p','/'}};
// Checking for maps
for(int i=0; i<gm.length; i++){
if(gm[i][0]==in){
out=gm[i][1];
break;
}
}
return out;
}
}