-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResistanceCalculator
More file actions
133 lines (117 loc) · 3.06 KB
/
ResistanceCalculator
File metadata and controls
133 lines (117 loc) · 3.06 KB
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
#include <stdio.h>
#include <math.h>
#define CALCULATE 1
#define HELP 2
#define EXIT 3
int bandColor[3];
float tolerance = 0.10;
int calculate()
{
printf("These are the color choices for the bands\n");
printf("\t0. black\n");
printf("\t1. brown\n");
printf("\t2. red\n");
printf("\t3. orange\n");
printf("\t4. yellow\n");
printf("\t5. green\n");
printf("\t6. blue\n");
printf("\t7. violet\n");
printf("\t8. grey\n");
printf("\t9. white\n");
printf("\t10. gold\n");
printf("\t11. silver\n");
printf("Pleas enter the color for band 1\n");
scanf("%d", &bandColor[0]);
while(0>bandColor[0] || 9< bandColor[0])
{
printf("Please enter a valid number\n");
printf("Pleas enter the color for band 1\n");
scanf("%d", &bandColor[0]);
}
printf("Pleas enter the color for band 2\n");
scanf("%d", &bandColor[1]);
while(0>bandColor[1] || 9< bandColor[1])
{
printf("Please enter a valid number\n");
printf("Pleas enter the color for band 2\n");
scanf("%d", &bandColor[1]);
}
printf("Pleas enter the color for band 3\n");
scanf("%d", &bandColor[2]);
while(0>bandColor[2] || 11< bandColor[2])
{
printf("Please enter a valid number\n");
printf("Pleas enter the color for band 3\n");
scanf("%d", &bandColor[2]);
}
printf("Pleas enter the color for band 4\n");
scanf("%d", &bandColor[3]);
while(10>bandColor[3] || 11< bandColor[3])
{
printf("Please enter a valid number\n");
printf("Pleas enter the color for band 4\n");
scanf("%d", &bandColor[3]);
}
printf("\nThe Value of the resistor is.\n");
tolerance = bandColor[3] == 10 ? 0.05:0.10;
metricScale();
main();
}
char *getScale()
{
switch(bandColor[2])
{
case 2:
case 3:
case 4:
return " K-ohms";
case 5:
case 6:
case 7:
return " M-ohms";
case 8:
case 9:
return " G-ohms";
default:
return " ohms";
}
}
int metricScale()
{
float power = bandColor[2] == 10 ? 0 :
bandColor[2] == 11 ? -1 : (bandColor[2]+1)%3;
float value = ((float)bandColor[0] + (float)bandColor[1]/10.0) * pow(10,power);
tolerance = value * tolerance;
printf("%.2f ~ %.2f %s\n", value, tolerance, getScale());
}
int help()
{
printf("you will be prompted to enter the color of each band 1 - 4.\n");
printf("The program will then give you the resistance in ohms of the resistor.\n\n");
return main();
}
int main()
{
int userInput;
printf("Please choose an option.\n");
printf("\t1. calculate the resistance value\n");
printf("\t2. Help\n");
printf("\t3. Exit\n");
scanf("%d", &userInput);
switch(userInput)
{
case CALCULATE:
calculate();
break;
case HELP:
help();
break;
case EXIT:
printf("Have a Nice Day!\n");
break;
default:
printf("Please Enter a Valid Number.\n");
main();
break;
}
}