-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculator.c
165 lines (156 loc) · 4.03 KB
/
calculator.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**************************\
* Atikur Rahman Chitholian *
* CSE 2015-16 *
* University of Chittagong *
\**************************/
#include <stdlib.h>
#include <util/delay.h>
#include "calculator.h"
#include "keyboard.h"
#include "lcd.h"
long double r, a, b; // a and b are operands, r is the result.
int count; // Number of characters displaying on first line.
unsigned short state; // Calculator and Monitor state.
unsigned char op; // Operator: [/, *, +, -].
static char line[] = " "; // Single line, 16 characters long.
static char buffer[16];
static char overflow[] = " Overflow ";
void decide(unsigned char);
void send_digit(unsigned char);
void init_calculator() {
_delay_ms(500);
init_display();
init_keyboard();
reset();
run();
}
double calculate(long double m, char operator, long double n) {
switch (operator) {
case '+':
return r = m + n;
case '-':
return r = m - n;
case '*':
return r = m * n;
case '/':
return r = m / n;
}
return r = m;
}
void show_result() {
move_to(1, 0);
send_string("A=");
char *data = buffer;
if (r <= 999999999999)
dtostrf(r, -14, 12, buffer);
else data = overflow;
send_string(data);
state = 4;
}
void send_digit(unsigned char digit) {
send_data(digit + '0');
}
void run() {
while (1) {
decide(scan_key());
}
}
void reset() {
clear();
send_string("CSE 612: Group 7");
move_to(1, 0);
send_string(" Calculator");
r = a = b = op = state = count = 0;
}
/* States:
0 - Displaying banner.
1 - Input first operand, a.
2 - Middle Operator pressed, op.
3 - Input second operand, b.
4 - Result displayed.
*/
void decide(unsigned char key) {
// Check if it is a digit.
if (key >= '0' && key <= '9')
{
unsigned char digit = key - '0'; // '2' --> 2
switch (state) {
case 0:
if (digit) {
clear(); // Clear screen if input is non-zero.
state = 1;
}
case 1:
if (count == 14) return; // You cannot fillup the screen with a single operand.
a = a * 10 + digit; // append to a.
if (a > 0) {
send_data(key);
count++;
}
break;
case 2:
if (digit) state = 3;
case 3:
b = b * 10 + digit; // append to b.
if (b > 0) {
send_data(key);
count++;
}
break;
case 4:
if (digit) {
line[0] = ' ';
line[1] = ' ';
move_to(0, 0);
send_string(line); // Clear 1st line.
move_to(0, 0);
a = b = op = count = 0;
state = 1;
decide(key); // Recursively capture digit.
return;
}
break;
}
} else {
switch (key) {
case '/':
case '*':
case '+':
case '-':
switch (state) {
case 2:
move_to(0, --count); // Modify the operand displayed.
case 1:
send_data(key);
count++;
state = 2;
break;
case 3:
calculate(a, op, b);
show_result();
case 4:
move_to(0, 0);
line[0] = 'A'; // 'A' represents current result.
line[1] = key; // Operator of the operation.
send_string(line);
move_to(0, 2);
count = 2;
a = r; // Put result into a.
b = 0; // Clear b.
state = 2;
break;
}
op = key;
break;
case '=':
if(state == 3){
calculate(a, op, b);
show_result();
}
break;
case 'C': // "ON/C" button.
if (state != 0) reset();
break;
}
}
}