I’ve got a really messed up, non working solution for ex. 5.1 of Accelerated C++ [closed]

  accelerated-c++, c++

I am currently trying to learn the basic C++ with Accelerated C++.
I tried to create my own solution for the ex. 5.1 in the book but the program got kind a complicated and I was not able to find a solution to errors that I got when I compiled it.
If someone can help me improve this code and criticize me I’d be really grateful.

Design and implement a program to produce a permuted index. A permuted index is one in
which each phrase is indexed by every word in the phrase. So, given the following input,

The quick brown fox
jumped over the fence

the output should be:

      The quick   brown fox
jumped over the   fence
The quick brown   fox
                  jumped over the fence
         jumped   over the fence
            The   quick brown fox
    jumped over   the fence
                  The quick brown fox

And now, my solution:

//Here's my code for the exercise 5.1 of ACCELERATED C++
//I can easily tell that I messed up big time in this code and got 
//everything complicated (it doesn't run and I can't resolve the errors)
//Thank you very much if you review this code and criticize me! 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <istream>
#include <list>


using namespace std;

// a struct that holds each strings original index 
// and the index of the phrase that it's in
struct word_indx{
    string word;
    vector<word_indx>::size_type string_index;
    vector<vector<word_indx>>::size_type vec_index;
};

vector<word_indx> permut(const vector<word_indx>& );

string rightblock(word_indx , const vector<word_indx>& );

string leftblock(word_indx , const vector<word_indx>&,  vector<word_indx>::size_type&);

vector<word_indx>::size_type find_maxlen(vector<vector<word_indx>>& );

bool word_indx_sorter(word_indx& , word_indx&);

int main() {
    word_indx s;
    vector<word_indx> vec_s;
    vector<vector<word_indx>> vv_s;

    //Couldn't find a way to detect different phrases each time 
    //user presses "enter" key
    cout<<"Please enter phrases with "." between each of them"<<endl;
    
    //store each phrase in a vector 
    //if the user types "." store that vector in another vector
    // P.S Each word_indx object has the it's own index info 
    while(getline(cin, s.word)){
        if(s.word == "."){

            vv_s.push_back(vec_s);
        }
        else{
        s.string_index =vec_s.size();
        vec_s.push_back(s);
        }
    }

    // maxlen for even spacing
    vector<word_indx>::size_type maxlen = find_maxlen(vv_s);
    vector<word_indx> fullordered;


    //Put all the phrases together in a vec;
    for(vector<vector<word_indx>>::size_type i = 0; i < vv_s.size(); ++i){
        //P.S. Yep, this part is useless
        //vector<word_indx> tmp = permut(vv_s[i]);
        vector<word_indx> tmp = vv_s[i];
        fullordered.insert(fullordered.end(), tmp.begin(), tmp.end());
    }
    // sort each word in alph order
    sort(fullordered.begin(), fullordered.end(), word_indx_sorter);

    string o_phrase;
    // print each line accordingly
    for(vector<word_indx>::size_type i = 0; i < fullordered.size(); ++i){
        vector<word_indx> tmp = vv_s[fullordered[i].vec_index];
        word_indx  tmpword = fullordered[i];
        o_phrase = leftblock(tmpword, tmp, maxlen) + " " + rightblock(tmpword, tmp);
        cout<<o_phrase<<endl;
    }

    return 0;
}

//Take the single phrase input
//Create another vector with strings in alph order
//P.S. JUST REALIZED THIS FUNCTION IS USELESS
/* vector<word_indx> permut(const vector<word_indx>& phrase){
    vector<word_indx> ordered;
    ordered = phrase;
    sort(ordered.begin(), ordered.end());
    return ordered;
}
*/

// Bool function to sort the word_indx objects
bool word_indx_sorter(word_indx& x, word_indx& y){
    return x.word < y.word;
}

//Take a word_indx and the phrase vector that originally contains it
//Create a new string = input  (word_indx.word) + strings with higher index value ( the ones that come after it )
string rightblock(word_indx s, const vector<word_indx>& phrase){
    string right_s;
    right_s = s.word;
    for(vector<word_indx>::size_type i = s.string_index++
        ; i < phrase.size(); ++i){
        right_s = right_s + " " + phrase[i].word;
    }
    return right_s;
}

// Now create a string that has all the words that come before the input object
// Keeping in mind the maxlen
string leftblock(word_indx s, const vector<word_indx>& phrase,  vector<word_indx>::size_type& maxlen){
    string left_s;
    left_s = ""; //Gotta try it with NULL as well
    for(vector<word_indx>::size_type i = 0; i != s.string_index; ++i){
        left_s = left_s + " " + phrase[i].word;
    }
    left_s = string(maxlen - left_s.size(), ' ') + left_s;
    return left_s;
}

// Find the maxlen
vector<word_indx>::size_type find_maxlen(vector<vector<word_indx>>& vv){
    vector<word_indx>::size_type maxlenvec = 0;
    vector<vector<word_indx>>::size_type maxlenvec_indx = 0;
    string::size_type maxlen = 0; 
    for(vector<vector<word_indx>>::size_type i = 0; i< vv.size(); ++i){
        if(vv[i].size() > maxlenvec){
            maxlenvec = vv[i].size();
            maxlenvec_indx = i; 
        }
    }
    //Gotta add +1 for the spaces
    vector<word_indx>& maxv = vv[maxlenvec_indx];
    for(vector<word_indx>::size_type i = 0; i < maxv.size(); ++i){
        //maxlen = maxlen + vv[maxlen_vec_indx][i].word.size()++;
        maxlen = maxlen + maxv[i].word.size();
        if(i != maxv.size() - 1){
            ++maxlen;
        }
    }
    return maxlen;
}

Source: Windows Questions C++

LEAVE A COMMENT