Skip to content
This repository was archived by the owner on Jul 30, 2023. It is now read-only.

Commit 687dcf9

Browse files
committed
Lab9
1 parent c926ca9 commit 687dcf9

File tree

7 files changed

+407
-0
lines changed

7 files changed

+407
-0
lines changed

Lab9/Drug.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// Created by danilapoddubny on 15/12/2020.
3+
//
4+
5+
#include "Drug.h"
6+
#include<iostream>
7+
8+
Drug::Drug(const std::string &name, const std::string &country, int date, int rating, int sold) : name(name),
9+
country(country),
10+
date(date),
11+
rating(rating),
12+
sold(sold) {}
13+
14+
Drug::Drug() : name("name"),
15+
country("country"),
16+
date(100),
17+
rating(10),
18+
sold(100) {}
19+
20+
const std::string &Drug::getName() const {
21+
return name;
22+
}
23+
24+
void Drug::setName(const std::string &name) {
25+
Drug::name = name;
26+
}
27+
28+
const std::string &Drug::getCountry() const {
29+
return country;
30+
}
31+
32+
void Drug::setCountry(const std::string &country) {
33+
Drug::country = country;
34+
}
35+
36+
int Drug::getDate() const {
37+
return date;
38+
}
39+
40+
void Drug::setDate(int date) {
41+
Drug::date = date;
42+
}
43+
44+
int Drug::getRating() const {
45+
return rating;
46+
}
47+
48+
void Drug::setRating(int rating) {
49+
Drug::rating = rating;
50+
}
51+
52+
int Drug::getSold() const {
53+
return sold;
54+
}
55+
56+
void Drug::setSold(int sold) {
57+
Drug::sold = sold;
58+
}
59+
60+
bool operator==(const Drug &lhs, const Drug &rhs) {
61+
return lhs.name == rhs.name &&
62+
lhs.country == rhs.country &&
63+
lhs.date == rhs.date &&
64+
lhs.rating == rhs.rating &&
65+
lhs.sold == rhs.sold;
66+
}
67+
68+
bool operator<(const Drug &lhs, const Drug &rhs) {
69+
if (lhs.name < rhs.name)
70+
return true;
71+
if (rhs.name < lhs.name)
72+
return false;
73+
if (lhs.country < rhs.country)
74+
return true;
75+
if (rhs.country < lhs.country)
76+
return false;
77+
if (lhs.date < rhs.date)
78+
return true;
79+
if (rhs.date < lhs.date)
80+
return false;
81+
if (lhs.rating < rhs.rating)
82+
return true;
83+
if (rhs.rating < lhs.rating)
84+
return false;
85+
return lhs.sold < rhs.sold;
86+
}
87+
88+
bool operator>(const Drug &lhs, const Drug &rhs) {
89+
return rhs < lhs;
90+
}
91+
92+
bool operator<=(const Drug &lhs, const Drug &rhs) {
93+
return !(rhs < lhs);
94+
}
95+
96+
bool operator>=(const Drug &lhs, const Drug &rhs) {
97+
return !(lhs < rhs);
98+
}
99+
100+
std::ostream &operator<<(std::ostream &os, const Drug &drug) {
101+
os << "name: " << drug.name << "; country: " << drug.country << "; date: " << drug.date << "; rating: " << drug.rating
102+
<< "; sold: " << drug.sold;
103+
return os;
104+
}
105+
106+
std::istream& operator>>(std::istream& in, Drug& drug)
107+
{
108+
in >> drug.name;
109+
in >> drug.country;
110+
in >> drug.date;
111+
in >> drug.rating;
112+
in >> drug.sold;
113+
return in;
114+
}

Lab9/Drug.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Created by danilapoddubny on 15/12/2020.
3+
//
4+
5+
#ifndef LAB8_DRUG_H
6+
#define LAB8_DRUG_H
7+
8+
#include <string>
9+
#include <ostream>
10+
11+
class Drug {
12+
public:
13+
Drug(const std::string &name, const std::string &country, int date, int rating, int sold);
14+
Drug();
15+
16+
const std::string &getName() const;
17+
18+
void setName(const std::string &name);
19+
20+
const std::string &getCountry() const;
21+
22+
void setCountry(const std::string &country);
23+
24+
int getDate() const;
25+
26+
void setDate(int date);
27+
28+
int getRating() const;
29+
30+
void setRating(int rating);
31+
32+
int getSold() const;
33+
34+
void setSold(int sold);
35+
36+
bool operator!=(const Drug &rhs) const;
37+
38+
friend bool operator==(const Drug &lhs, const Drug &rhs);
39+
40+
friend bool operator<(const Drug &lhs, const Drug &rhs);
41+
42+
friend bool operator>(const Drug &lhs, const Drug &rhs);
43+
44+
friend bool operator<=(const Drug &lhs, const Drug &rhs);
45+
46+
friend bool operator>=(const Drug &lhs, const Drug &rhs);
47+
48+
friend std::ostream &operator<<(std::ostream &os, const Drug &drug);
49+
50+
friend std::istream &operator>>(std::istream& in, Drug& drug);
51+
52+
private:
53+
std::string name;
54+
std::string country;
55+
int date;
56+
int rating;
57+
int sold;
58+
};
59+
60+
61+
#endif //LAB8_DRUG_H

Lab9/Exception.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Created by danilapoddubny on 16/12/2020.
3+
//
4+
5+
#include "Exception.h"
6+
7+
const std::string &Exception::getMessage() const {
8+
return message;
9+
}
10+
11+
Exception::Exception(const std::string &message) : message(message) {}

Lab9/Exception.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by danilapoddubny on 16/12/2020.
3+
//
4+
5+
#ifndef LAB8_EXCEPTION_H
6+
#define LAB8_EXCEPTION_H
7+
8+
#include <string>
9+
10+
class Exception {
11+
public:
12+
Exception(const std::string &message);
13+
14+
const std::string &getMessage() const;
15+
16+
private:
17+
std::string message;
18+
19+
};
20+
21+
22+
#endif //LAB8_EXCEPTION_H

Lab9/main.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
#include "Exception.h"
3+
#include <set>
4+
#include <fstream>
5+
#include "Drug.h"
6+
#include "setCalculator.h"
7+
8+
using namespace setCalculator;
9+
10+
11+
12+
/*
13+
Контейнер: set(ключ - число, значение - объект).
14+
Алгоритмы:
15+
1. Подсчет слов в строке, которые содержат все символы из указанного набора символов.
16+
17+
18+
19+
20+
/*Общие требования к выполнению работы
21+
3. Реализовать алгоритм (алгоритм 1 в индивидуальном задании), который обрабатывает строки текстового файла согласно заданию, а результаты обработки помещает в строки другого текстового файла. Для хранения и обработки данных использовать контейнер библиотеки STL. На экран выводить отладочную информацию: какой файл открыт (или ошибка открытия файла), номер обработанной строки и результат ее обработки и т.п.
22+
4. Любую работу с данными реализовывать через обработку исключительных ситуаций через try - catch и реализовать перегрузку операторов для контейнера и итератора.*/
23+
24+
25+
26+
int main(){
27+
Drug d1("Galoperidol", "Russia", 4, 10, 420);
28+
Drug d2("Penecilin", "England", 22, 6, 212);
29+
Drug d3("Analgin", "Spain", 18, 6, 233);
30+
Drug d4("Lithium", "France", 300, 8, 90);
31+
Drug d5("Simvastatin", "Germany", 210, 7, 123);
32+
33+
Calculator drugSet;
34+
35+
drugSet.push(d1);
36+
drugSet.push(d2);
37+
drugSet.push(d3);
38+
drugSet.push(d4);
39+
drugSet.push(d5);
40+
drugSet.push(d1);
41+
drugSet.view();
42+
43+
std:: cout << "Amount of elements according to value: "<< drugSet.amount(setCalculator::Calculator::RATING, 6) << '\n';
44+
45+
std::set<Drug> secretSet;
46+
Calculator::move(drugSet.getCustomSet(), secretSet);
47+
48+
for(const auto &obj: secretSet){
49+
std::cout << obj << std::endl;
50+
}
51+
52+
53+
try
54+
{
55+
std::set<std::string> tmpSet;
56+
std::string line;
57+
58+
std::ifstream in("text1.txt");
59+
if (in.is_open()) {
60+
while (getline(in, line)) {
61+
tmpSet.insert(line);
62+
}
63+
} else throw Exception("File not founded");
64+
65+
in.close();
66+
67+
std::ofstream out;
68+
out.open("text2.txt");
69+
if (out.is_open()) {
70+
for (const auto line : tmpSet) {
71+
out << line << std::endl;
72+
}
73+
}
74+
out.close();
75+
}catch(Exception err){
76+
std::cerr << err.getMessage();
77+
}
78+
79+
80+
81+
return 0;
82+
}

Lab9/setCalculator.h

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// Created by danilapoddubny on 18/12/2020.
3+
//
4+
5+
#ifndef LAB9_SETCALCULATOR_H
6+
#define LAB9_SETCALCULATOR_H
7+
8+
namespace setCalculator{
9+
10+
class Calculator{
11+
private:
12+
std::set<Drug> customSet;
13+
public:
14+
enum fields{NAME, COUNTRY, DATE, RATING, SOLD};
15+
16+
int amount(fields c, int number){
17+
int a = 0;
18+
switch (c) {
19+
case DATE:
20+
for(auto obj : customSet){
21+
if(obj.getDate() == number)
22+
a++;
23+
24+
}
25+
break;
26+
27+
case RATING:
28+
for(auto obj : customSet){
29+
if(obj.getRating() == number)
30+
a++;
31+
32+
}
33+
break;
34+
35+
case SOLD:
36+
for(auto obj : customSet){
37+
if(obj.getSold() == number)
38+
a++;
39+
40+
}
41+
break;
42+
}
43+
return a;
44+
}
45+
46+
int amount(fields c, std::string word){
47+
int a = 0;
48+
switch (c) {
49+
case NAME:
50+
for(auto obj : customSet){
51+
if(obj.getName() == word)
52+
a++;
53+
54+
}
55+
break;
56+
case COUNTRY:
57+
for(auto obj : customSet){
58+
if(obj.getCountry() == word)
59+
a++;
60+
}
61+
break;
62+
}
63+
return a;
64+
}
65+
66+
void push(Drug obj){
67+
customSet.insert(obj);
68+
}
69+
void destroy(Drug obj){
70+
customSet.erase(obj);
71+
}
72+
auto find(Drug obj){
73+
return customSet.find(obj);
74+
}
75+
void view(){
76+
for(auto i = customSet.begin(); i != customSet.end(); i++){
77+
std::cout << *i << '\n';
78+
}
79+
}
80+
81+
static void move(const std::set<Drug> lhs, std::set<Drug> &rhs){
82+
for(auto obj: lhs){
83+
rhs.insert(obj);
84+
}
85+
}
86+
87+
const std::set<Drug> &getCustomSet() const {
88+
return customSet;
89+
}
90+
};
91+
}
92+
93+
94+
#endif //LAB9_SETCALCULATOR_H

0 commit comments

Comments
 (0)