Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in C language (not cpp, nor C#) Input: Add BadRomance 4 5 Query 5 Insert Greensleeves HA 1 20 Query 20 Print 21

Please answer in C language (not cpp, nor C#)image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedInput:

Add BadRomance 4 5 Query 5 Insert Greensleeves HA 1 20 Query 20 Print 21 Delete BadRomance 40 Query 42 Print 42 Query 43 Print 43

Ouput:

Greensleeves BadRomance Greensleeves HA BadRomance BadRomance Greensleeves HA BadRomance Greensleeves Greensleeves HA

Codes given:

#include #include #include #include

// You can change anything in this program // We only help you with the input format int main(int argc, char *argv[]) { FILE *fin, *fout; int i, j, d, t; char cmd[300], op[20], song1[101], song2[101]; int current_time; char *output; fin = fopen(argv[1], "r"); fout = fopen(argv[2], "w");

current_time = 0; memset(cmd, 0, sizeof(cmd)); while (fgets(cmd, 300, fin) > 0) { memset(op, 0, sizeof(op)); memset(song1, 0, sizeof(song1)); memset(song2, 0, sizeof(song2)); sscanf(cmd, "%s", op); if (strcmp(op, "Add\0") == 0) { printf("DEBUG: Add "); sscanf(cmd, "%s%s%d%d", op, song1, &d, &t); } else if (strcmp(op, "Insert\0") == 0) { printf("DEBUG: Insert "); sscanf(cmd, "%s%s%s%d%d", op, song1, song2, &d, &t); } else if (strcmp(op, "Delete\0") == 0) { printf("DEBUG: Delete "); sscanf(cmd, "%s%s%d", op, song1, &t); } else if (strcmp(op, "Query\0") == 0) { printf("DEBUG: Query "); sscanf(cmd, "%s%d", op, &t); } else if (strcmp(op, "Print\0") == 0) { printf("DEBUG: Print "); sscanf(cmd, "%s%d", op, &t); } memset(cmd, 0, sizeof(cmd)); } fclose(fin); fclose(fout); return 0; }

You are asked to simulate a music player. The music player is initialized with time =0min and one default song = "Greensleeves" with duration 5min. This song can never be removed from the music player. The player will play all songs in the list order and repeat again and again. There are 5 operations available for the music player. In this problem, you can also assume the operation is sorted by the time. Your program should read the input from the file, and output the answer to another file. The first argument is the input file name, while the second argument is the output file name. Name your program as "lab1-q4.c". Input file: Each line contains one operation in the table above. You can assume that the name of the song contains capital or small letters only. The length of a song will not be longer than 100. Output file: Output the results based on the query and print operations. You can assume the maximum number of songs in the playlist does not exceed 100. Sample Input: Add BadRomance 45 Query 5 Insert Greensleeves HA 120 Query 20 Print 21 Delete BadRomance 40 Query 42 Print 42 Query 43 Print 43 Sample Output: Greensleeves BadRomance Greensleeves HA BadRomance BadRomance Greensleeves HA BadRomance Greensleeves Greensleeves HA Note: If you have a trailing space at the end of line, you will receive "wrong answer". Take this sample output as an example, if it is written in a string, it will be, "Greensleeves BadRomance GreensleevesHA BadRomance BadRomance GreensleevesHA BadRomance Greensleeves \ ngreensleeves HA " You are asked to simulate a music player. The music player is initialized with time =0min and one default song = "Greensleeves" with duration 5min. This song can never be removed from the music player. The player will play all songs in the list order and repeat again and again. There are 5 operations available for the music player. In this problem, you can also assume the operation is sorted by the time. Your program should read the input from the file, and output the answer to another file. The first argument is the input file name, while the second argument is the output file name. Name your program as "lab1-q4.c". Input file: Each line contains one operation in the table above. You can assume that the name of the song contains capital or small letters only. The length of a song will not be longer than 100. Output file: Output the results based on the query and print operations. You can assume the maximum number of songs in the playlist does not exceed 100. Sample Input: Add BadRomance 45 Query 5 Insert Greensleeves HA 120 Query 20 Print 21 Delete BadRomance 40 Query 42 Print 42 Query 43 Print 43 Sample Output: Greensleeves BadRomance Greensleeves HA BadRomance BadRomance Greensleeves HA BadRomance Greensleeves Greensleeves HA Note: If you have a trailing space at the end of line, you will receive "wrong answer". Take this sample output as an example, if it is written in a string, it will be, "Greensleeves BadRomance GreensleevesHA BadRomance BadRomance GreensleevesHA BadRomance Greensleeves \ ngreensleeves HA

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions