Question

C++ Chapters 3 and 4 discuss sorted and unsorted linear lists. In each of the text...

C++ Chapters 3 and 4 discuss sorted and unsorted linear lists. In each of the text examples, the program begins with an empty list, data is added and manipulated, and the program ends freeing the data. Suggest ideas for dealing with list data that would have more permanence and be stored and retrieved as needed. Use code fragments to illustrate your response.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:-

music.txt:-

Imagine a text file music.txt with the followingcontent:

AC/DC,Dirty Deeds Done Dirt Cheap,303,
Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
The Angels,Take A Long Line,180,

Output:-

Code to copy:-

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <algorithm>

#include <iterator>

#include <sstream>

#include <set>

#define kMaxLineSize 2048

#define kMusicLibraryFileName "music.txt"

using namespace std;

struct song {

     string artist;

     string title;

     int duration;

};

class Playlist

{

     public:

     string clear()

     {

          playList.erase(playList.begin(), playList.end());

          return "Cleared play list";

     };

     string load(string fileName)

     {

          ifstream musicFile(fileName.c_str());

          if(!musicFile)

          {

              cout << "Can't open input file " << fileName << endl;

              exit(1);

          }

          while(!musicFile.eof())

          {

              // Read a line from file and split on commas

              string bufString;

              getline(musicFile, bufString);

              vector<string> tokens = tokenizer(bufString, ",");

              // Add song to library

              if(tokens.size() == 3) {

                   song newSong;

                   newSong.artist = tokens.at(0);

                   newSong.title = tokens.at(1);

                   newSong.duration = atoi(tokens.at(2).c_str());

                   playList.push_back(newSong);

              }

          }

         

          return "Loaded music data";

     }

     string info()

     {

          ostringstream ost;

          vector<song>::iterator i;

          for(i = playList.begin(); i != playList.end(); i++)

          {

              ost << (*i).artist << "," << (*i).title << "," << (*i).duration << "," << endl;

          }

          return ost.str();

     }

     string add()

     {

          // Roll you own song adding routine...

          song newSong;

          newSong.artist = "The Adders";

          newSong.title = "Adding at the end";

          newSong.duration = 120;

          playList.push_back(newSong);

          return "Added song";

     };

     string save(string fileName)

     {

          ofstream musicFile(fileName.c_str());

          if(!musicFile)

          {

              cout << "Can't open output file " << fileName << endl;

              exit(1);

          }

          musicFile << info();

          musicFile.close();

          return "Saved music data";

     };

     protected:

     vector<string> tokenizer(string s, string delimiter)

     {

          size_t start = 0;

          size_t end = s.find(delimiter);

          vector<string> tokens;

          while ( end != string::npos )

          {

              string token = s.substr(start, end-start);

              tokens.push_back(token);

              start = end+delimiter.size();

              end = s.find(delimiter, start);

          }

          return tokens;

     }

     private:

     vector<song> playList;

};

bool isCommand(string command, string in)

{

     return in.find(command,0) != string::npos;

}

int getCommandArgument(string s)

{

     int arg = -1;

     if(string::npos != s.find(" "))

     {

          arg = atoi(s.substr(s.find(" ")).c_str());

     }

     return arg;

}

int main ()

{

     cout << "Simulation MP3 player" << endl;

     Playlist masterList;

     string command;

     do

     {

          cout << endl << "Command (info, add, load, save, clear):";

          getline(cin, command);

          if(isCommand("info", command))

          {

              cout << masterList.info();

          }

          else if(isCommand("add", command))

          {

              cout << masterList.add();

          }

          else if(isCommand("load", command))

          {

              cout << masterList.load(kMusicLibraryFileName);

          }

          else if(isCommand("save", command))

          {

              cout << masterList.save(kMusicLibraryFileName);

          }

          else if(isCommand("clear", command))

          {

              cout << masterList.clear();

          }

          else if(isCommand("quit", command))

          {

              // No cleanup needed

          }

          else

          {

              cout << "Illegal command" << endl;

          }

     } while(!isCommand("quit", command));

     return 0;

     system("pause");

}

Add a comment
Answer #2

#include<iostream.h>

#incude<conio.h>

void main()

{

int n,a[15],i,j,temp;

clracr();

cout<<"enter how many element u want ";

cin>>n;

cout<<"enter the element\n";

for(i=0;i<n;i++)

cin>>a[i];

for(i=0;i<n;i++)

{

for(j=i;j<n-1;j++)

{

if(a[i]>a[j+1])

{

temp=a[i];

a[i]=a[j+1];

a[j+1]=temp;

}}}

cout<<"after sorting the given list of eements\n";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

getch();

}

Add a comment
Know the answer?
Add Answer to:
C++ Chapters 3 and 4 discuss sorted and unsorted linear lists. In each of the text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary...

    All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT