In: Computer Science
C++ Zybook LAB15.3.1: What order? (function templates
Define two generic functions called GetVector() and CheckOrder(). GetVector() takes a string of items delimited by spaces and an item of a generic type (i.e., char, int, double, or string) and returns a vector of the given item type. CheckOrder() checks if the vector of items is in ascending, neither, or descending order. The function should return 'a' if the items are in ascending order, 'u' if the items are unordered, and 'd' if the items are in descending order.
To test your functions, you are provided a main program which: Reads items from the input one line at a time, Uses a function called DataType() to determine the type of data (e.g., char, int, etc.) in the line, Constructs a vector of the appropriate type using your GetVector() function, and Then calls your CheckOrder() to output how the items are ordered. The items can be different types, including integers, strings, characters, or doubles.
Ex. If the input is: bat hat mat sat 63.2 196.5 123.5 the output is: Order: a Order: u
Code:
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
// TODO: Define a generic function called GetVector() that
// takes a string of items delimited by spaces and
// an item of a generic type (i.e., char, int, double,
// or string) and returns a vector of the given item
// type. The GetVector() function signature is below.
template <typename T>
vector<T> GetVector(string items, T item);
// TODO: Define a generic function called CheckOrder()
that
// takes a vector of either char, double, int or string
// type as an argument and returns 'a' if the contents
// of the vector is ascending, 'd' if descending and 'u'
// if it is neither. A vector of zero or one items is
// considered 'u'. The CheckOrder() signature is below.
template <typename T>
char CheckOrder(vector<T> v);
char DataType(string items);
int main(int argc, char* argv[]) {
// Read in items
string items, str_item = "";
char char_item = ' ';
int int_item = 0;
double double_item = 0.0;;
vector<string> sv;
vector<int> iv;
vector<double> dv;
vector<char> cv;
getline(cin, items);
while (!cin.eof()) {
// check for the type of data: DataType returns 'c' for char,
// 'd' for double, 'i' for int, and 's' for string
char type = DataType(items);
// populate the vector
switch (type) {
case 'c': cv = GetVector(items, char_item);
cout << "Order: " << CheckOrder(cv) <<
endl;
break;
case 'd': dv = GetVector(items, double_item);
cout << "Order: " << CheckOrder(dv) <<
endl;
break;
case 'i': iv = GetVector(items, int_item);
cout << "Order: " << CheckOrder(iv) <<
endl;
break;
default : sv = GetVector(items, str_item);
cout << "Order: " << CheckOrder(sv) <<
endl;
break;
}
getline(cin, items);
}
return 0;
}
char DataType(string items) {
bool chars = true;
bool strings = true;
bool ints = true;
bool doubles = true;
for (auto c : items) {
if (isalpha(c)) { ints = false; doubles = false; }
if (c == '.') { ints = false; }
}
if (ints) { return 'i'; }
if (doubles) { return 'd'; }
stringstream ss(items);
string s;
while (ss >> s) {
if (s.length() > 1) { chars = false; }
}
if (chars) { return 'c'; }
return 's';
}
Hello! :)
Here is the completed code to solve the assignment:
prog.cpp:
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
// TODO: Define a generic function called GetVector() that
// takes a string of items delimited by spaces and
// an item of a generic type (i.e., char, int, double,
// or string) and returns a vector of the given item
// type. The GetVector() function signature is below.
template <typename T>
vector<T> GetVector(string items, T item);
// TODO: Define a generic function called CheckOrder() that
// takes a vector of either char, double, int or string
// type as an argument and returns 'a' if the contents
// of the vector is ascending, 'd' if descending and 'u'
// if it is neither. A vector of zero or one items is
// considered 'u'. The CheckOrder() signature is below.
template <typename T>
char CheckOrder(vector<T> v);
char DataType(string items);
int main(int argc, char* argv[]) {
// Read in items
string items, str_item = "";
char char_item = ' ';
int int_item = 0;
double double_item = 0.0;;
vector<string> sv;
vector<int> iv;
vector<double> dv;
vector<char> cv;
getline(cin, items);
while (!cin.eof()) {
// check for the type of data: DataType returns 'c' for char,
// 'd' for double, 'i' for int, and 's' for string
char type = DataType(items);
// populate the vector
switch (type) {
case 'c': cv = GetVector(items, char_item);
cout << "Order: " << CheckOrder(cv) << endl;
break;
case 'd': dv = GetVector(items, double_item);
cout << "Order: " << CheckOrder(dv) << endl;
break;
case 'i': iv = GetVector(items, int_item);
cout << "Order: " << CheckOrder(iv) << endl;
break;
default : sv = GetVector(items, str_item);
cout << "Order: " << CheckOrder(sv) << endl;
break;
}
getline(cin, items);
}
return 0;
}
char DataType(string items) {
bool chars = true;
bool strings = true;
bool ints = true;
bool doubles = true;
for (auto c : items) {
if (isalpha(c)) { ints = false; doubles = false; }
if (c == '.') { ints = false; }
}
if (ints) { return 'i'; }
if (doubles) { return 'd'; }
stringstream ss(items);
string s;
while (ss >> s) {
if (s.length() > 1) { chars = false; }
}
if (chars) { return 'c'; }
return 's';
}
template <typename T>
vector<T> GetVector(string items, T item)
{
vector<T> itemsVector;
// parsing and inserting the items
stringstream ss(items);
T element;
while(ss >> element)
{
itemsVector.emplace_back(element);
}
return itemsVector;
}
template <typename T>
char CheckOrder(vector<T> v)
{
// initially v is neither ascending nor descending
bool a{false};
bool d{false};
for(int i{1}; i < v.size(); ++i)
{
if(v[i - 1] < v[i])
{
a = true; // at least one ascending consecutive pair found
}
else if(v[i - 1] > v[i])
{
d = true; // at least one descending consecutive pair found
}
}
if(a ^ d)
{
// only one of a and d is true
return a ? 'a' : 'd';
}
else
{
// both a and d are true or false
return 'u';
}
}
Here is the snapshot of a demo run with the input you suggested and few other ones:
Hope this helps! :)