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
  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • 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...

  • C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: ....

    C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: . Reads a text file provided along with this data and maintains it an object. Determines the number of characters and keeps in the object. Determines the number of words and retains the result in the object. Determines the number of paragraphs and keeps the result in the object. A possible class definition: class Textutil { string text = ** int words = @; int...

  • In the lectures about lists we have seen some Python code examples that involve the processing of...

    In the lectures about lists we have seen some Python code examples that involve the processing of lists containing weather statistics. The original source of the values shown in the slides was taken from part of the Environment Canada website that serves up historical data: http://climate.weather.gc.ca/historical_data/search_historic_data_e.html    Data can be provied by that website using the Comma Separated Value (CSV) format which is stored in text files normally using a CSV suffix. We will not work directly with such files in...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • 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...

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
ADVERTISEMENT