-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
365 lines (237 loc) · 10.4 KB
/
main.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* File: Lab9.c
* Author: Arjun Mittal
*
* Created on April 3, 2017, 6:35 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LENGTH 1024
#define MAX_LIBRARY_SIZE 100
//Defining Struct
typedef struct song {
char songName[MAX_LENGTH] ;
char artist[MAX_LENGTH] ;
} Song ;
//PrintStatments
// Function to call when printing an empty music library.
void printMusicLibraryEmpty(void) {
printf("\nThe music library is empty.\n");
}
void songNameDuplicate(char songName[]){
printf("\nA song with the name '%s' is already in the music library.\n"
"No new song entered.\n", songName);
}
//Getting User Input
void getStringFromUserInput(char s[], int maxStrLength) {
int i = 0;
char c;
while (i < maxStrLength && (c = getchar()) != '\n')
s[i++] = c;
s[i] = '\0';
}
//Initializing the beginning of songName and artist to 0, indicating
//this portion of the array is empty
void initializeArray(Song library[], int size){
for(int i = 0; i < size; i++){
library[i].songName[0] = 0;
library[i].artist[0] = 0;
}
}
//Prints the library
void printList (Song library[], int size) {
bool donePrinting = false;
for(int i = 0; (i < size) && (!donePrinting); i++) {
if ((i == 0) && (library[i].songName[0] == 0) && (library[i].artist[0] == 0)) {
printMusicLibraryEmpty ();
donePrinting = true;
}
else if ((library[i].songName[0] == 0) && (library[i].artist[0] == 0)){
donePrinting = true;
}
else {
if(i == 0){
printf("\nMy Personal Music Library: \n");
}
printf("\n");
printf("%s \n", library[i].songName);
printf("%s \n", library[i].artist);
}
}
}
//Inserting user entered element into the end of the list
void insertElement(Song library[], int size, char input [], int maxStrLength){
bool notInserted = true;
char input1[MAX_LENGTH];
printf("Song name: ");
getStringFromUserInput (input, maxStrLength);
for (int i = 0; (i < size) && (notInserted) ; i++){
//checks for duplicate entry
int j = 0;
while((library[j].songName[0] != 0) && (j < size)) {
if (strcmp(input, library[j].songName) == 0) {
strcpy(input1, input);
printf("Artist: ");
getStringFromUserInput(input, maxStrLength);
songNameDuplicate(input1);
notInserted = false;
}
j++;
}
//checks intitial character of array position ensuring section is empty before inserting
if((library[i].songName[0] == 0) && (library[i].artist[0] == 0) && (notInserted)){
strcpy(library[i].songName, input);
printf("Artist: ");
getStringFromUserInput(input, maxStrLength);
strcpy(library[i].artist, input);
notInserted = false;
}
}
}
//Counts the number of elements in the array
int numberOfItems(Song library[], int size){
int items = 0;
int counter = 0;
bool doneCounting = false;
while((counter < size) && (!doneCounting)) {
if((library[counter].songName[0] == 0) && (library[counter].artist[0] == 0)){
doneCounting = true;
}
else{
items++;
counter++;
}
}
return items;
}
//cocktail sorts the array
void cocktailSort( Song library[], int size ){
//Used for swaps
Song temp [1];
bool swaps = true;
bool swapForward = true;
bool swapBackward = true;
int items = 0;
int alphaOrder = 0;
int front = 0;
items = numberOfItems(library, size);
//ensures list is not empty
if (items != 0){
for(int top = items - 1; (top > front) && (swaps); top--){
swaps = false;
swapForward = false;
swapBackward = false;
//First iteration moving artists names to the back in ascending order
for(int i = front; i < top; i++){
alphaOrder = strcmp(library[i].artist, library[i+1].artist);
//Second artist name is less than first and needs to be swapped
if(alphaOrder > 0) {
//copy into temporary position
strcpy(temp[0].songName, library[i].songName);
strcpy(temp[0].artist, library[i].artist);
//copy next element into current position
strcpy(library[i].songName, library[i+1].songName);
strcpy(library[i].artist, library[i+1].artist);
//copies next element from temp
strcpy(library[i+1].songName, temp[0].songName);
strcpy(library[i+1].artist, temp[0].artist);
swapForward = true;
}
//sorting based on song name if the artist is the same
if(alphaOrder == 0) {
alphaOrder = strcmp(library[i].songName, library[i+1].songName);
if(alphaOrder > 0) {
//copy into temporary position
strcpy(temp[0].songName, library[i].songName);
strcpy(temp[0].artist, library[i].artist);
//copy next element into current position
strcpy(library[i].songName, library[i+1].songName);
strcpy(library[i].artist, library[i+1].artist);
//copies next element from temp
strcpy(library[i+1].songName, temp[0].songName);
strcpy(library[i+1].artist, temp[0].artist);
swapForward = true;
}
}
}
//second iteration moving artists names forward in descending order
for(int j = top; j > front; j--) {
alphaOrder = strcmp(library[j].artist, library[j-1].artist);
if(alphaOrder < 0) {
//copy into temporary position
strcpy(temp[0].songName, library[j-1].songName);
strcpy(temp[0].artist, library[j-1].artist);
//copy next element into current position
strcpy(library[j-1].songName, library[j].songName);
strcpy(library[j-1].artist, library[j].artist);
//copies next element from temp
strcpy(library[j].songName, temp[0].songName);
strcpy(library[j].artist, temp[0].artist);
swapBackward = true;
}
//sorting based on song name if the artist is the same
if(alphaOrder == 0) {
alphaOrder = strcmp(library[j].songName, library[j-1].songName);
if(alphaOrder < 0) {
//copy into temporary position
strcpy(temp[0].songName, library[j-1].songName);
strcpy(temp[0].artist, library[j-1].artist);
//copy next element into current position
strcpy(library[j-1].songName, library[j].songName);
strcpy(library[j-1].artist, library[j].artist);
//copies next element from temp
strcpy(library[j].songName, temp[0].songName);
strcpy(library[j].artist, temp[0].artist);
swapBackward = true;
}
}
}
front++;
if (swapForward || swapBackward){
swaps = true;
}
}
}
}
//Main function
int main(int argc, char** argv) {
Song Library [MAX_LIBRARY_SIZE];
initializeArray(Library, MAX_LIBRARY_SIZE);
// Announce the start of the program
printf("%s", "Personal Music Library.\n\n");
printf("%s", "Commands are I (insert), S (sort by artist), P (print), Q (quit).\n");
char response;
char input[MAX_LENGTH + 1];
do {
printf("\nCommand?: ");
getStringFromUserInput(input, MAX_LENGTH);
// Response is the first character entered by user.
// Convert to uppercase to simplify later comparisons.
response = toupper(input[0]);
//Inserts element into the end of the list
if (response == 'I') {
insertElement (Library, MAX_LIBRARY_SIZE, input, MAX_LENGTH);
}
else if (response == 'S') {
//Sorts array
cocktailSort(Library, MAX_LIBRARY_SIZE);
printList(Library, MAX_LIBRARY_SIZE);
}
else if (response == 'P') {
//Print the music library
printList(Library, MAX_LIBRARY_SIZE);
}
else if (response == 'Q') {
; // do nothing, we'll catch this below
}
else {
// do this if no command matched ...
printf("\nInvalid command.\n");
}
} while (response != 'Q');
return 0;
return (EXIT_SUCCESS);
}