summaryrefslogtreecommitdiff
path: root/MassCalc/src/main.c
blob: dd4f9529544aeeae18f0deadd9d1cfa3e9df8626 (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
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
#include <tice.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define EnterKey 5
#define maxline 22

double lbsToG(double lbs);
double gToLbs(double gram);
double ozToG(double oz);
double gToOz(double gram);
double ozToLbs(double oz);
double lbsToOz(double lbs);
double getInput(void);
void output(int operation);

int main(void)
{
	while(true) {
		os_ClrHome();

		printf("MassCalc\nSelect:\n1. Pounds to Grams\n2. Grams to Pounds\n3. Ounces to Grams\n4. Grams to Ounces\n5. Pounds to Ounces\n6. Ounces to Pounds\n7. Exit");
		uint16_t key = os_GetKey();

		if(key == 149)
			break;

		output(key);
		/* Waits for a key press */
		while (!os_GetCSC());
	}
	return 0;
}

void output(int operation)
{
	os_ClrHome();
	double value = 0.0;
	switch(operation) {
		case 143:
			printf("Pounds to Grams\n");
			value = lbsToG(getInput());
			break;
		case 144:
			printf("Grams to Pounds\n");
			value = gToLbs(getInput());
			break;
		case 145:
			printf("Ounces to Grams\n");
			value = ozToG(getInput());
			break;
		case 146:
			printf("Grams to Ounces\n");
			value = gToOz(getInput());
			break;
		case 147:
			printf("Pounds to Ounces\n");
			value = lbsToOz(getInput());
			break;
		case 148:
			printf("Ounces to Pounds\n");
			value = ozToLbs(getInput());
			break;
		default: printf("Invalid");
	}
	printf("\nOutput: %f", value);
}

double getInput(void)
{
	uint8_t key, i = 0;
	char number[maxline];
	char *eptr;
	double value = 0.0;

	printf("Input: ");
	while((key = os_GetKey()) != EnterKey) {
		if(key == 141 || key == 140)
			key--;
		number[i] = (key - 142) + '0';
		printf("%c", number[i++]);
	}
	number[i] = '\0';
	value = strtod(number, &eptr);
	return value;
}

double lbsToG(double lbs)
{
	double gram = 0;
	gram = lbs * 453.59237;
	return gram;
}

double gToLbs(double gram)
{
	double lbs = 0;
	lbs = gram / 453.59237;
	return lbs;
}

double ozToG(double oz)
{
	double gram = 0;
	gram = lbsToG(ozToLbs(oz));
	return gram;
}

double gToOz(double gram)
{
	double oz = 0;
	oz = lbsToOz(gToLbs(gram));
	return oz;
}

double lbsToOz(double lbs)
{
	double oz = 0;
	oz = lbs * 16;
	return oz;
}

double ozToLbs(double oz)
{
	double lbs = 0;
	lbs = oz / 16;
	return lbs;
}