-
Notifications
You must be signed in to change notification settings - Fork 0
/
cash.c
180 lines (159 loc) · 3.72 KB
/
cash.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
// ANSI escape sequences to color output
const char *bgWhite = "\033[47m";
const char *blackBright = "\033[90m";
const char *black = "\033[30m";
const char *cyan = "\033[36m";
const char *green = "\033[32m";
const char *red = "\033[31m";
const char *magenta = "\033[35m";
const char *reset = "\033[0m";
// Declare function prototypes
int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);
// Main: Print total number of coins to give the customer
int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();
// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;
// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;
// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;
// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;
// Sum coins
int coins = quarters + dimes + nickels + pennies;
// Print total number of coins to give the customer
printf(
"Total # coins to give the customer: %s%i%s\n",
blackBright, coins, reset
);
// Print quarters
printf(
"%sQuarters:%s %s%i%s\n",
blackBright, reset, magenta, quarters, reset
);
// Print dimes
printf(
"%sDimes:%s %s%i%s\n",
blackBright, reset, cyan, dimes, reset
);
// Print nickels
printf(
"%sNickels:%s %s%i%s\n",
blackBright, reset, red, nickels, reset
);
// Print pennies
printf(
"%sPennies:%s %s%i%s\n",
blackBright, reset, green, pennies, reset
);
}
int get_cents(void)
{
// Get user input: must be a positive integer
int cents;
do
{
printf("%sChange owed: %s", blackBright, reset);
// If scanf return value of 1, it means that the input was invalid
if (scanf("%d", ¢s) != 1)
{
// If scanf() couldn't scan an integer, consume invalid input and continue prompting
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
}
while (cents < 0);
return (int) cents;
}
int calculate_quarters(int cents)
{
int quarter = 25, quarters = 0;
// if cents < 25 return 0
if (cents < 25)
{
return 0;
}
else
{
// Subtract 25 cents
do
{
cents -= quarter;
quarters++;
}
while (cents >= quarter);
}
return quarters;
}
int calculate_dimes(int cents)
{
int dime = 10, dimes = 0;
// if cents < 10 return 0
if (cents < 10)
{
return 0;
}
else
{
// Subtract 10 cents
do
{
cents -= dime;
dimes++;
}
while (cents >= dime);
}
return dimes;
}
int calculate_nickels(int cents)
{
int nickel = 5, nickels = 0;
// if cents < 5 return 0
if (cents < 5)
{
return 0;
}
else
{
// Subtract 5 cents
do
{
cents -= nickel;
nickels++;
}
while (cents >= nickel);
}
return nickels;
}
int calculate_pennies(int cents)
{
int pennie = 1, pennies = 0;
// if cents < 1 return 0
if (cents < 1)
{
return 0;
}
else
{
// Subtract 1 cents
do
{
cents -= pennie;
pennies++;
}
while (cents >= pennie);
}
return pennies;
}