Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Final-Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ys554
Final-Project
Commits
96424f52
Commit
96424f52
authored
6 years ago
by
ys554
Browse files
Options
Downloads
Patches
Plain Diff
assn2
parent
a20b8c07
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
assn2/README.md
+3
-0
3 additions, 0 deletions
assn2/README.md
assn2/calc.py
+148
-0
148 additions, 0 deletions
assn2/calc.py
with
151 additions
and
0 deletions
assn2/README.md
0 → 100644
+
3
−
0
View file @
96424f52
The program is written python 3!
Thank you :)
This diff is collapsed.
Click to expand it.
assn2/calc.py
0 → 100755
+
148
−
0
View file @
96424f52
#!/usr/bin/env python3
#Yegeon Seo
#CS265 Assn2
import
fileinput
# Stack class and it's ADTs
class
Stack
:
def
__init__
(
self
):
self
.
items
=
[]
self
.
precedence
=
{
'
+
'
:
1
,
'
-
'
:
1
,
'
*
'
:
2
,
'
/
'
:
2
,
'
%
'
:
2
}
def
isEmpty
(
self
):
return
self
.
items
==
[]
def
push
(
self
,
item
):
self
.
items
.
insert
(
0
,
item
)
def
pop
(
self
):
return
self
.
items
.
pop
(
0
)
def
peek
(
self
):
return
self
.
items
[
0
]
def
size
(
self
):
return
len
(
self
.
items
)
# Method to check if a token is an operator
def
isOperator
(
exp
):
operator
=
[
"
+
"
,
"
-
"
,
"
*
"
,
"
/
"
,
"
%
"
]
if
exp
in
operator
:
return
True
else
:
return
False
# Method to check if a token is an operand
def
isOperand
(
exp
):
if
isOperator
(
exp
)
or
exp
==
"
(
"
or
exp
==
"
)
"
:
return
False
else
:
return
True
# Method to check and compare the values of precedence
def
notGreater
(
stack
,
i
):
try
:
a
=
stack
.
precedence
[
i
]
b
=
stack
.
precedence
[
stack
.
peek
()]
return
True
if
a
<=
b
else
False
except
KeyError
:
return
False
# Method that converts an infix expression to a postfix expression
def
infix2postfix
(
infixList
):
stack
=
Stack
()
postfixList
=
[]
# Append a right parenthesis to the end of the expression
# and push a new left parenthesis to the stack
infixList
.
append
(
"
)
"
)
stack
.
push
(
"
(
"
)
for
token
in
infixList
:
# If the token is an operand, append it to the postfix expression
if
isOperand
(
token
):
postfixList
.
append
(
token
)
# If the token is a left parenthesis, push it to the stack
elif
token
==
"
(
"
:
stack
.
push
(
token
)
# If the token is a right parenthesis,
# pop operators from the stack and append
# to the postfix expression, until a left
# parenthesis is encountered on the stack.
# Then remove and discard the left parenthesis
elif
token
==
"
)
"
:
while
(
(
not
stack
.
isEmpty
())
and
stack
.
peek
()
!=
"
(
"
):
a
=
stack
.
pop
()
postfixList
.
append
(
a
)
if
(
not
stack
.
isEmpty
()
and
stack
.
peek
()
!=
"
(
"
):
return
False
else
:
stack
.
pop
()
# If the token is an operator, then pop operators
# from the stack and append to the postfix expression
# while the operators have equal or higher precedence
# than the current token. Then push current operator to stack
else
:
while
(
not
stack
.
isEmpty
()
and
notGreater
(
stack
,
token
)):
postfixList
.
append
(
stack
.
pop
())
stack
.
push
(
token
)
# Clear out the stack if it's not empty after all procedures
# (This should not run if all the inputs are correct)
while
not
stack
.
isEmpty
():
postfixList
.
append
(
stack
.
pop
())
# Take the output and pass it to the evalPostfix function
evalPostfix
(
postfixList
)
def
evalPostfix
(
postfix
):
stack
=
Stack
()
for
token
in
postfix
:
# If the token is an operand, push it to the stack
if
isOperand
(
token
):
stack
.
push
(
token
)
# if the token is an operator, assign x and y as top 2 values in the stack
# and do the math
else
:
y
=
int
(
stack
.
pop
())
x
=
int
(
stack
.
pop
())
if
token
==
"
+
"
:
result
=
x
+
y
elif
token
==
"
-
"
:
result
=
x
-
y
elif
token
==
"
*
"
:
result
=
x
*
y
elif
token
==
"
/
"
:
result
=
x
/
y
else
:
result
=
x
%
y
stack
.
push
(
result
)
# Print the output in postfix expression = result format
print
(
"
"
.
join
(
postfix
),
"
=
"
,
stack
.
peek
())
# Driver program that reads in a file and pass it to infix2postfix function above
for
line
in
fileinput
.
input
():
infixInput
=
[]
newLine
=
line
.
split
()
infix2postfix
(
newLine
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment