FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Programs/postfix.java at main · VirajPatidar/Java-Programs · GitHub
VirajPatidar
/
Java-Programs
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java-Programs
/
postfix.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (58 loc) · 1.87 KB
Breadcrumbs
Java-Programs
/
postfix.java
Copy path
File metadata and controls
60 lines (58 loc) · 1.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.*;
class
postfix
{
static
int
precedence
(
char
ch
)
{
switch
(
ch
)
{
case
'+'
:
case
'-'
:
return
1
;
case
'*'
:
case
'/'
:
return
2
;
case
'^'
:
return
3
;
}
return
-
1
;
}
static
String
infixToPostfix
(
String
exp
){
String
result
=
new
String
(
""
);
Stack
<
Character
>
stack
=
new
Stack
<>();
for
(
int
i
=
0
;
i
<
exp
.
length
(); ++
i
){
char
c
=
exp
.
charAt
(
i
);
if
(
Character
.
isLetterOrDigit
(
c
))
result
+=
c
;
else
if
(
c
==
'('
)
stack
.
push
(
c
);
else
if
(
c
==
')'
){
while
(!
stack
.
isEmpty
() &&
stack
.
peek
() !=
'('
)
result
+=
stack
.
pop
();
if
(!
stack
.
isEmpty
() &&
stack
.
peek
() !=
'('
)
return
"Invalid Expression"
;
// invalid expression
else
stack
.
pop
();
}
else
{
while
(!
stack
.
isEmpty
() &&
precedence
(
c
) <=
precedence
(
stack
.
peek
())){
if
(
stack
.
peek
() ==
'('
)
return
"Invalid Expression"
;
result
+=
stack
.
pop
();
}
stack
.
push
(
c
);
}
}
while
(!
stack
.
isEmpty
()){
if
(
stack
.
peek
() ==
'('
)
return
"Invalid Expression"
;
result
+=
stack
.
pop
();
}
return
result
;
}
public
static
void
main
(
String
[]
args
){
Scanner
sc
=
new
Scanner
((
System
.
in
));
System
.
out
.
println
(
"Enter the infix expression to get its postfix form"
);
String
exp
=
sc
.
nextLine
();
System
.
out
.
println
(
infixToPostfix
(
exp
));
sc
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL