NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CDSorter2.h"

int main()
{
// create cdTower-Array (Vector not available in C)
// for storage of 'sizeOfTower' CD of varying types
int sizeOfTower = 25;
CD cdTower[sizeOfTower];
// fill the cdTower-Array with the ListOfCDs.txt
initialize(cdTower, sizeOfTower);

// get a pointer to the third CD in the tower (Music-GD-Insomniac-1995)
CD *thirdCDPtr = &cdTower[2];
// get a pointer to the last CD in the tower (Game - Half-Life)
CD *lastCDPtr = &cdTower[20];
// exchange third and last CD positions in the tower

printCD(cdTower[2]);
printCD(cdTower[20]);

changePosition(thirdCDPtr, lastCDPtr);

printCD(cdTower[2]);
printCD(cdTower[20]);


orderByType(cdTower, sizeOfTower);

printf("Now printing the entire CD-Tower:n");
int i;
for (i = 0; i < sizeOfTower; i++) {
printCD(cdTower[i]);
}

#if 0

printf("nNow printing Music only:n");
printCDtypes(cdTower, sizeOfTower, 1);

printf("nNow printing Films only:n");
printCDtypes(cdTower, sizeOfTower, 2);

printf("nNow printing Games only:n");
printCDtypes(cdTower, sizeOfTower, 3);

#endif

return 0;
}


/* Read the ListOfCDs.txt file (placed in the same
director as the program) and initialize the CDTower
*/
int initialize(CD cdTower[], int sizeOfTower) {
char buf[30];
int Games = 0;

// variable to determine the type of CD that is being read at the moment
// 0-empty, 1-Music, 2-Films, 3-Games
int cdType = 0;

// once cdType ist 1, 2 or 3, the 'cdLineCounter' is used to determine
// which field (Artist, Name, Year etc. has to be filled)
int cdLineCounter = 1;

// count the number of CDs stored in the drawer
int cdCounter = 0;

/* Try to open the ListOfCDs.txt file in reading mode */
FILE *TextIn=fopen("ListOfCDs.txt", "r");

if (TextIn != NULL) {
/* Read the file until the end */
while ( fgets(buf, sizeof(buf), TextIn) != NULL ) {

// check if the tower is too small for the CDs
// and break the reading process if neccessary
if (cdCounter >= sizeOfTower) {
printf("nWarning: The CDTower only has space for %d CDs.nYou were trying to add too many of them.n", sizeOfTower);
printf("nHowever, I have added %d CDs to the tower (%d of them are Games).nn", cdCounter, Games);
break; //exit the while-loop immediately
}

// depending on type of CD, the union must be filled differently
switch (cdType) {
case 1: // it's a Music-CD
switch (cdLineCounter) {
case 1:
strcpy(cdTower[cdCounter].musicCD.artist, buf);
break;
case 2:
strcpy(cdTower[cdCounter].musicCD.name, buf);
break;
case 3:
cdTower[cdCounter].musicCD.year = atoi(buf);
break;
}
// in the next iteration of the while-loop, the next line for the CD
// has to be filled, so we increase the cdLineCounter
cdLineCounter++;

// reset cdType if reading of current cdType is finished
// and count this CD via cdCounter
if (cdLineCounter > 3) {
cdType = 0;
cdCounter++;
}
break;

case 2: // it's a Film-CD
switch (cdLineCounter) {
case 1:
strcpy(cdTower[cdCounter].filmCD.name, buf);
break;
case 2:
if (strncmp(buf,"English",4) == 0) {
cdTower[cdCounter].filmCD.language = 0;
} else if (strncmp(buf,"German",4) == 0) {
cdTower[cdCounter].filmCD.language = 1;
} else if (strncmp(buf,"Spanish",4) == 0) {
cdTower[cdCounter].filmCD.language = 2;
}
break;
case 3:
cdTower[cdCounter].filmCD.year = atoi(buf);
break;
}

// in the next iteration of the while-loop, the next line for the CD
// has to be filled, so we increase the cdLineCounter
cdLineCounter++;

// reset cdType if reading of current cdType is finished
// and count this CD via cdCounter
if (cdLineCounter > 3) {
cdType = 0;
cdCounter++;
}
break;

case 3: // it's a Game-CD
strcpy(cdTower[cdCounter].gameCD.name, buf);

// in the next iteration of the while-loop, the next line for the CD
// has to be filled, so we increase the cdLineCounter
cdLineCounter++;

// reset cdType if reading of current cdType is finished
// and count this CD via cdCounter
if (cdLineCounter > 1) {
cdType = 0;
cdCounter++;
}
break;
} // here ends the cdType determination-switch


// determine if the line which has just been
// read is Music, Film or Game and then set
// cdType-information accordingly for the
// switch-case above
if (strncmp(buf,"Music",4) == 0) {
cdType = 1;
cdLineCounter = 1;
cdTower[cdCounter].type = cdType;
} else if (strncmp(buf,"Film",4) == 0) {
cdType = 2;
cdLineCounter = 1;
cdTower[cdCounter].type = cdType;
} else if (strncmp(buf,"Game",4) == 0) {
cdType = 3;
cdLineCounter = 1;
cdTower[cdCounter].type = cdType;
Games++;
}
} // while-loop for reading the text file ends here

printf("Thank you.nI have put %d CDs into your drawer (%d of them are games).nn", cdCounter, Games);

// if there are remaining empty spaces in the tower,
// initialize those with type 0/FREE
cdCounter++;
CD emptyCD;
emptyCD.type = FREE;
while (cdCounter <= sizeOfTower) {
cdTower[cdCounter-1] = emptyCD;
printf("Drawer %d is emptynn", cdCounter);
cdCounter++;
}

} else { // error while opening the specified file
printf("Error: Fail opening the filen");
return 0;
}

/* Close the file */
fclose(TextIn);
return 1;

}


// used for printing any CD which is in the tower
// warning, does not check if you specify an invalid (too large)
// location in the tower!
int printCD(CD cd) {

switch (cd.type) {
case MUSIC:
printf("nMusicn");
printf("%s",cd.musicCD.artist);
printf("%s",cd.musicCD.name);
printf("%dn",cd.musicCD.year);
break;
case FILM:
printf("nFilmn");
printf("%s",cd.filmCD.name);
switch (cd.filmCD.language) {
case 0:
printf("Englishn");
break;
case 1:
printf("Germann");
break;
case 2:
printf("Spanishn");
break;
}
printf("%dn",cd.filmCD.year);
break;
case GAME:
printf("nGamen");
printf("%s",cd.gameCD.name);
break;
default:
printf("nCD is free.n");
}

return 0;
}

// lists all CDs in the tower for a given type:
// 1-Music, 2-Film, 3-Game
int printCDtypes(CD cdTower[], int towerSize, int type) {

int i;
for (i = 0; i < towerSize; i++) {
if (cdTower[i].type == type) {
printCD(cdTower[i]);
}
}
return 0;
}

int changePosition(CD* cd1Ptr, CD* cd2Ptr) {

//let the pointer magic begin...

// create a temporary CD with the contents of cd1Ptr
CD tempCD = *cd1Ptr;

// copy the contents of cd2Ptr to the contents of cd1Ptr
// (overwriting the latter)
*cd1Ptr = *cd2Ptr;

// copy the previously saved tempCD to the contents of cd2Ptr
// (overwriting the latter)
*cd2Ptr = tempCD;

return 0;
}


// ordering implemented with bubbleSort,
// I know it's extremely inefficient but it's
// the easiest sorting mode I could think of
// right now ;)
// order this way: [ { MusicCd } { FilmCd } { GameCd } {FreePlaces} ]
int orderByType(CD cdTower[], int towerSize) {

int i;
int j;
bool hasChanged;

// for sorting purposes,
// change FREE=0 to int 99 (temporarily!)
for (i = 0; i < towerSize; i++) {
if (cdTower[i].type == 0) cdTower[i].type = 99;
}

for (i = 0; i < towerSize; ) {
hasChanged = false;
for (j = 0; j < towerSize; j++) {
if (cdTower[j].type > cdTower[i].type) {
changePosition(&cdTower[j], &cdTower[i]);
hasChanged = true;
break;
}
} // end of inner for-loop
if (hasChanged == false) i++;
} // end of outer for-loop

// sorting finished, now we can
// re-change FREE=99 to FREE=0
for (i = 0; i < towerSize; i++) {
if (cdTower[i].type == 99) cdTower[i].type = 0;
}

return 0;
}

// order this way: [ { MusicCd & FilmCd (ordered by year)} { GameCd } {FreePlaces} ]
int orderByYear(CD cdTower[], int towerSize) {

int i;
int j;
bool hasChanged;

for (i = 0; i < towerSize; ) {
hasChanged = false;
for (j = 0; j < towerSize; j++) {
if (cdTower[j].type > cdTower[i].type) {
changePosition(&cdTower[j], &cdTower[i]);
hasChanged = true;
break;
}
} // end of inner for-loop
if (hasChanged == false) i++;
} // end of outer for-loop

return 0;
}

// order this way: [ { MusicCd & FilmCd & GameCd (ordered by name)} {FreePlaces} ]
int orderByName(CD cdTower[], int towerSize) {

return 0;
}

#if 0

public void sort(int[] values) {
// Check for empty or null array
if (values ==null || values.length==0){
return;
}
this.numbers = values;
number = values.length;
quicksort(0, number - 1);
}


int quickSort(int low, int high, int numbers[]) {
int i = low;
int j = high;
// get pivot element from the middle of the list
int pivot = numbers[low + (high-low)/2];

// divide into two lists
while (i <= j) {

// if the current value from the left list is smaller than the pivot
// element then get the next element from the left list
while (numbers[i] < pivot) {
i++;
}

// if the current value from the right list is larger than the pivot
// element then get the next element from the right list
while (numbers[j] > pivot) {
j--;
}

// if we have found a values in the left list which is larger than
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the values
// once we are done we can increase i and decrease j
if (i <= j) {
// exchange i and j
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
}

} // while-loop ends here

// some magical recursion
if (low < j) {
quicksort(low, j, numbers);
}

if (i < high) {
quicksort(i, high, numbers);
}

};

// accepts different sorting criteria with the
// criterion variable: 0-Type, 1-Year, 2-Name
int quickSortCDs(CD cdTower[], int criterion) {

switch (criterion) {
case 0: // order by Type

break;
case 1: // order by Year
break;
case 2: // order by Name
break;
default:
printf("Sorting criterion undefined. No sorting done.");
}

return 0;

}

#endif
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.