-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric_conversion.c
80 lines (69 loc) · 1.37 KB
/
metric_conversion.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void continuance();
int decimal(int diff);
int main()
{
int unit1, unit2;
double out, value;
char metric[7][4] = {"mm", "cm", "dm", "m", "dam", "hm", "km"};
//char imperial[4][4] = {"in", "ft", "yd", "mi"};
while (1)
{
printf("1:mm\n2:cm\n3:dm\n4:m\n5:dam\n6:hm\n7:km\n");
//printf("\n8:in\n9:ft\n10:yd\n11:mi\n");
printf("\nInsert value:\n");
scanf("%lf", &value);
printf("\nInsert unit to convert from:\n");
scanf("%d", &unit1);
printf("\nInsert unit to convert to:\n");
scanf("%d", &unit2);
if (unit1 > 11 || unit1 < 0 ||
unit2 > 11 || unit2 < 0)
{
printf("Invalid input");
system("sleep 1");
system("clear");
continue;
}
if (unit1 < unit2)
out = value / decimal(unit2 - unit1);
else
out = value * decimal(unit1 - unit2);;
printf("\n%lf%s = %lf%s\n\n", value, metric[unit1-1], out, metric[unit2-1]);
continuance();
}
}
int decimal(int diff)
{
int i = 0;
int dec = 1;
while (i < diff)
{
dec *= 10;
i++;
}
return (dec);
}
void continuance()
{
int input;
while(1)
{
printf("Proceed with another conversion?\n");
printf("-press 1 to proceed\n");
printf("-press 0 to end program\n");
scanf("%d", &input);
system("clear");
if (input==1)
break;
else if (input==0)
exit(0);
else
{
system("clear");
printf("Invalid Input\n");
}
}
}