Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minihomework3 (Sobolev Fedor) #230

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions sem1/Fedor Sobolev/MIni Homework №3/MInitask3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <memory>

//Базовый класс для предметов
class ItemBase {
public:
virtual ~ItemBase() = default;
virtual double getPrice() const = 0;
virtual double getWeight() const = 0;
};

//Общий класс Item
class Item : public ItemBase {
protected:
std::string name;
double price;
double weight;

public:
Item(const std::string& name, double price, double weight)
: name(name), price(price), weight(weight) {}

double getPrice() const override { return price; }
double getWeight() const override { return weight; }

std::string getName() const { return name; }
};

//Класс для "ForPCs"
class ForPCsItem : public Item {
std::string spec;
int performance;

public:
ForPCsItem(const std::string& name, double price, double weight, const std::string& spec, int performance)
: Item(name, price, weight), spec(spec), performance(performance) {}
};

//Класс для Fruits"
class FruitItem : public Item {
std::vector<std::string> vitamins;

public:
FruitItem(const std::string& name, double price, double weight, const std::vector<std::string>& vitamins)
: Item(name, price, weight), vitamins(vitamins) {}
};

//Класс для "Books"
class BookItem : public Item {
std::string genre;
std::string category;

public:
BookItem(const std::string& name, double price, double weight, const std::string& genre, const std::string& category)
: Item(name, price, weight), genre(genre), category(category) {}
};

//Класс для магазина, который содержит предметы
class Shop {
std::string name;
std::vector<std::shared_ptr<ItemBase>> items;

public:
Shop(const std::string& name) : name(name) {}

void addItem(const std::shared_ptr<ItemBase>& item) {
items.push_back(item);
}

double averagePrice() const {
double total = 0;
for (const auto& item : items) {
total += item->getPrice();
}
return items.empty() ? 0 : total / items.size();
}

double averageWeight() const {
double total = 0;
for (const auto& item : items) {
total += item->getWeight();
}
return items.empty() ? 0 : total / items.size();
}

void printShopInfo() const {
std::cout << "Shop: " << name << "\n"
<< "Average Price: " << averagePrice() << "\n"
<< "Average Weight: " << averageWeight() << "\n\n";
}
};

//Функция для парсинга данных из файла
std::vector<Shop> parseFile(const std::string& filename) {
std::ifstream file(filename);
std::vector<Shop> shops;
std::string line;

while (std::getline(file, line)) {
if (line.rfind("Shop", 0) == 0) {
std::string shopName = line.substr(5);
Shop shop(shopName);

std::getline(file, line);
int itemCount = std::stoi(line.substr(6));

for (int i = 0; i < itemCount; ++i) {
std::getline(file, line);
std::istringstream itemStream(line);
std::string name;
double price, weight;
std::string attribute1, attribute2;

itemStream >> name >> price >> weight >> attribute1 >> attribute2;

if (shopName == "ForPCs") {
int performance = std::stoi(attribute2);
shop.addItem(std::make_shared<ForPCsItem>(name, price, weight, attribute1, performance));
} else if (shopName == "Fruits") {
std::vector<std::string> vitamins;
vitamins.push_back(attribute1);
while (itemStream >> attribute2) {
vitamins.push_back(attribute2);
}
shop.addItem(std::make_shared<FruitItem>(name, price, weight, vitamins));
} else if (shopName == "Books") {
shop.addItem(std::make_shared<BookItem>(name, price, weight, attribute1, attribute2));
}
}

shops.push_back(shop);
}
}
return shops;
}

int main() {
std::vector<Shop> shops = parseFile("shops.txt");

for (const auto& shop : shops) {
shop.printShopInfo();
}

return 0;
}
23 changes: 23 additions & 0 deletions sem1/Fedor Sobolev/MIni Homework №3/shops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Shop ForPCs
Items 5
NVIDIA10090 100000 1000 FPS 1
NVIDIA9090 99999 999 FPS 2
IntelCoreI99 50000 1 CPU 550
AMDThreadripper 7 3 CPU 770
ASUSROGMonitor 333 10 Monitor 333

Shop Fruits
Items 5
Banana 1 1 Vitamins Ka Ca
Apple 2 1 Vitamins A C
Orange 1.5 1 Vitamins C
Grapes 2.5 0.5 Vitamins A C E
Pineapple 3 2 Vitamins B C

Shop Books
Items 5
CrimeaAndPunishment 15 0.5 Literature Classic
WarAndPeace 20 1 Literature Classic
MobyDick 10 1 Literature Adventure
CatchMeIfYouCan 12 0.8 Literature Classic
HarryPotter 25 1.2 Fantasy Adventure