All pastes #47543 Raw Edit

Unnamed

public c v1 · immutable
#47543 ·published 2006-03-30 01:32 UTC
rendered paste body
/* ================================================================ *//*AUTHOR: Daniel Frederich    FSU MAIL NAME: djf05    RECITATION SECTION NUMBER: 9    RECITATION INSTRUCTOR NAME: Prakash Mohan    CGS 3408 - Spring 2006    PROJECT NUMBER: 2    DUE DATE: Friday 2/03/2006    PLATFORM: Windows XP using Microsoft Visual C++ compiler*//* ================================================================ */#include <stdio.h>#include <stdlib.h>#include <time.h>#define NUM_SONGS 15/* ================================================================ */typedef enum SongCategory {COUNTRY, DANCEPARTY, ELEVATOR,                           ROCK}    SongCategory;/* ================================================================ */void PrintIntro ();int ReadSongInfo (int, int [ ], SongCategory [ ]);void PrintSongInfo (int, int [ ], SongCategory [ ]);char* getSongType (SongCategory);void randomize (int [ ]);void playPlaylist (int [ ], int [ ], SongCategory [ ]);/* ================================================================ */int main ( ){	int        Track,        PlayTrack,        SongIDs [NUM_SONGS],        choice,        random [NUM_SONGS];    SongCategory SongTypes [NUM_SONGS];    SongCategory PlayType;    srand((unsigned)time(NULL));    //print intro and menu    ReadSongInfo (NUM_SONGS, SongIDs, SongTypes);	/* read in the song info and store it in the 2 */ 	/* arrays, then print it all out */ 	while (choice != 4){ 	    PrintIntro ();        choice = 0;        while (choice < 1 || choice > 4){            printf("Please enter a choice from the menu above: ");            scanf("%d", &choice);        }        if (choice == 1){            PrintSongInfo (NUM_SONGS, SongIDs, SongTypes);        } //end if        if (choice == 2){            randomize(random);            playPlaylist(random, SongIDs, SongTypes);        }        if (choice == 3){            printf("Enter the track number (1-15) that you wish to play: ");            scanf("%d", &Track);            while (Track < 1 || Track > 15){                printf("There are only 15 tracks, please enter");                printf(" a track 1-15: ");                scanf("%d", &Track);            } //end while            PlayTrack = SongIDs [Track - 1];            PlayType = SongTypes [Track - 1];            printf("now playing...%d %s\n", PlayTrack, getSongType(PlayType));        } //end if    }//end while	printf ("\nExecution Terminated.\n\n");	return (0);}/* ================================================================ */void PrintIntro (int choice){printf("Welcome to the Music Player Program\n");printf("Please make a selection from the menu below\n\n\n");printf("1. Play Current Playlist in Order\n");printf("2. Play Randomized Playlist\n");printf("3. Play One Song\n");printf("4. Leave the Program\n\n");}/* ================================================================ */int ReadSongInfo (int numsongs,	 int SongIDs [ ], SongCategory SongTypes [ ]){    FILE* inFile;	int count;			/* loop control variable */	char typecode;		/* code for enum value */    inFile = fopen ( "songData.txt", "r" );	if ( !inFile)	{		printf ( "Error: Data file could not be opened\n" );		return (EXIT_FAILURE);	}	for ( count = 0;  count < NUM_SONGS; count++ )	{		fscanf (inFile, "%d %c", &SongIDs [count ], &typecode);		switch (typecode)		{			case 'C': SongTypes [count] = COUNTRY;					break;			case 'D': SongTypes [count] = DANCEPARTY;					break;			case 'E': SongTypes [count] = ELEVATOR;					break;            case 'R': SongTypes [count] = ROCK;					break;		}	/* switch */	}	/* for loop */}	/* function ReadSongInfo *//* ================================================================ */void PrintSongInfo (int numsongs,	int SongIDs [ ],	SongCategory SongTypes [ ]){	int lcv;	/* print all IDs and corresponding categories */	for (lcv = 0; lcv < NUM_SONGS; lcv++)	{		printf ("Now Playing %6d %s\n", SongIDs [lcv],		    getSongType(SongTypes [lcv]));		getSongType (SongTypes [lcv]);    }	/* for loop */}	/* function PrintSongInfo *//* ================================================================ */char* getSongType (SongCategory songTypes){	switch (songTypes)	{		case ROCK:			return "ROCK";			break;		case DANCEPARTY:			return "DANCEPARTY";			break;		case ELEVATOR:			return "ELEVATOR";			break;		case COUNTRY:			return "COUNTRY";			break;	}	/* switch */}	/* function PrintSongType *//* ================================================================ */void randomize(int random[]){    int i;    int swapIdx;    int tmp;    // Step 1    // Make the random array have have the values be the same as the indices.    for(i = 0 ; i < NUM_SONGS ; i++){        random[i] = i;    }    // Step 2    // Go through the array and for each index, pick a random index to swap it with.    // This assures that the result is just a permutation of the original and no duplicates exist.    for(i = 0 ; i < NUM_SONGS ; i++){        // get a random number between 0 and NUM_SONGS        swapIdx = rand() % NUM_SONGS;        // standard 3 step swap        tmp = random[i];        random[i] = random[swapIdx];        random[swapIdx] = tmp;    }    for(i = 0 ; i < NUM_SONGS ; i++){        printf("%d\n",random[i]);    }}/* ================================================================ */void playPlaylist (int playlist [ ],	int SongIDs [ ],	SongCategory SongTypes [ ]){	int lcv;	/* print all IDs and corresponding categories */	for (lcv = 0; lcv < NUM_SONGS; lcv++)	{		printf ("Now Playing %6d %s\n", SongIDs [playlist[lcv]],		    getSongType(SongIDs [playlist[lcv]]));    }	/* for loop */}	/* function PrintSongInfo */