Code in c++ please. Start with the checkout.cpp file provided. You must make a c
ID: 3692505 • Letter: C
Question
Code in c++ please.
Start with the checkout.cpp file provided. You must make a class that works with the existing main() function that simulates checking out at a grocery store. You may assume fewer than 200 items will be checked out. Ensure that you class makes the provided main() output match the examples exactly. The add() function should add a checkout item to the items being bought, either increasing the amount by one if the item is already present or adding it to the list if it is new. You may directly compare strings when adding, thus if the string is different at all it can be treated as a new item. The checkout() function should return a list of all items and their quantities, along with a final additional line saying how many items were bought. Note that the item bought is the sum of the quantities, not the amount of different types of items on the list. The order that you display the items is not important (but the order the examples use is probably the easiest to do).
Checkout.cpp
Explanation / Answer
/** class GroceryList C++ code to add items and display count of products **/
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
string int2str(int x);
class GroceryList
{
vector<string> v;
vector<string> result;
public:
void add(string item)
{
v.push_back(item);
}
public:
string int2str(int x)
{
string reverse = "";
int pos = abs(x);
while(pos > 0)
{
reverse += ('0'+pos%10);
pos/=10;
}
if(reverse.length() == 0)
{
return "0";
}
string result = "";
if(x < 0)
{
result += '-';
}
for(int i=0; i < static_cast<int>(reverse.length()); i++)
{
result += reverse[reverse.length()-1-i];
}
return result;
}
public:
vector<string> checkout()
{
int count = 1;
sort(v.begin(),v.end());
for (int i = 0; i < v.size(); ++i)
{
string x = "";
cout << i << endl;
for (int j = i+1; j < v.size(); ++j)
{
if (v[i] == v[j]) count++;
else { i = j-1; break;}
}
x = x + int2str(count);
x = x + "x " + v[i];
count = 1;
result.push_back(x);
}
result.erase (result.begin()+v.size()-2);
string r = "Total items: " + int2str(v.size());
result.push_back(r);
return result;
}
};
int main()
{
GroceryList gl;
string item;
getline(cin, item);
while(item != "!checkout")
{
gl.add(item);
getline(cin, item);
}
int size;
vector<string> receipt;
receipt = gl.checkout(); // this should initialize size
size = receipt.size();
cout << " Receipt: ";
int maxLen = 0;
for(int i=0; i < size-1; i++) // list of items, but not total (last in array)
{
cout << receipt[i] << endl;
if(static_cast<int>(receipt[i].length()) > maxLen)
{
maxLen = receipt[i].length();
}
}
for(int i=0; i < maxLen; i++)
{
cout << "-";
}
cout << endl;
cout << receipt[size-1] << endl; // total number of items
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.