Hi, need this question ansered in c++, has multiple levels will post again if yo
ID: 3835856 • Letter: H
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 . 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 (#includeExplanation / Answer
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.