summaryrefslogtreecommitdiff
path: root/c/menu/calculator.c
blob: ff26f07a28f0612ee38769811228301c87f6ce4a (plain)
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
/* Menu Thing for RVController - Calculator
 * A product of Advanced Mesecons Devices, a Cheapie Systems company
 * This is free and unencumbered software released into the public domain.
 * See http://unlicense.org/ for more information */

#include <stdint.h>
#include <stdbool.h>
#include "rvcontroller-ecalls.h"

void runCalculator(void) {
	char input[2] = {0};
	int32_t num1 = 0;
	int32_t num2 = 0;
	int32_t result = 0;

	printstr("\n\n\n\n\n\n");
	printstr("Enter number 1:\n> ");
	num1 = readint();
	printint(num1);
	printstr("\nEnter number 2:\n> ");
	num2 = readint();
	printint(num2);

	bool opok = false;
	while (!opok) {
		printstr("\nSelect operation:\n[+-*/%&|^<<>>] > ");
		readstr(input,2);
		printstr(input);

		switch (input[0]) {
			case '<':
			case '>':
				// Duplicate the character for these two to give the illusion that I bothered to store both
				// Then fall through
				printstr(input);
			case '+':
			case '-':
			case '*':
			case '/':
			case '%':
			case '&':
			case '|':
			case '^':
				printchar('\n');
				opok = true;
				break;
			default:
				printstr("\nInvalid operation\n");
		}
	}

	switch (input[0]) {
		case '+':
			printint(num1+num2);
			break;
		case '-':
			printint(num1-num2);
			break;
		case '*':
			printint(num1*num2);
			break;
		case '/':
			printint(num1/num2);
			break;
		case '%':
			printint(num1%num2);
			break;
		case '&':
			printint(num1&num2);
			break;
		case '|':
			printint(num1|num2);
			break;
		case '^':
			printint(num1^num2);
			break;
		case '<':
			if (num2 < 0 || num2 > 31) {
				printstr("Nice try.");
			} else {
				printint(num1<<num2);
			}
			break;
		case '>' :
			if (num2 < 0 || num2 > 31) {
				printstr("Nice try.");
			} else {
				printint(num1>>num2);
			}
			break;
	}

	printstr("\nPress any key");
	readstr(input,1);
}