2020-04-01 15:19:57 +03:00
|
|
|
// Q: What's this?
|
|
|
|
// A: This is a mini "home-made" calculator. You may also regard it as a very elementary version of "interpreter".
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
const (
|
|
|
|
NUMERIC_CHAR = [`0`,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`.`,`e`,`E`]
|
|
|
|
)
|
|
|
|
|
2020-04-26 07:39:23 +03:00
|
|
|
// Convert expression to Reverse Polish Notation.
|
2020-04-01 15:19:57 +03:00
|
|
|
fn expr_to_rev_pol(expr string) ?[]string {
|
|
|
|
if expr == '' {
|
|
|
|
return error('err: empty expression')
|
|
|
|
}
|
|
|
|
mut stack := []string
|
|
|
|
mut rev_pol := []string
|
|
|
|
mut pos := 0
|
|
|
|
for pos<expr.len {
|
|
|
|
mut end_pos := pos
|
|
|
|
for end_pos<expr.len && expr[end_pos] in NUMERIC_CHAR {
|
|
|
|
end_pos++
|
|
|
|
}
|
|
|
|
if end_pos>pos {
|
|
|
|
stack << expr[pos..end_pos]
|
|
|
|
pos = end_pos
|
|
|
|
}
|
|
|
|
else if end_pos==pos {
|
|
|
|
op := expr[pos].str()
|
|
|
|
match op {
|
2020-04-26 07:39:23 +03:00
|
|
|
'(' {
|
2020-04-01 15:19:57 +03:00
|
|
|
stack << op
|
|
|
|
}
|
|
|
|
'*', '/' {
|
2020-04-26 07:39:23 +03:00
|
|
|
for stack.len>0 && stack.last() !in ['(', '+', '-'] {
|
2020-04-01 15:19:57 +03:00
|
|
|
rev_pol << stack.last()
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
}
|
|
|
|
stack << op
|
|
|
|
}
|
|
|
|
'+', '-' {
|
|
|
|
for stack.len>0 && stack.last() != '(' {
|
|
|
|
rev_pol << stack.last()
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
}
|
|
|
|
stack << op
|
|
|
|
}
|
|
|
|
')' {
|
|
|
|
for stack.len>0 && stack.last() != '(' {
|
|
|
|
rev_pol << stack.last()
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
}
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return error('err: invalid character `${op}`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for stack.len>0 {
|
|
|
|
top := stack.last()
|
2020-04-26 07:39:23 +03:00
|
|
|
rev_pol << top
|
2020-04-01 15:19:57 +03:00
|
|
|
stack.delete(stack.len-1)
|
|
|
|
}
|
|
|
|
return rev_pol
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the result of Reverse Polish Notation.
|
|
|
|
fn eval_rev_pol(rev_pol []string) ?f64 {
|
|
|
|
mut stack := []f64
|
|
|
|
for item in rev_pol {
|
|
|
|
if is_num_string(item) {
|
|
|
|
stack << item.f64()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if stack.len>=2 {
|
|
|
|
oprand_r := stack.last()
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
oprand_l := stack.last()
|
|
|
|
stack.delete(stack.len-1)
|
|
|
|
match item {
|
|
|
|
'+' { stack << oprand_l+oprand_r }
|
|
|
|
'-' { stack << oprand_l-oprand_r }
|
|
|
|
'*' { stack << oprand_l*oprand_r }
|
2020-04-26 07:39:23 +03:00
|
|
|
'/' {
|
2020-04-01 15:19:57 +03:00
|
|
|
if oprand_r == 0 {
|
|
|
|
return error('err: divide by zero')
|
|
|
|
}
|
|
|
|
stack << oprand_l/oprand_r
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return error('err: invalid expression')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_num_string(str string) bool {
|
|
|
|
for c in str {
|
2020-04-26 07:39:23 +03:00
|
|
|
if c !in NUMERIC_CHAR {
|
2020-04-01 15:19:57 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println('Please enter the expression you want to calculate, e.g. 1e2+(3-2.5)*6/1.5 .')
|
|
|
|
println('Enter \'exit\' or \'EXIT\' to quit.')
|
|
|
|
mut expr_count := 0
|
|
|
|
for {
|
|
|
|
expr_count++
|
|
|
|
print('[$expr_count] ')
|
|
|
|
expr := os.get_line().trim_space()
|
|
|
|
if expr in ['exit', 'EXIT'] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rev_pol := expr_to_rev_pol(expr) or {
|
|
|
|
eprintln(err)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-26 07:39:23 +03:00
|
|
|
res := eval_rev_pol(rev_pol) or {
|
2020-04-01 15:19:57 +03:00
|
|
|
eprintln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
println(res)
|
|
|
|
}
|
2020-04-26 07:39:23 +03:00
|
|
|
}
|