Skip to content

Commit e46d0c7

Browse files
authored
[Dana] (Simple) Calculator Program (#65)
* [Dana] (Simple) Calculator Program The program takes reads the input line by line and on each line it expects an integer followed by and operator and then another integer, any other character among them is ignored. If the input is valid it outputs the operation's result otherwise it fails on that line with a message "Invalid" There is also a sample input and output for the program * [Dana] Calculator Program, better comments
1 parent 201c03d commit e46d0c7

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

dana/programs/calculator.dana

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
def main
2+
# Helper Function
3+
4+
def is_digit is byte: c as byte
5+
if '0' <= c and c <= '9': return: true
6+
else: return: false
7+
8+
def is_operator is byte: c as byte
9+
if c = '+' or c = '-' or c = '*' or c = '/':
10+
return: true
11+
else:
12+
return false
13+
14+
(*
15+
Conversion from part of a string to an int
16+
[from, to)
17+
*)
18+
def string_to_int is int: s as byte[], from to as int
19+
var i res pow is int
20+
21+
res := 0
22+
pow := 1
23+
24+
i := to - 1
25+
loop:
26+
if i < from: break
27+
res := res + pow * extend(s[i] - '0')
28+
pow := pow * 10
29+
i := i - 1
30+
31+
return: res
32+
33+
(*
34+
Simple Calculator
35+
36+
Input: lines of the following form:
37+
<Num1> <Op> <Num2>
38+
where <NumX> is a positive integer
39+
and <Op> is one of +, -, *, /
40+
Any other characters among them get ignored.
41+
42+
Output: lines with the results
43+
or a message "Invalid" in case of failure
44+
*)
45+
46+
var buf is byte[256]
47+
var idx len start end a b is int
48+
var op is byte
49+
50+
loop lines:
51+
readString: 256, buf
52+
53+
# Stop if we got nothing
54+
len := strlen(buf)
55+
if len = 0: break: lines
56+
57+
idx := 0
58+
59+
loop a_start:
60+
if idx == len:
61+
writeString: "Invalid\n"
62+
continue: lines
63+
elif is_number(buf[idx]):
64+
break: a_start
65+
idx := idx + 1
66+
start := idx;
67+
68+
loop a_end:
69+
if idx == len:
70+
writeString: "Invalid\n"
71+
continue: lines
72+
elif not is_number(buf[idx]):
73+
break: a_end
74+
idx := idx + 1
75+
end := idx;
76+
77+
a := string_to_int(buff, start, end);
78+
79+
loop op_detect:
80+
if idx == len:
81+
writeString: "Invalid\n"
82+
continue: lines
83+
elif is_operator(buf[idx]):
84+
break: op_detect
85+
idx := idx + 1
86+
op := buf[idx]
87+
88+
loop b_start:
89+
if idx == len:
90+
writeString: "Invalid\n"
91+
continue: lines
92+
elif is_number(buf[idx]):
93+
break: b_start
94+
idx := idx + 1
95+
start := idx;
96+
97+
loop b_end:
98+
if idx == len or not is_number(buf[idx]):
99+
break: b_end
100+
idx := idx + 1
101+
end := idx;
102+
103+
b := string_to_int(buff, start, end);
104+
105+
# We have finished parsing now we can calculate
106+
107+
if op = '+':
108+
writeInteger: a + b
109+
elif op = '-':
110+
writeInteger: a - b
111+
elif op = '*':
112+
writeInteger: a * b
113+
elif op = '/':
114+
writeInteger: a / b
115+
else:
116+
writeString: "Invalid\n"
117+
118+
exit
119+

dana/programs/calculator.input

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1+2
2+
4-3
3+
5*2
4+
100/4
5+
25 + 10
6+
10 * 3 dsfdfadf
7+
asdf 5sdf -adfd2
8+
5-
9+
35+
10+
+7
11+
2345 _ 2342

dana/programs/calculator.output

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
3
2+
1
3+
10
4+
25
5+
35
6+
30
7+
3
8+
Invalid
9+
Invalid
10+
Invalid
11+
Invalid

0 commit comments

Comments
 (0)