Question
Write a c program, based on Mini-assignment #2 codes below, that keeps track of ratings for MP3 files and displays information about them. Changed Functional
Write a c program, based on Mini-assignment #2 codes below, that keeps track of ratings for MP3 files and displays information about them.
Changed Functional Requirements
Along with the artist and title for each song, also get an integer from the user that represents the rating for the song. The order of input for each song should be artist, title, and rating, all on separate lines.
You can assume that the artist and title are least 1 character long and no longer than 30 (this is different from the Mini-Assignment).
Valid ratings are 1 to 5 inclusive.
No other input is allowed.
Put the input into a sorted doubly-linked list (instead of the unsorted singly-linked list from Mini #2), where the sorting is done by rating. If two songs have the same rating, you can arbitrarily choose which song to put first.
The sorting must be done such that each element is inserted into the proper location in the list in turn. Do not create an unsorted list and then sort the list afterwards (doing thist would result in a failing mark for the assignment).
At the same time, put the song into another sorted linked list, where the sorting is done by artist. If two songs have the same artist, you can arbitrarily choose which song to put first.
The user will never enter a song with an artist and title that is already in the list (e.g. if "Beautiful Day" by "U2" is in the list already, the user will not ask you to enter it again).
Stop getting user input once an invalid rating is entered.
This means that there is no longer a limit of 10 songs.
You should ignore the song that had the invalid rating (i.e. don't put it into the linked list).
After the input is complete, traverse the ratings-sorted linked list, displaying one song per line with artist (left-justified with a width of 35 (not 40) characters), title (left-justified with a width of 35 (not 40) characters), and rating (left-justified).
Next, traverse the artist-sorted linked list, displaying with the same output format as the previous traversal.
Next, obtain one song as additional user input in the same format as before. Search for that song in the artist-sorted linked list.If there is a match of artist, title, and rating, prompt for a new rating.
If the rating is changed, update the artist-sorted linked list with the new rating. Then delete the song from the rating-sorted linked list and re-insert it.
If the rating is unchanged, do nothing.
The rating will be a valid rating between 1 and 5 inclusive.
If there is not an exact match, simply ignore the change and continue with the next step (i.e. just redisplay both linked lists).
Redisplay both linked lists as before.
Once completely done, you must free all allocated memory.
Additional Functions
Create the findSong() function. It returns NULL if a song is not found or it returns a pointer to the node containing a song, if both the title and artist are matched (with a non-case-sensitive match). It takes three parameters:
songNode *head: head of list
char *artist: pointer to null-terminated string containing artist
char *title: pointer to null-terminated string containing title
If only the artist or the title are found but both are not found in the same node, the song is not found.
Create the deleteNode() function. It deletes a node, using three parameters:
songNode *node: node to delete
songNode **head: pointer to head of list
songNode **tail: pointer to tail of list
The key to this function is relinking pointers around the node before deleting.
If node is NULL, it returns immediately.
It returns nothing.
Other Requirements
The artist and title fields must be dynamically allocated to an appropriate size as in Mini-assignment 1.
Do not get user input except as described in these requirements.
Do not display output except as described in these requirements.
Do not clear the screen at any time in this program.
You must do error checking where necessary.
If you use getNum() for input of the rating, you do not have to error-check the input beyond what getNum() does.
If you detect an error, you should display an appropriate error message and take appropriate action.
You can assume that all statements above that have the phrase "will be" in them will be true when testing is done.
It must not use global variables.
It must not use goto.
It must be programmed in C or C++ (your choice). Be aware that malloc() does not support C++ strings.
The SET Coding Standards must be adhered to.
File Requirements
Call your project dsA1.
Call the file with your main() in it dsA1.c or dsA1.cpp.
Call the file with your linked list functions in it dsA1LL.c or dsA1LL.cpp.
If you have trouble deciding which file a function should go into, ask yourself "could I use this function in another program that uses linked list without having to change much (it doesn't have to be perfect) in it?"
Create dsA1.h for inclusion into both of the above files for sharing constants, data types, prototypes, constants.
Do not create other source files.
//////////////////////////////////////////////
Mini-assignment #2
mini2.c
////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include "linkedlist.h"
#pragma warning(disable:4996)
int main()
{
struct songInfo *songs;
struct songInfo *p;
char artist[40] = "";
char title[40] = "";
songs = NULL;
//loop and call getSongInfo for 10 times(10 pairs of songs)
int i;
for (i = 0; i < 10; i++)
{
//get input from user
printf("Enter the Name of the Artist for Song %d: ", i + 1);
fgets(artist, sizeof(artist), stdin);
clearCR(artist);
if (sscanf(artist, "%d", &i) != 1)
{
printf("Enter the Title of Song %d: ", i + 1);
}
fgets(title, sizeof(title), stdin);
clearCR(title);
if (sscanf(title, "%d", &i) != 1)
{
printf(" ");
}
songs = getSongInfo(songs, artist, title);
}
printSongInfo(songs);
//free the memory
p = songs;
for (i = 0; i < 10; i++)
{
if (p != NULL) {
free(p->artist);
free(p->title);
p = p->next;
}
}
return 0;
}
////////////////////////////////////////
linkedList.c
//////////////////////////////////////////
#include
#include
#include
#include
#include
#include "linkedlist.h"
#pragma warning(disable:4996)
struct songInfo *getSongInfo(struct songInfo *song, char artist[], char title[])
{
struct songInfo *p, *q;
p = song;
q = (struct songInfo *)malloc(sizeof(struct songInfo));
//allocates memory of size length of the string
q->artist = (char *)malloc((strlen(artist) + 1) * sizeof(char));
if (q->artist == NULL)
{
printf("Out of memory ");
return song;
}
//copies the artist name into our song element in array
strcpy(q->artist, artist);
q->title = (char *)malloc((strlen(title) + 1) * sizeof(char));
if (q->title == NULL)
{
printf("Out of memory ");
return song;
}
//copies the title into our song element in array
strcpy(q->title, title);
q->next = NULL;
if (song == NULL) {
song = q;
return song;
}
else {
p = song;
while (p->next != NULL)
p = p->next;
p->next = q;
return song;
}
}
//This function displays the songs and its title
void printSongInfo(struct songInfo *songs)
{
//prints all the 10 songs information
int i;
struct songInfo *p;
p = songs;
for (i = 0; i<10; i++) {
if (p != NULL) {
//40 is minimum number of chareters to be printed and '-' is to say they should be left justified
printf("%-40s%-40s ", p->artist, p->title);
p = p->next;
}
}
}
void clearCR(char *buf)
{
char *whereCR = strchr(buf, ' ');
if (whereCR != NULL)
*whereCR = '\0';
}
/////////////////////////////////////////
linkedList.h
//////////////////////////////////////////
//constants
#define true 1
#define false 0
// define linked list structure
struct songInfo
{
char *artist;
char *title;
struct songInfo *next;
};
//prototypes
struct songInfo * getSongInfo(struct songInfo *song, char artist[], char title[]);
void printSongInfo(struct songInfo *songs);
void clearCR(char *buf);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started