Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming problem : Please Help Absolute C++; 5th edition; page 415; chapt

ID: 3765314 • Letter: C

Question

C++ programming problem : Please Help

Absolute C++; 5th edition; page 415; chapter9

6. There is a CD available for purchase that contains .jpeg and .gif images of music that is in the public domain. The CD includes a file consisting of lines containing the names, then composers of that title, one per line. The name of the piece is first, then zero or more spaces then a dash (-) character, then one or more spaces, then the composer’s name. The composer name may be only the last name, an initial and one name, two names (first and last), or three names (first, middle, and last). There are a few tunes with “no author listed” as author. In the subsequent processing, “no author listed” should not be rearranged. Here is a very abbreviated list of the titles and authors:

1. Adagio “MoonLight” Sonata - Ludwig Van Beethoven 2. An Alexis - F.H. Hummel and J.N. Hummel 3. A La Bien Aimee - Ben Schutt 4. At Sunset - E. MacDowell 5. Angelus - J. Massenet 6. Anitra’s Dance - Edward Grieg 7. Ase’s Death - Edward Grieg 8. Au Matin- Benj. - Godard … 37. The Dying Poet - L. Gottschalk 38. Dead March - G.F. Handel 39. Do They Think of Me At Home - Chas. W. Glover 40. The Dearest Spot - W.T. Wrighton 1. Evening - L. Van Beethoven 2. Embarrassment - Franz Abt 3. Erin is my Home - no author listed 4. Ellen Bayne - Stephen C. Foster … 9. Alla Mazurka - A. Nemerowsky … 1. The Dying Volunteer - A.E. Muse 2. Dolly Day - Stephen C. Foster 3. Dolcy Jones - Stephen C. Foster 4. Dickory, Dickory, Dock - no author listed Programming   5. The Dear Little Shamrock - no author listed 6. Dutch Warbler - no author listed …

The ultimate task is to produce an alphabetized list of composers followed by a list of pieces by them alphabetized on the title within composer. This exercise is easier if it is broken into pieces: Write code to do the following:

a. Remove the lead numbers, any periods, and any spaces so that the first word of the title is the first word of the line. b. Replace any multiple spaces with a single space. c. A few titles may have several - characters, for example,
20. Ba- Be- Bi- Bo- Bu - no author listed Replace all dash - characters on any line before the end of the line by a space except the last one. d. The last word in the title may have the - character with no space between it and the = character. Put the space in. e. W hen alphabetizing the title, you do not want to consider an initial “A”, “An”, or “The” in the title. Write code to move such initial words to just before the - character. A comma after the last word in the title is not required, but that would be a nice touch. This can be done after the composer’s names are moved to the front, but obviously the code will be different. f. M ove the composer’s names to the beginning of the line, followed by the character, followed by the composition title. g. M ove any first initial, or first and second names of the composer to after the composer’s last name. If the composer is “no author listed” this should not be rearranged, so test for this combination. h. A lphabetize by composer using any sort routine you know. You may ignore any duplicate composer’s last name, such as CPE Bach and JS Bach, but sorting by composer’s second name would be a nice touch. You may use the insertion sort, or selection sort, or bubble sort, or other sorting algorithm. i. If you have not already done so, move “A”, “An”, or “The” that may begin a title to the end of the title. Then alphabetize within each composer by composition title. j. K eep a copy of your design and your code. You will be asked to do this over using the STL vector container.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
int main() {
   ios::sync_with_stdio(false);
   vector<string> lines;
   string line;
   ifstream file ("music.txt");
   if (file.is_open()) {
       while (getline(file, line)) {
           lines.push_back(line);
       }
   }
   else {
       cout << "Could not open file" << endl;
   }
   for (int i = 0; i < lines.size(); i++) {
       // a. Remove the lead numbers, any periods, and any spaces so that the first word of the title is the first word of the line.
       lines[i] = lines[i].substr(3, string::npos);
       // c. Replace all dash - characters on any line before the end of the line by a space except the last one.
       bool flag = false;
       int dashPosition = 0;
       for (int j = lines[i].size()-1; j >= 0; j--) {
           if (lines[i].at(j) == '-' && !flag) {
               flag = true;
               dashPosition = j;
           }
           else if (lines[i].at(j) == '-' && flag) {
               lines[i][j] = ' ';
           }
       }
       // b. Replace any multiple spaces with a single space.
       string::iterator new_end = std::unique(lines[i].begin(), lines[i].end(), BothAreSpaces);
       lines[i].erase(new_end, lines[i].end());
       // e. Write code to move such initial words to just before the - character.
       int lastBlankSpacePosition = -1;
       string word;
       for (int j = 0; j < dashPosition; j++) {
           if (lines[i].at(j) == ' ') {
               word = lines[i].substr(lastBlankSpacePosition+1, j - lastBlankSpacePosition - 1);
               if (word == "A") {

               }
               lastBlankSpacePosition = j;

           }
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote