Extend the program so that if run with the u option (specified by the command-li
ID: 3746245 • Letter: E
Question
Extend the program so that if run with the u option (specified by the command-line argument -u), it displays the number of unique words. To count unique words, use the STL vector class to keep track of which words you've already seen. When each word is read, check to see if it's already in the vector; if it's not, insert it. The number of unique words will then be the size of the vector.
here is the main but i stuck on exapanding it to pass the level.
#include <cstdlib>
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
Vector <string> vec_unique;
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;
}
}
argv -= optind;
argv += optind;
std:: string word;
int count = 0;
while (std::cin>>word)
{
count +=1;
}
switch (mode)
{case total:
std::cout<< "Total: " << count << std::endl;
break;
case unique:
std::cout << "unique: " << vec_unique.size() << std::endl;
break;
}
return 0;
}
Explanation / Answer
PROGRAM:
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.