-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICG1.java
More file actions
118 lines (101 loc) · 1.99 KB
/
Copy pathICG1.java
File metadata and controls
118 lines (101 loc) · 1.99 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
// instead of using variable tokens, I have used number tokens. Pls Bear with me.
import java.util.*;
class ICG1
{
public int countOfOperators(String exp)
{
int c=0;
for(int i=0;i<exp.length();i++)
{
if(exp.charAt(i)=='+' || exp.charAt(i)=='-' || exp.charAt(i)=='*' || exp.charAt(i)=='/')
c++;
}
return c;
}
public void findICG(String exp)
{
int i1=0,i2=0,i3=0,i4=0;
String t[]=new String[10];
char start=exp.charAt(0);
exp=exp.substring(2);
int c=countOfOperators(exp);
exp+="|";
int r=c;
if(c!=0)
{
for(int i=0;i<c+1;i++)
{
if(r!=0)
{
i1=exp.indexOf('*');
i2=exp.indexOf('/');
i3=exp.indexOf('+');
i4=exp.indexOf('-');
if(i1==-1)
i1=99;
if(i2==-1)
i2=99;
if(i3==-1)
i3=99;
if(i4==-1)
i4=99;
if(i1<i2)
{
t[i]=exp.substring(i1-1,i1+2);
String a=Integer.toString(i);
exp=exp.replace(t[i],a);
r--;
}
else if(i1>i2)
{
t[i]=exp.substring(i2-1,i2+2);
String a=Integer.toString(i);
exp=exp.replace(t[i],a);
r--;
}
else if(i3<i4)
{
t[i]=exp.substring(i3-1,i3+2);
String a=Integer.toString(i);
exp=exp.replace(t[i],a);
r--;
}
else if(i3>i4)
{
t[i]=exp.substring(i4-1,i4+2);
String a=Integer.toString(i);
exp=exp.replace(t[i],a);
r--;
}
System.out.println("Expr is : " +exp.substring(0,exp.length()-1));
continue;
}
else
{
t[i]=exp;
}
}
}
else
t[0]=exp;
display(t,start,c);
}
public void display(String t[], char start, int c)
{
int j;
for( j=0;j<c;j++)
{
System.out.println(j+"="+t[j]);
}
System.out.println(start+"="+t[j].substring(0,t[j].length()-1));
}
public static void main(String args[])
{
ICG1 obj=new ICG1();
String exp;
Scanner ob=new Scanner(System.in);
System.out.println("Enter the expr");
exp=ob.next();
obj.findICG(exp);
}
}