Создайте класс Shop, добавив в него
массив товаров
метод добавления нового товара void addGoods(), информацию о товаре при этом вам нужно считать с клавиатуры
метод вывода информации о всех товарах в магазине void status(), формат вывода тот же - по строке на каждый товар
метод проверки количества или общей стоимости товаров одного типа int check(string type, string com), который должен возвращать
количество товаров заданного типа type, если com == "count"
общую стоимость всех товаров заданного типа type, если com == "price"
Отправьте в качестве решения только описанные классы.
#include < iostream>
#include < vector>
#include < string>
// Ваш код должен быть здесь
using namespace std ;
int main() {
Shop iShop ;
int comCoun t;
cin >> comCount ;
for (int i = 0; i < comCount; i++ ) {
string command ;
cin >> command ;
if (command == "add ")
iShop.addGoods( );
if (command == "check" ) {
string type, com;
cin >> type >> com;
cout << type << ":" << iShop.check( type, com ) << endl;
}
if (command == "all") {
iShop.status( );
}
}
}
входные данные
4
add Youphone electronics 25000 5
check electronics price
check electronics count
all
выходные данные
electronics:125000
electronics:5
Youphone:electronics count:5 price:25000
C/C++
Помогите пожалуйста с задачей на С++:)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Good {
public:
Good() : count_(0), price_(0) {}
string type()const {
return type_;
}
int count()const {
return count_;
}
int price()const {
return price_;
}
private:
string type_;
int count_;
int price_;
friend istream& operator>>(istream& inp, Good& good) {
auto flush = [&inp] {
inp.ignore(numeric_limits<streamsize>::max(), '\n');
};
cout << "Наименование товара: ";
getline(inp, good.type_);
cout << "Количество товара: ";
inp >> good.count_;
flush();
cout << "Цена за единицу товара: ";
inp >> good.price_;
flush();
return inp;
}
friend ostream& operator<<(ostream& out, const Good& good) {
return out << good.type_ << " " << good.count_ << " шт. " << good.price_ << " руб.";
}
};
class Goods {
public:
void addGoods() {
Good good;
cin >> good;
goods_.push_back(good);
}
void addGoods(Good&& good) {
goods_.emplace_back(good);
}
void status()const {
for (const auto& good : goods_) cout << good << '\n';
}
int check(string type, string com)const {
auto result = 0;
for (const auto& good : goods_) {
if (type == good.type()) {
if (com == "count") result += good.count();
else if (com == "price") result += good.count() * good.price();
}
}
return result;
}
private:
vector<Good> goods_;
};
int main() {
system("chcp 1251 > nul");
Goods goods;
goods.addGoods();
goods.addGoods();
goods.addGoods();
system("cls");
goods.status();
puts("");
cout << "Введите тип товара: ";
string type;
getline(cin, type);
cout << "Количество товара: " << goods.check(type, "count") << " шт. \n";
cout << "Общая стоимость товаров: " << goods.check(type, "price") << " руб. \n";
system("pause > nul");
}
#include <string>
#include <vector>
using namespace std;
class Good {
public:
Good() : count_(0), price_(0) {}
string type()const {
return type_;
}
int count()const {
return count_;
}
int price()const {
return price_;
}
private:
string type_;
int count_;
int price_;
friend istream& operator>>(istream& inp, Good& good) {
auto flush = [&inp] {
inp.ignore(numeric_limits<streamsize>::max(), '\n');
};
cout << "Наименование товара: ";
getline(inp, good.type_);
cout << "Количество товара: ";
inp >> good.count_;
flush();
cout << "Цена за единицу товара: ";
inp >> good.price_;
flush();
return inp;
}
friend ostream& operator<<(ostream& out, const Good& good) {
return out << good.type_ << " " << good.count_ << " шт. " << good.price_ << " руб.";
}
};
class Goods {
public:
void addGoods() {
Good good;
cin >> good;
goods_.push_back(good);
}
void addGoods(Good&& good) {
goods_.emplace_back(good);
}
void status()const {
for (const auto& good : goods_) cout << good << '\n';
}
int check(string type, string com)const {
auto result = 0;
for (const auto& good : goods_) {
if (type == good.type()) {
if (com == "count") result += good.count();
else if (com == "price") result += good.count() * good.price();
}
}
return result;
}
private:
vector<Good> goods_;
};
int main() {
system("chcp 1251 > nul");
Goods goods;
goods.addGoods();
goods.addGoods();
goods.addGoods();
system("cls");
goods.status();
puts("");
cout << "Введите тип товара: ";
string type;
getline(cin, type);
cout << "Количество товара: " << goods.check(type, "count") << " шт. \n";
cout << "Общая стоимость товаров: " << goods.check(type, "price") << " руб. \n";
system("pause > nul");
}
Похожие вопросы
- Помогите пожалуйста с задачей на c++, если кто-нибудь захочет помочь.
- Помогите пожалуйста решить задачу на языке С#.
- Помогите пожалуйста составить задачу на программе С++
- Помогите пожалуйста решить задачу по с++
- Помогите пожалуйста решить задачу на Си
- Помогите пожалуйста с задачей по С++
- Помогите, пожалуйста, решить задачу.
- Помогите пожалуйста доделать задачу на языке СИ!!!
- Программирование С++. Помогите, пожалуйста, решить задачу.
- Всем привет. Помогите пожалуйста с задачей на C++