Question

Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that.

here is a sketch of the program from the screenshot

int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -= optind; argv += optind; string word; int count = 0; while (cin >> word) { count += 1; } switch (mode) { case total: cout << "Total: " << count << endl; break; case unique: cout << "Unique: " << "** missing **" << endl; break; } }

you'll have to copy and paste formatted

Your task is to write a program, words, that reports information about the number of words read from its standard input. For example, if the file qbf.txt contains the text the quick brown fox jumps over the lazy dog then running the program with is input redirected to come whisper from that file will report 9 words behind words qbft .txt Total Automatic Testing This task is available for automatic testing using the name words. Yo can run the demo using demo words and you can test your work using try words <<filename>>. Background Use the following skeleton for the program int main int argc chart argv) total unique mode total for int c getopt (argc 1;) tu argv switch (c) mode total break mode unique break. optind argo optind argv string word, int count. while cin word) count switch mode) case total Total count endl. break. cout case unique Unique missing endl. break. cout The getopt function (#include <unistd.h provides a standard way of handling option values in c mmand-line arguments to programs. It analyses the command-line parameters argC and argv, looking for arguments that begin with It then examines all such arguments for specified option letters, returning individual letters on successive calls and adjusting the variable optind to indicate which arguments it has processed. Consult getopt documentation for details In this case, the option processing code is used to optionally modify a variable that determines what output the program should produce. By default, mode is set to total (indicating that it should display the tot tal number of words read). The getopt code looks for the t and u options (which would be specified on the command line as t or -u) and overwrit tes the mode variable accordingly. When there are no more options ndicated by getopt returning -1), argc and

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

main.cpp

#include <string>
#include <vector>
#include <iostream>
#include <typeinfo>
#include <unistd.h>
using namespace std;

int main(int argc, char** argv)
{
    enum {total, unique} mode = total;
    for (int c; (c = getopt(argc, argv, "tu")) != -1;)
    {
        switch(c)
        {
            case 't': mode = total; break;
            case 'u': mode = unique; break;
        }
    }

    argc -= optind;
    argv += optind;
    string word;
    vector <string> words;

    int count = 0;
    while (cin >> word)
    {
        count += 1;
        bool repeated = false;
        for (auto it = words.begin(); it < words.end(); ++it)
        {
            if ((*it) == word)
            {
                 repeated = true;
                 break;
            }
        }
        if (!repeated)
        {
            words.push_back(word);
        }
    }
    switch (mode)
    {
        case total: cout << "Total: " << count << endl; break;
        case unique: cout << "Unique: " << words.size() << endl; break;
    }

    return 0;
}


main.cpp

#include <string>
#include <iostream>
#include <unistd.h>
using namespace std;

template <class T>
class Vector
{
    public:
        typedef T* iterator;
        Vector()
        {
            used = 0;
        }
        iterator begin()
        {
            return items;
        }
        iterator end()
        {
            return items + used;
        }
        int size()
        {
            return used;
        }
        iterator insert(iterator position, const T& item)
        {
            for (iterator it = end(); it > position; --it)
            {
                *it = *(it - 1);
            }
            *position = item;
            used ++;
            return position;
        }
    private:
        T items[1000];
        int used;
};

int main(int argc, char** argv)
{
    enum {total, unique} mode = total;
    for (int c; (c = getopt(argc, argv, "tu")) != -1;)
    {
        switch(c)
        {
            case 't': mode = total; break;
            case 'u': mode = unique; break;
        }
    }

    argc -= optind;
    argv += optind;
    string word;
    Vector <string> words;

    int count = 0;
    while (cin >> word)
    {
        count += 1;
        bool repeated = false;
        for (auto it=words.begin(); it<words.end(); ++it)
        {

            if ((*it) == word)
            {
                repeated = true;
            }
        }
        if (!repeated)
        {
            words.insert(words.end(), word);
        }
    }
    switch (mode)
    {
        case total: cout << "Total: " << count << endl; break;
        case unique: cout << "Unique: " << words.size() << endl; break;
    }

    return 0;
}

main.cpp

#include <string>
#include <algorithm>
#include <iostream>
#include <unistd.h>
using namespace std;

class WordInfo
{
    public:
        string text;
        int count;

        WordInfo()
        {
            text = "";
            count = 0;
        }

        WordInfo(string mText, int mCount)
        {
            text = mText;
            count= mCount;
        }
};

bool myCompare(WordInfo i1, WordInfo i2)
{
    return i1.text < i2.text;
}

template <class T>
class Vector
{
    public:
        typedef T* iterator;
        Vector()
        {
            used = 0;
        }
        iterator begin()
        {
            return items;
        }
        iterator end()
        {
            return items + used;
        }
        int size()
        {
            return used;
        }
        iterator insert(iterator position, const T& item)
        {
            for (iterator it = end(); it > position; --it)
            {
                *it = *(it - 1);
            }
            *position = item;
            used ++;
            return position;
        }
    private:
        T items[1000];
        int used;
};

int main(int argc, char** argv)
{
    enum {total, unique, indiv} mode = total;
    for (int c; (c = getopt(argc, argv, "tui")) != -1;)
    {
        switch(c)
        {
            case 't': mode = total; break;
            case 'u': mode = unique; break;
            case 'i': mode = indiv; break;
        }
    }

    argc -= optind;
    argv += optind;
    string word;
    Vector <WordInfo> words;

    int count = 0;
    while (cin >> word)
    {
        count += 1;

        bool repeated = false;
        for (auto it=words.begin(); it<words.end(); ++it)
        {

            if ((*it).text == word)
            {
                repeated = true;
                (*it).count ++;
            }
        }
        if (!repeated)
        {
            WordInfo entry(word, 1);
            words.insert(words.end(), entry);
        }
    }
    switch (mode)
    {
        case total: cout << "Total: " << count << endl; break;
        case unique: cout << "Unique: " << words.size() << endl; break;
        case indiv:
                     {
                         sort(words.begin(), words.end(), myCompare);
                         for (auto it = words.begin(); it < words.end(); ++it)
                         {
                              cout << (*it).text << ": " << (*it).count << endl;
                         }
                         break;
                     }
    }

    return 0;
}


main.cpp

#include <string>
#include <iostream>
#include <algorithm>
#include <unistd.h>
using namespace std;

class WordInfo
{
    public:
        string text;
        int count;

        WordInfo()
        {
            text = "";
            count = 0;
        }

        WordInfo(string mText, int mCount)
        {
            text = mText;
            count= mCount;
        }
};

bool myCompare(WordInfo i1, WordInfo i2)
{
    return i1.text < i2.text;
}

template <class T>
class Vector
{
    public:
        typedef T* iterator;
        Vector()
        {
            used = 0;
            items = new T[1000];
            max_size = 1000;
        }
        iterator begin()
        {
            return items;
        }
        iterator end()
        {
            return items + used;
        }
        int size()
        {
            return used;
        }
        iterator insert(iterator position, const T& item)
        {
            int offset = position - end();
            if(used + 1 == max_size) // double the size
            {
                T * newItems = new T[2 * max_size];
                for (int i=0; i<used; ++i)
                {
                     newItems[i] = items[i];
                }
                delete []items;
                items = NULL;
                items = newItems;
                max_size *= 2;
            }
            position = end() + offset;

            for (iterator it = end(); it > position; --it)
            {
                *it = *(it - 1);
            }
            *position = item;
            used ++;
            return position;
        }
    private:
        T * items;
        int max_size;
        int used;
};

int main(int argc, char** argv)
{
    enum {total, unique, indiv} mode = total;
    for (int c; (c = getopt(argc, argv, "tui")) != -1;)
    {
        switch(c)
        {
            case 't': mode = total; break;
            case 'u': mode = unique; break;
            case 'i': mode = indiv; break;
        }
    }

    argc -= optind;
    argv += optind;
    string word;
    Vector <WordInfo> words;

    int count = 0;
    while (cin >> word)
    {
        count += 1;
        bool repeated = false;
        for (auto it=words.begin(); it<words.end(); ++it)
        {

            if ((*it).text == word)
            {
                repeated = true;
                (*it).count ++;
            }
        }
        if (!repeated)
        {
            WordInfo entry(word, 1);
            words.insert(words.end(), entry);
        }
    }
    switch (mode)
    {
        case total: cout << "Total: " << count << endl; break;
        case unique: cout << "Unique: " << words.size() << endl; break;
        case indiv:
                     {
                         sort(words.begin(), words.end(), myCompare);
                         for (auto it = words.begin(); it < words.end(); ++it)
                         {
                              cout << (*it).text << ": " << (*it).count << endl;
                         }
                         break;
                     }
    }

    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Hi, need this question ansered in c++, has multiple levels will post again if you can...
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
  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • The provided code is my solution, stripped of the details needed to make it work. It...

    The provided code is my solution, stripped of the details needed to make it work. It is not a “good” program. It lives in a single file, does not use classes, and it has those evil global variables. That is by design. I want to to craft code. Use my code as a guide to help you put together the needed parts. #include #include #include // defaults const int MAX_STEPS = 100; // how long do we run the simulation...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

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