i have a code. could you complete it by writing both play and shuffle functions
ID: 3788369 • Letter: I
Question
i have a code. could you complete it by writing both play and shuffle functions ?
this is the code
//main.c
#include "header_dmm.h"
#define LOAD 1
#define STORE 2
#define DISPLAY 3
#define INSERT 4
#define DELETE 5
#define EDIT 6
#define SORT 7
#define RATE 8
#define EXIT 9
int main(void)
{
Record song1 = { 0 };
Node *pHead = NULL;
int option = 0;
do
{
option = menu();
switch (option)
{
case LOAD:
load(&pHead, song1);
system("cls");
break;
case STORE:
store(pHead);
system("pause");
break;
case DISPLAY:
display(pHead);
break;
case INSERT:
insert(&pHead, song1);
printList(pHead);
break;
case DELETE:
deleteNode(&pHead);
break;
case EDIT:
edit(&pHead);
system("pause");
system("cls");
printList(pHead);
store(pHead);
break;
case SORT:
sort(&pHead);
printList(pHead);
break;
case RATE:
rate(&pHead);
store(pHead);
}
} while (option != 9);
return 0;
}
============================================================================
//header_dmm.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int menu(void);
typedef struct duration {
int seconds;
int minutes;
}Duration;
typedef struct record
{
char artist[100];
char albumTitle[100];
char song[100];
char genre[100];
Duration length;
int numTimesPlayed;
int rating;
}Record;
typedef struct node
{
Record data;
struct node *pNext, *pPrev; // must be node not Node or else it would keep calling itself
} Node ;
Node * makeNode(Record newEntry);
void insertFront(Node **pList, Record newEntry);
Node * load( Node **pHead, Record music);
void store(Node *pHead);
void display(Node *pList);
Node * edit(Node **pHead);
Node * rate(Node **pHead);
Node * insert(Node **pHead, Record music);
Node * deleteNode(Node **pHead);
void sort(Node **pHead);
void printList(Node *pList);
=========================================================================
//dmm.c
#include "header_dmm.h"
int menu(void)
{
printf("Welcome to your Digital Music Manager! ");
printf("Menu options are as follows: ");
printf("1. Load 2. Store 3. Display 4. Insert 5. Delete 6. Edit 7. Sort 8. Rate 9. Exit! ");
int option = 0;
scanf("%d", &option);
return option;
}
Node * makeNode(Record newEntry)
{
Node *pMem = malloc(sizeof(Node));
if (pMem != NULL) //if we allocated space correctly
{
pMem->pNext = NULL;
pMem->pPrev = NULL;
strcpy(pMem->data.artist, newEntry.artist);
strcpy(pMem->data.albumTitle, newEntry.albumTitle);
strcpy(pMem->data.song, newEntry.song);
strcpy(pMem->data.genre, newEntry.genre);
pMem->data.length.minutes = newEntry.length.minutes;
pMem->data.length.seconds = newEntry.length.seconds;
pMem->data.numTimesPlayed = newEntry.numTimesPlayed;
pMem->data.rating = newEntry.rating;
}
return pMem;
}
void insertFront(Node **pList, Record newEntry)
{
Node *pMem = NULL;
pMem = makeNode(newEntry);
if (pMem == NULL)
{
*pList = pMem;
}
else {
pMem->pNext = *pList;
pMem->pPrev = *pList;
*pList = pMem;
}
}
Node * load(Node **pHead, Record music)
{
FILE *infile = fopen("music.csv", "r");
Node *pMem = NULL;
int i = 0;
char *token = NULL, line[200];
pMem = *pHead;
//pList = makeNode(song);
if (infile != NULL)
{
for (i = 0; i < 9; i++)
{
// loading files into the linked list
fgets(line, 200, infile);
//printf("Line : %s ", line);
token = strtok(line, ",");
//printf("Token: %s ", token);
strcpy(music.artist, token);
if (line[0] == '"')
{
token = strtok(NULL, ",");
//printf("Token 2 %s ", token);
strcat(music.artist, ",");
strcat(music.artist, token);
}
// printf("Artist: %s ", music.artist);
token = strtok(NULL, ",");
strcpy(music.albumTitle, token);
// printf("Album : %s ", music.albumTitle);
token = strtok(NULL, ",");
strcpy(music.song, token);
// printf("Song: %s ", music.song);
token = strtok(NULL, ",");
strcpy(music.genre, token);
// printf("Genre: %s ", music.genre);
token = strtok(NULL, ":");
music.length.minutes = atoi(token);
// printf("Minutes: %d ", music.length.minutes);
token = strtok(NULL, ",");
music.length.seconds = atoi(token);
// printf("Seconds %d ", music.length.seconds);
token = strtok(NULL, ",");
music.numTimesPlayed = atoi(token);
// printf("Num Played: %d ", music.numTimesPlayed);
token = strtok(NULL, ",");
music.rating = atoi(token);
// printf("Rating: %d ", music.rating);
token = strtok(NULL, ",");
pMem = makeNode(music);
//insertFront;
pMem->pNext = *pHead;
if (*pHead != NULL)
pMem->pNext->pPrev = pMem;
//pMem->pPrev = pMem;
*pHead = pMem;
}
printf("Loaded successfully. ");
fclose(infile);
}
return pMem;
}
void store(Node *pHead)
{
FILE *outfile = fopen("music.csv", "w");
Node *pTemp = pHead;
while (pTemp != NULL)
{
// NOTE MUST PUT A COMMENT BETWEEN ARTIST FIRST AND LAST
//printf("Artist : %s", pTemp->data.artist);
fprintf(outfile, "%s,", pTemp->data.artist);
fprintf(outfile, "%s,", pTemp->data.albumTitle);
fprintf(outfile, "%s,", pTemp->data.song);
fprintf(outfile, "%s,", pTemp->data.genre);
fprintf(outfile, "%d:%d,", pTemp->data.length.minutes, pTemp->data.length.seconds);
fprintf(outfile, "%d,", pTemp->data.numTimesPlayed);
fprintf(outfile, "%d ", pTemp->data.rating);
pTemp = pTemp->pNext;
}
fclose(outfile );
}
void display(Node *pList)
{
int answer = 0;
char artist[20] = "";
Node *pMem = pList;
pMem->pPrev = NULL;
printf("Would you like to see 1. All music records 2. Records for one artist ");
scanf("%d", &answer);
if (answer == 1)
{
while (pMem != NULL)
{
printf("%s ", pMem->data.artist);
printf("%s ", pMem->data.albumTitle);
printf("%s ", pMem->data.song);
printf("%s ", pMem->data.genre);
printf("%d:%d ", pMem->data.length.minutes, pMem->data.length.seconds);
printf("Times Played: %d ", pMem->data.numTimesPlayed);
printf("Rating: %d ", pMem->data.rating);
system("pause");
printf(" ");
pMem = pMem->pNext;
}
}
else if (answer == 2)
{
pMem = pList;
printf("If the artist has a first and last name please enter the last then the first with just a space. Ex: Brooks Garth Capitalize the first letters in the names. ");
system("pause");
printf("ALSO if the artist includes a first and last name, type it in quotes ");
printf("Now, what artist would you like to see records for? ");
scanf(" %[a-z A-Z '"']s", artist); //must put space before % or else scanf won't read anything.
printf("Artist: %s ", artist);
while (strcmp(artist, pMem->data.artist) != 0 && pMem != NULL)
{
pMem = pMem->pNext;
}
if (strcmp(artist, pMem->data.artist) == 0)
{
printf("%s ", pMem->data.artist);
printf("%s ", pMem->data.albumTitle);
printf("%s ", pMem->data.song);
printf("%s ", pMem->data.genre);
printf("%d:%d ", pMem->data.length.minutes, pMem->data.length.seconds);
printf("Times Played: %d ", pMem->data.numTimesPlayed);
printf("Rating: %d ", pMem->data.rating);
system("pause");
}
else
printf("Something went wrong :( ");
}
}
Node * edit(Node **pHead)
{
Node *pTemp = *pHead; // sets pTemp to start of list
char artist[20] = "", modifystrings[50] = "";
int duplicates = 0, song = 0, editAttribute = 0, i = 0;
int newint = 0;
printf("Type in the artist of the record you would like to edit. ");
printf("If your artist has a first and last name type it in like this: '"Brooks, Garth'". ");
printf("If the artist has just one name like Drake, then type it in as normal, but capitalize the first letter of course. ");
printf("Type in artist now. ");
scanf(" %[a-z A-Z '"' ,]s", artist);
// while loop finds all records matching artist and displays them
while (pTemp != NULL)
{
if (strcmp(artist, pTemp->data.artist) == 0) // if the artist entered has a match in the database
{
duplicates++;
printf("Option %d ", duplicates);
printf("%s ", pTemp->data.artist);
printf("%s ", pTemp->data.albumTitle);
printf("%s ", pTemp->data.song);
printf("%s ", pTemp->data.genre);
printf("%d:%d ", pTemp->data.length.minutes, pTemp->data.length.seconds);
printf("Times Played: %d ", pTemp->data.numTimesPlayed);
printf("Rating: %d ", pTemp->data.rating);
system("pause");
}
pTemp = pTemp->pNext;
}
pTemp = *pHead;
// points to the first record matching artist since there's only one
if (duplicates == 1)
{
while (strcmp(artist, pTemp->data.artist) != 0 && pTemp != NULL)
{
pTemp = pTemp->pNext;
}
}
// points to record that user selected for artist
else if (duplicates > 1) // if there is more than one song for artist
{
printf("Which song would you like to modify? (Enter number) ");
scanf("%d", &song);
while (i < song)
{
if (strcmp(artist, pTemp->data.artist) == 0)
{
i++;
}
pTemp = pTemp->pNext;
}
}
printf("Would you like to modify 1. Artist 2. Album 3. Song 4. Genre 5. Minutes 6. Seconds 7. Number of Times Played 8. Rating ");
scanf("%d", &editAttribute);
if (editAttribute >= 1 && editAttribute <= 4)
{
if (editAttribute == 1)
{
printf("Enter new artist: ");
scanf(" %[a-z A-Z '"']s", modifystrings);
strcpy(pTemp->data.artist, modifystrings);
}
else if (editAttribute == 2)
{
printf("Enter new album: ");
scanf(" %[a-z A-Z '"']s", modifystrings);
strcpy(pTemp->data.albumTitle, modifystrings);
}
else if (editAttribute == 3)
{
printf("Enter new song name: ");
scanf(" %[a-z A-Z '"']s", modifystrings);
strcpy(pTemp->data.song, modifystrings);
}
else if (editAttribute == 4)
{
printf("Enter new genre: ");
scanf(" %[a-z A-Z '"']s", modifystrings);
strcpy(pTemp->data.genre, modifystrings);
}
}
if (editAttribute >= 5 && editAttribute <= 8)
{
if (editAttribute == 5)
{
printf("Enter new minute time: ");
scanf("%d", &newint);
pTemp->data.length.minutes = newint;
}
if (editAttribute == 6)
{
printf("Enter new seconds time: ");
scanf("%d", &newint);
pTemp->data.length.seconds = newint;
}
if (editAttribute == 7)
{
printf("Enter new number of times played: ");
scanf("%d", &newint);
pTemp->data.numTimesPlayed = newint;
}
if (editAttribute == 8)
{
printf("Enter new rating: ");
scanf("%d", &newint);
pTemp->data.rating = newint;
}
}
return pTemp;
}
Node * rate(Node **pHead)
{
Node * pTemp = *pHead; //sets temp pointer to head of list
int rating = 0;
char song_title[20] = "";
printf("Here is your library! ");
printList(*pHead);
system("cls");
printf("What song would you like to rate? ");
scanf(" %[a-z A-z 0-9]s", song_title);
while (pTemp != NULL && (strcmp(song_title, pTemp->data.song) != 0))
{
pTemp = pTemp->pNext;
}
printf("Song: %s", pTemp->data.song);
printf("Enter new rating for %s (1-5) ", song_title);
scanf("%d", &rating);
if (rating < 1 || rating > 5)
{
printf("That rating was not between 1-5 dingdong. Enter new rating: ");
scanf("%d", &rating);
}
pTemp->data.rating = rating;
printList(*pHead);
return pTemp;
}
Node * insert(Node **pHead, Record music)
{
Node *pTemp = makeNode(music);
int min, sec, num, rate;
if (pTemp != NULL) // allocated mem for node
{
printf("Enter new artist (please enter artists with two names '"last, first'". Include quotes: ");
scanf(" %[a-z A-Z '" ,]s", pTemp->data.artist);
//strcat(pTemp->data.artist, ",");
printf("Enter new album: ");
scanf(" %[a-z A-Z 0-9]s", pTemp->data.albumTitle);
//strcat(pTemp->data.albumTitle, ",");
printf("Enter new song title: ");
scanf(" %[a-z A-Z 0-9]s", pTemp->data.song);
//strcat(pTemp->data.albumTitle, ",");
printf("Enter new genre: ");
scanf(" %[a-z A-Z 0-9]s", pTemp->data.genre);
//strcat(pTemp->data.genre, ",");
printf("Enter how many minutes the song lasts: ");
scanf("%d", &min);
pTemp->data.length.minutes = min;
printf("Enter how many seconds the song lasts: ");
scanf("%d", &sec);
pTemp->data.length.seconds = sec;
printf("Enter the number of times it's been played: ");
scanf("%d", &num);
pTemp->data.numTimesPlayed = num;
printf("Enter a rating for this song: ");
scanf("%d", &rate);
pTemp->data.rating = rate;
}
pTemp->pNext = *pHead;
*pHead = pTemp;
return pTemp;
}
Node * deleteNode(Node **pHead)
{
Node *pTemp = NULL;
char song[50];
pTemp = *pHead;
printf("What song would you like to delete? ");
scanf(" %[a-z A-Z 0-9]s", song);
while (pTemp != NULL && (strcmp(pTemp->data.song, song) != 0)) // while we havent found song, keep going through list
{
pTemp = pTemp->pNext;
}
// pTemp should be pointing to the correct node.
*pHead = pTemp->pNext;
pTemp->pPrev = NULL;
free(pTemp);
}
void sort(Node **pHead)
{
Node *pCurrent = *pHead;
Node *pTemp = NULL;
int length=0, c=0, u=0, option = 0;
while (pCurrent != NULL)
{
pCurrent = pCurrent->pNext; //finds length
length++;
}
pCurrent = *pHead; //pcurrent is null after the loop
u = length - 1;
printf("Would you like to sort by 1. Artist 2. Album 3. Rating 4. Number of times played ");
scanf("%d", &option);
while (u > 1)
{
pCurrent = *pHead;
while (pCurrent != NULL && pCurrent->pNext != NULL )
{
if (option == 2) // sort by artist
{
if ((strcmp(pCurrent->data.albumTitle, pCurrent->pNext->data.albumTitle) > 0))
{
if (*pHead == pCurrent)
{
*pHead = pCurrent->pNext;
}
pTemp = pCurrent->pNext;
if (pTemp->pNext != NULL)
{
pTemp->pNext->pPrev = pCurrent;
}
pCurrent->pNext = pTemp->pNext; // pcur pnext pnext
pTemp->pPrev = pCurrent->pPrev;
pTemp->pNext = pCurrent;
pCurrent->pPrev = pTemp;
}
}
pCurrent = pCurrent->pNext;
}
u--;
}
}
void printList(Node *pList)
{
Node *pMem = pList;
while (pMem != NULL)
{
printf("%s ", pMem->data.artist);
printf("%s ", pMem->data.albumTitle);
printf("%s ", pMem->data.song);
printf("%s ", pMem->data.genre);
printf("%d:%d ", pMem->data.length.minutes, pMem->data.length.seconds);
printf("Times Played: %d ", pMem->data.numTimesPlayed);
printf("Rating: %d ", pMem->data.rating);
system("pause");
printf(" ");
pMem = pMem->pNext;
}
}
Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would manipulate our music collection based on attributes such as artist, album title, song title, genre, song length, number times played, and rating. For this assignment you will write a basic digital music manager (DMM).
Your DMM program must have a text-based interface which allows the user to select from a main menu of options including: (1) load, (2) store, (3) display, (4) insert, (5) delete, (6) edit, (7) sort, (8) rate, (9) play, (10) shuffle, and (11) exit. For Part I of the assignment, you will only need to complete the main menu, (1) load, (2) store, (3) display, (6) edit, (8) rate, (9) play, and (11) exit features. The other features will be completed in the next part of the assignment.
What must the main menu contain?
The main menu must display the following commands:
(1) load
(2) store
(3) display
(4) insert
(5) delete
(6) edit
(7) sort
(8) rate
(9) play
(10) shuffle
(11) exit
After a command is selected and completed, your program must display the main menu again. This procedure will continue until the “exit” command is selected.
What must “load” do?
The “load” command must read all records from a file called musicPlayList.csv (you may find a sample file here) into a dynamic doubly linked list. The doubly linked list is considered the main playlist. As each record is read from the file, it must be inserted at the front of the list. Each record consists of the following attributes:
Each attribute, in a single record, will be separated by a comma in the .csv (comma separated values) file. This means that you will need to design an algorithm to extract the required attributes for each record. Each field in each record will have a value. You do not need to check for null or empty values.
You must define a struct called Record to represent the above attributes. Also, do not forget that the Song Length must be represented by another struct called Duration. Duration is defined as follows:
Finally, each struct Node in the doubly linked list must be defined as follows:
What must “store” do?
The “store” command writes the current records, in the dynamic doubly linked list, to the musicPlayList.csv file. The store will completely overwrite the previous contents in the file.
What must “display” do?
The “display” command prints records to the screen. This command must support two methods, one of which is selected by the user:
Print all records.
Print all records that match an artist.
What must “edit” do?
The “edit” command must allow the user to find a record in the list by artist. If there are multiple records with the same artist, then your program must prompt the user which one to edit. The user may modify all of the attributes in the record.
What must “rate” do?
The “rate” command must allow the user to assign a value of 1 – 5 to a song; 1 is the lowest rating and 5 is the highest rating. The rating will replace the previous rating.
What must “play” do?
The “play” command must allow the user to select a song, and must start “playing” each song in order from the current song. “Playing” the song for this assignment means displaying the contents of the record that represents the song for a short period of time, clearing the screen and showing the next record in the list, etc. This continues until all songs have been played.
What must “exit” do?
The “exit” command saves the most recent list to the musicPlayList.csv file. This command will completelyoverwrite the previous contents in the file.
IV. Logical Block Diagram
The logical block diagram for your doubly linked list should look like the following:
As you can see from the illustration a doubly linked list has a pointer to the next node and the previous node in the list. The first node’s previous node pointer is always NULL and the last node’s next pointer is always NULL. When you insert and delete nodes from a doubly linked list, you must always carefully link the previous and next pointers.
here is a sample csv file
Swift, Taylor 1989 Shake it Off Pop 3:35 12 3 Drake NOTHING WAS THE SAME Own it Rap 3:23 3 3 Drake YOU WELCOME The Motto Rap 4:13 7 4 Perri, Christina HEAD OF HEART Trust Pop 2:35 3 5 Bieber, Justin PURPOSE No Sense Pop 4:12 6 1 Eminem SHADYXV Vegas Rap 3:37 8 3 Adele 25 Remedy Pop 4:11 24 4 Swift, Taylor RED Stay Stay Stay Pop 4:42 5 1 Brooks, Garth FRESH HORSES The Old Stuff Country 2:57 11 2Explanation / Answer
Please check that some images are not uploaded properly...I request you upload them again...Mean while I am going to work on your code...Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.