Question: #include #include #include #include #include using namespace std; //---structure for date------ void save2file(); struct date { int day, month, year; }; // structure for song

#include
#include
#include
#include
#include
using namespace std;
//---structure for date------
void save2file();
struct date
{
int day, month, year;
};
// structure for song
struct car
{ int id;
char songname[100],singername[100], category[10];
struct date albumdate;
}song_directory;
//---fuction for adding new song------
void create_album()
{
// taking inputs in structur variable
cout<<" Enter song id: ";
cin>>song_directory.id;
cout<<"Enter the song name: ";
fflush(stdin);
fgets(song_directory.songname,100,stdin);
cout<<"Enter the singer name: ";
fflush(stdin);
fgets(song_directory.singername,100,stdin);
cout<<"Enter the category[Pop, Rock, Jazz, Blues] : ";
fflush(stdin);
fgets(song_directory.category,10,stdin);
cout<<"Enter albumdate: ";
cout<<"Date: ";
cin>>song_directory.albumdate.day;
cout<<"Month: ";
cin>>song_directory.albumdate.month;
cout<<"Year: ";
cin>>song_directory.albumdate.year;
}
//function to remove extra line
char *remove_line(char *str)
{
int i = 0, j = 0;
while (str[i])
{
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '\0';
return str;
}
//---fuction for saving new record-----
void write_album()
{
// openig file in appand mode
fstream fo;
fo.open("song.txt",ios::out|ios::app);
if(!fo)
{ cout<<" File creation failed";}
else
{
create_album();
fo.write((char*)&song_directory, sizeof(car)); //Writing content to file
fo.close();
cout<<" Data saved!!";
}
}
void show_album()
{
cout<<" "<
<
<<"\t\t"<
<<"\t\t"<
<<"\t"<
<
<
}
//---fuction for displaying list of song------
void display_record()
{
int n=0;
fstream fo; // opening the text file
cout<<" -----List of All Songs---- ";
cout<<" Id | Song name |\tSinger name | \tCategory |\tAlbum date";
fo.open("song.txt",ios::in);
if(!fo)
{ cout<<"File creation failed";}
else
{
while(fo.read((char*)&song_directory, sizeof(car))) // displaying records till end of file
{
show_album();
}
fo.close(); //closing the file object
}
}
//---fuction for deleting a record------
void delete_record()
{
int id;
int flg=0;
fstream fo,fo1; // structure variable c2
//taking entry for which record is to be deleted
cout<<" Enter song id :";
cin>> id;
fo.open("song.txt",ios::in|ios::out); //opening song file for reading
fo1.open("temp.txt",ios::out); //opening temporary text file to copy records
if(!fo1)
{ cout<<" File temp creation failed";}
if(!fo)
{ cout<<" File record opening failed";}
else
{
fo.seekg(0,ios::beg);
while(fo.read((char*)&song_directory, sizeof(car))) // reading contacts from txt file
{
// checking if record exists
if(song_directory.id!=id)
{
//copying rcords ecept matching in temp.txt file
fo1.write((char*)&song_directory, sizeof(car));
}
else
flg=1;
}
fo.close(); // closing both file objects
fo1.close();
remove("song.txt"); // removing song.txt file
rename("temp.txt", "song.txt"); // changing the name of temp. txt file to contact.txt
}
display_record(); // disply file records
if(flg==1)
{
cout<<" Record deleted successfuly";
}
else
{
cout<<" Record not fount";
}
}
//---fuction for modifying record------
void modify_record()
{
int id;
int flg=0;
fstream fo,fo1;
//taking entry for which record is to be modifyid
cout<<" Enter song id :";
cin>>id;
cout<<" Enter Modified details: ";
fo.open("song.txt",ios::in|ios::out); //opening txt file for reading
fo1.open("temp.txt",ios::out); //opening temporary text file to copy records
if(!fo1)
{ cout<<" File temp creation failed";}
if(!fo)
{ cout<<" File record opening failed";}
else
{
fo.seekg(0,ios::beg);
while(fo.read((char*)&song_directory, sizeof(car))) // reading contacts from txt file
{
// checking if record exists
if(song_directory.id==id)
{ //copying rcords ecept matching in temp.txt file
create_album();
fo1.write((char*)&song_directory, sizeof(car));
flg=1;
}
else
{
fo1.write((char*)&song_directory, sizeof(car));
}
}
fo.close(); // closing both file objects
fo1.close();
remove("song.txt"); // removing song.txt file
rename("temp.txt", "song.txt"); // changing the name of temp. txt file to contact.txt
}
display_record(); // disply file records
if(flg==1)
{ cout<<" Record updated successfuly..";}
else
{cout<<" Record not fount...";}
}
//---fuction for search a records------
void search_record(char ch)
{
char song_name[30];
char singer_name[30];
int flg=0;
fstream fo;
//taking entry for which record is to be searched for
fo.open("song.txt",ios::in); //opening contat file for reading
if(!fo)
{
cout<<" File record opening failed";
}
else
{
// reading contacts from txt file
if(ch=='A')
{
fflush(stdin);
cout<<" Enter Song name :";
fgets(song_name,30,stdin);
while(fo.read((char*)&song_directory, sizeof(car)))
{
if((strcmp(song_directory.songname, song_name)==0))
{
// disply file record
show_album();
flg=1;
}
}
}
else if(ch=='B')
{
fflush(stdin);
cout<<" Enter Singr name :";
fgets(singer_name,30,stdin);
while(fo.read((char*)&song_directory, sizeof(car)))
{
if((strcmp(song_directory.singername, singer_name)==0))
{ // disply file record
show_album();
flg=1;
}
}
}
fo.close(); //closing file object
}
if(flg==1)
{
cout<<" Record found successfuly";
}
else
{
cout<<" Record not fount...";
}
}
//count the number of lines
int count_line()
{
int count = 0;
fstream fo;
fo.open("song.txt",ios::in);
if(!fo)
{ cout<<"File creation failed";}
else
{
while(fo.read((char*)&song_directory, sizeof(car))) // displaying records till end of file
{
count++;
}
}
return count;
}
//print the row of a perticular id
void print_id(int id)
{
fstream fo;
fo.open("song.txt",ios::in);
if(!fo)
{ cout<<"File creation failed";}
else
{
while(fo.read((char*)&song_directory, sizeof(car)))
{
if(song_directory.id==id)
{
// disply file record
show_album();
}
}
}
}
//print the row of a perticular catagory
void print_cat(char cat[10])
{
fstream fo;
fo.open("song.txt",ios::in);
if(!fo)
{ cout<<"File creation failed";}
else
{
while(fo.read((char*)&song_directory, sizeof(car)))
{
if(strcmp(cat,song_directory.category)==0)
{
// disply file record
show_album();
}
}
}
}
//----fuction to sort the songs---------------------
void sort(char ch)
{
int i=0;
int size = count_line();
int arr[size];
char ar[size][10];
fstream fo;
cout<<" -----List of All Songs---- ";
cout<<" Id | Song name |\tSinger name | \tCategory |\tAlbum date";
fo.open("song.txt",ios::in);
if(!fo)
{ cout<<"File creation failed";}
else
{
while(fo.read((char*)&song_directory, sizeof(car)))
{
arr[i]=song_directory.id;
strcpy(ar[i],song_directory.category);
i++;
}
}
fo.close();
if(ch=='A' || ch=='a')
{
//sorting - ASCENDING ORDER
for(i=0;i
{
for(int j=i+1;j
{
if(arr[i]>arr[j])
{
int temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0; i
{
print_id(arr[i]);
}
}
else if(ch =='B' || ch=='b')
{
//sorting - Descending ORDER
for(i=0;i
{
for(int j=i+1;j
{
if(arr[i]
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0; i
{
print_id(arr[i]);
}
}
else if(ch =='C')
{
char temp[10];
//sorting - ASCENDING ORDER
for(i=0;i
{
for(int j=i+1;j
{
if(strcmp(ar[j-1],ar[j])>0)
{
strcpy(temp,ar[j-1]);
strcpy(ar[j-1],ar[j]);
strcpy(ar[j],temp);
}
}
}
for(i=0;i
{
print_cat(ar[i]);
}
}
}
//---main fuction ------
int main()
{
int choice;
char ch;
do{
cout<<" ******************************************************";
cout<<" \t\tDigital Music Archive System";
cout<<" ******************************************************";
cout<<" 1. Add a new Song to the disctionay";
cout<<" 2. Delete a Song from the disctionay";
cout<<" 3. List all Song in the disctionay";
cout<<" 4. Search a song:";
cout<<" \tA - Search according to song name";
cout<<" \tB - Search according to singer name";
cout<<" 5. Update song information";
cout<<" 6. Sort according to ID";
cout<<" \tA - In ascending order";
cout<<" \tB - In descending order";
cout<<" 7. Sort according to category in alphabetic order";
cout<<" 8. Quit";
cout<<" Enter your choice :";
cin>>choice;
if(choice==1)
{
write_album();
}
else if (choice==2)
{
delete_record();
}
else if (choice==3)
{
display_record();
}
else if(choice==4)
{
cout<<" Enter your option A/B :";
cin>>ch;
search_record(ch);
}
else if (choice==5)
{
modify_record() ;
}
else if (choice==6)
{
cout<<" Enter your option A/B :";
cin>>ch;
sort(ch);
}
else if (choice==7)
{
sort('C');
}
getch();
} while(choice!=8);
//getch();
return 0;
}
//---end of the program ---
_____________________________________
1- which sort algorithm you have used here????
2-which search algorithm you have used here?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!