-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram4.c
More file actions
315 lines (283 loc) · 13.3 KB
/
program4.c
File metadata and controls
315 lines (283 loc) · 13.3 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**********************************************************************/
/* */
/* Program Name: program4 - Database of Customer Accounts */
/* Receivable */
/* Author: Ethan Pinto */
/* Installation: Pensacola Christian College, Pensacola, Florida */
/* Course: CS227, Data Structures and Algorithms */
/* Date Written: October 28, 2016 */
/* */
/**********************************************************************/
/**********************************************************************/
/* */
/* I pledge all of the lines in this C program are my own original */
/* work and that none of the lines in this C program have been copied */
/* from anyone else, unless I was specifically authorized to do so by */
/* my CS227 instructor. */
/* */
/* */
/* Signed: _____________________________________ */
/* (signature) */
/* */
/* */
/**********************************************************************/
/**********************************************************************/
/* */
/* This program
/* */
/**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/**********************************************************************/
/* Symbolic Constants */
/**********************************************************************/
#define PROGRAM_NUMBER 4 /* Teacher assigned program number */
#define LAST_NAME "Pinto" /* The programmer's last name */
#define COURSE_NUMBER "CS227" /* PCC assigned course number */
#define MIN_CUSTOMER 2 /* Minimum number of customers */
#define MAX_CUSTOMER 100 /* Maximum number of customers */
#define MAX_NAME_LENGTH 20 /* Maximum length of customer's name */
#define QUIT 0 /* Program exit value */
#define DB_ALLOC_ERR 1 /* Can't allocate memory for database */
#define END_OF_STRING '\0' /* End of string character */
#define NEW_LINE '\n' /* New line characer */
/**********************************************************************/
/* Program Structures */
/**********************************************************************/
/* Database of customer accounts receivable records */
struct customer
{
char p_last_name[MAX_NAME_LENGTH + 1]; /* Customer's last name */
float amount; /* Amount owed */
int priority; /* Customer priority */
};
/**********************************************************************/
/* Function Prototypes */
/**********************************************************************/
void print_heading();
/* Prints the program heading */
void print_instructions();
/* Prints the program instructions */
int get_number_customers();
/* Gets the number of customer accounts */
void get_customer_data(struct customer *p_customer, int num_customers);
/* Gets the customer data for the accounts database */
void clean_data(struct customer *p_customer, int num_customers);
/* Cleans the customer name in the accounts database */
void sort_data(struct customer *p_customer, int num_customers);
/* Sorts the customer data in the accounts database */
void print_data(struct customer *p_customer, int num_customers);
/* Prints the customer accounts receivable */
/**********************************************************************/
/* Main Function */
/**********************************************************************/
int main()
{
struct customer *p_customer; /* Points to the accounts database */
int num_customers; /* Number of customer accounts */
/* Prints the program heading */
printf("\n\n\n\n\n\n\n");
print_heading();
/* Loop processing customer databases until user quits */
while (print_instructions(), (num_customers =
get_number_customers()) != QUIT)
{
/* Allocate memory for customer databases and abort if an error */
/* occurs */
if ((p_customer = (struct customer *)
malloc(sizeof(struct customer) * num_customers)) == NULL)
{
printf("\nError # %d occurred in main().", DB_ALLOC_ERR);
printf("\nCan't allocate memory for customer databases");
printf("\nThe program is aborting");
exit (DB_ALLOC_ERR);
}
/* Gets the customer accounts receivable, removes any */
/* non-characters and titlecases the customer names, sorts the */
/* accounts alphabetically, and prints the customer accounts */
/* receivable */
get_customer_data(p_customer, num_customers);
clean_data (p_customer, num_customers);
sort_data (p_customer, num_customers);
print_data (p_customer, num_customers);
}
/* Prints the end of data processing line */
printf("\n******* END OF DATA PROCESSING *******");
/* Releases memory allocated for customer databases */
free (p_customer);
/* Prints a goodbye message and terminates the program */
printf("\n\nThanks for using this program for processing a ");
printf( "database of customer accounts receivable.");
printf( "\nHave a nice day! ;-)");
printf("\n\n\n\n\n\n");
return 0;
}
/**********************************************************************/
/* Prints the Program Heading */
/**********************************************************************/
void print_heading()
{
printf("\n======================================================");
printf("\n Program Number: %d", PROGRAM_NUMBER);
printf("\n Programmer: %s", LAST_NAME);
printf("\n PCC Course Number: %s", COURSE_NUMBER);
printf("\n======================================================");
return;
}
/**********************************************************************/
/* Prints the Instructions */
/**********************************************************************/
void print_instructions()
{
printf("\n\nThis program allows you to input customers which owe");
printf( "\nyou money (your accounts receivable), and manage these");
printf( "\naccounts in a database. You will enter the following:");
printf( "\n Customer last name (%d-%d characters)", MIN_CUSTOMER,
MAX_CUSTOMER);
printf( "\n Amount the customer owes (to the exact cent)");
printf( "\n Customer priority (1=VIP, 2=Important, 3=Regular)");
printf( "\nFrom %d to %d customers may be processed.",
MIN_CUSTOMER, MAX_CUSTOMER);
return;
}
/**********************************************************************/
/* Gets the Number of Customer Accounts Receivable */
/**********************************************************************/
int get_number_customers()
{
int num_customers; /* Number of customer accounts receivable */
printf("\n\nGet the customers for the database");
printf( "\n--------------------------------------------------");
do
{
printf("\nHow many customers do you have (%d to ", MIN_CUSTOMER);
printf("%d, %d = quit): ", MAX_CUSTOMER, QUIT);
scanf ("%d", &num_customers);
}
while(num_customers != QUIT && (num_customers < MIN_CUSTOMER ||
num_customers > MAX_CUSTOMER));
return num_customers;
}
/**********************************************************************/
/* Gets the Name, Amount Owed, and Priority Level of the Customer */
/**********************************************************************/
void get_customer_data(struct customer *p_customer_start,
int num_customers)
{
struct customer *p_customer; /* Points to the accounts database */
char *p_last_name; /* Points to the customer's last */
/* name in the accounts database */
/* Loop points to every customer account receivable */
for (p_customer = p_customer_start;
(p_customer - p_customer_start) < num_customers; p_customer++)
{
/* Prints the customer number */
printf("\nCustomer number %d :", + 1);
/* Gets the customer name */
printf("\n Enter the customer's last name: ");
while (getchar() != NEW_LINE);
p_last_name = p_customer->p_last_name;
do
{
*p_last_name++ = getchar();
}
while(*(p_last_name - 1) != NEW_LINE);
/* Gets the amount that the customer owes */
*(p_last_name - 1) = END_OF_STRING;
printf(" Enter the amount owed: ");
scanf ("%f", &p_customer->amount);
/* Gets the priority of the customer */
do
{
printf(" Enter the customer's priority (1-3): ");
scanf ("%d", &p_customer->priority);
}
while(p_customer->priority < 1 || p_customer->priority > 3);
}
return;
}
/**********************************************************************/
/* Removes All Non-Characters and Title-cases the Customer Name */
/**********************************************************************/
void clean_data(struct customer *p_customer_start, int num_customers)
{
struct customer *p_customer; /* Points to the accounts database */
char *p_fast = p_customer_start->p_last_name,
/* Points to every char in word */
*p_slow = p_customer_start->p_last_name;
/* Receives all valid characters */
for(p_customer = p_customer_start;
(p_customer - p_customer_start) < num_customers; p_customer++)
{
p_fast = p_customer->p_last_name;
p_slow = p_customer->p_last_name;
while (*p_fast != END_OF_STRING)
{
if (isalpha(*p_fast))
*p_slow++ = tolower(*p_fast);
p_fast++;
}
*p_slow = END_OF_STRING;
*p_customer->p_last_name = toupper(*p_customer->p_last_name);
}
return;
}
/**********************************************************************/
/* Sorts the Accounts in Alphabetical Order */
/**********************************************************************/
void sort_data(struct customer *p_customer_start, int num_customers)
{
struct customer *p_customer, /* Points to the accounts database */
*p_outer, /* Points to the beginning of the */
/* customer's name */
*p_inner, /* Points
temp_customer; /* Temporary customer account */
for(p_outer = p_customer_start;
(p_outer - p_customer_start) < num_customers; p_outer++)
{
p_inner = p_customer;
for(p_outer = p_inner + 1;
(p_outer - p_customer_start) < num_customers; p_outer++)
{
if(strcmp(p_inner->p_last_name, p_outer->p_last_name) < 0)
p_outer = p_inner;
temp_customer = *p_customer;
*p_customer = *p_inner;
*p_inner = temp_customer;
p_inner++;
}
}
return;
}
/**********************************************************************/
/* Prints the Customer Accounts Receivable */
/**********************************************************************/
void print_data(struct customer *p_customer_start, int num_customers)
{
struct customer *p_customer; /* Points to the accounts database */
printf("\n Here is the accounts receivable customer database");
printf("\n=====================================================");
printf("\n Customer Name Amount Priority");
printf("\n--------------------- --------- -------------");
for(p_customer = p_customer_start;
(p_customer - p_customer_start) < num_customers; p_customer++)
{
printf("\n%20s $%9.2f %d ", p_customer->p_last_name,
p_customer->amount, p_customer->priority);
switch (p_customer->priority)
{
case 1:
printf("(VIP)");
break;
case 2:
printf("(Important)");
break;
case 3:
printf("(Regular)");
break;
}
}
return;
}