/
eremenko_danila
/
lab8
Обзор
Документация
Войти
/
eremenko_danila
/
lab8
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
261 строка
8 KB
Eremenko_Dabila
все работает
28 дек 2024, 19:18
28 дек 2024, 19:18
3a6eb32
Код
Авторство
О чём код?
#include <iostream> #include <fstream> #include <algorithm> #include <regex> #include <random> #include <string> #include <list> #include <map> #include <iterator> using namespace std; class Inventory { private: string item; double cost; int on_hand; public: Inventory() : item(""), cost(0), on_hand(0) {}; ~Inventory(){}; Inventory(string item, double cost, int on_hand) : item(item), cost(cost), on_hand(on_hand) {}; bool operator<(const Inventory& other) const { return cost < other.cost; } bool operator==(const Inventory& other) const { return item == other.item && cost == other.cost && on_hand == other.on_hand; } string get_item() const { return item; }; double get_cost() const { return cost; }; int get_on_hand() const { return on_hand; }; }; void print_vector(vector<int> numbers){ for (int i = 0; i < numbers.size(); i++) { cout << numbers[i] << " "; } cout << endl; } class Box { double a; // ширина double b; // высота double c; // длина public: Box() : a(0), b(0), c(0) {} Box(double a, double b, double c) : a(a), b(b), c(c) {} double volume() const { return a * b * c; } void print() { cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << "volume = " << volume() << endl; } bool operator<(const Box& other) const { return this->volume() < other.volume(); } bool operator==(const Box& other) const { return this->volume() == other.volume(); } }; void print_number5(vector<Inventory> mass){ for (int i = 0; i < mass.size(); i++) { cout << mass[i].get_item() << " - " << mass[i].get_cost() << " - " << mass[i].get_on_hand() << endl; } } void print_list(list<double> lst) { for (double i : lst) { cout << i << " "; } cout << endl; } void number1(){ string str; cout << "Номер №1: Пользователь вводит текст. Сколько раз в нем встречается символ =" << endl; cout << "Введите строчку: " << endl; cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::getline(cin, str); int cnt=0; for (int i = 0; i < str.length(); i++) { if (str[i] == '=') { cnt++; } } cout << cnt << endl; } void number2(){ string str, str2; cout << "Номер №2: Пользователь вводит некоторый текст. Переписать в другую переменную все символы за исключением цифр и символов арифметических операций.." << endl; cout << "Введите строчку: " << endl; cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); getline(cin, str); std::string result = regex_replace(str, regex("[\\d\\+\\-\\*\\/]"), ""); cout << result << endl; } void number3(){ std::string line; std::ifstream in("text.txt"); // открываем файл в режиме чтения std::ofstream out("result.txt"); // открываем файл в режиме записи cout << "Номер №3: Дан текстовый файл. Запишите в другой файл содержимое исходного файла кроме цифр." << endl; if (in.is_open() && out.is_open()){ while (getline(in, line)) { std::string result = regex_replace(line, regex("[\\d]"), ""); out << result << endl; } in.close(); out.close(); cout << "Сделано" << endl; } else { cout << "Error opening file" << endl; } } void number4(){ cout << "Номер 4 :Создать вектор из 6-ти вещественных случайных чисел от ( − 100 до 100)" <<endl; cout << "Распечатать. Из первого вектора создать второй вектор, который содержит только отрицательные элементы первого вектора" << endl; cout << "и распечатать его. Удалить из первого вектора элемент с индексом 4 и снова распечатать вектор." << endl; cout << "Переделать программу: печатать векторы с помощью функции." << endl; vector<int> numbers, number2; srand(time(NULL)); std::random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(-100, 100); for (int i = 0; i < 6; i++) { numbers.push_back(dis(gen)); } print_vector(numbers); std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(number2), [](int i){ return i < 0; }); print_vector(number2); numbers.erase(numbers.begin()+3); print_vector(numbers); } void number5(){ vector<Inventory> mass, mass2; mass.push_back(Inventory("Отверка", 99, 0)); mass.push_back(Inventory("Молоток", 430, 10)); mass.push_back(Inventory("Гайки", 70, 100)); mass.push_back(Inventory("Профиль", 540, 0)); mass.push_back(Inventory("Уголок", 230, 9)); mass.push_back(Inventory("Доска", 350, 17)); print_number5(mass); std::copy_if(mass.begin(), mass.end(), std::back_inserter(mass2), [](Inventory i){ return i.get_on_hand() == 0; }); print_number5(mass2); } void number6(){ list<double> lst1, lst2; random_device rd; mt19937 gen(rd()); uniform_real_distribution<> dis(-100, 100); for (int i = 0; i < 6; i++) { lst1.push_back(dis(gen)); } print_list(lst1); copy_if(lst1.begin(), lst1.end(), back_inserter(lst2), [](double i){ return i < 0; }); print_list(lst2); auto it = lst1.begin(); advance(it, 4); lst1.erase(it); } void number7(){ list<Box> boxes; boxes.push_back(Box(1, 2, 3)); boxes.push_back(Box(14, 57, 19)); boxes.push_back(Box(7, 32, 20)); boxes.push_back(Box(5, 13, 23)); boxes.push_back(Box(10, 19, 45)); boxes.push_back(Box(19, 9, 59)); list<Box>::iterator max = boxes.begin(); for(auto i = boxes.begin(); i!= boxes.end(); i++) { i->print(); if(i->volume() > max->volume()) { max = i;} } max->print(); } void number8(){ map<char, int>map; map['a'] = static_cast<int>('a'); map['b'] = static_cast<int>('b'); map['c'] = static_cast<int>('c'); map['d'] = static_cast<int>('d'); map['e'] = static_cast<int>('e'); for (auto i = map.begin(); i != map.end(); i++) { cout << i->first << " - " << i->second << endl; } auto it = map.begin(); advance(it, 1); map.erase(it); for(auto i = map.begin(); i != map.end(); i++) { cout << i->first << " - " << i->second << endl; } } void number9(){ std::map<std::string, int> Shop1 = { {"банан", 400}, {"яблоко", 300} }; std::map<std::string, int> Shop2 = { {"банан", 750}, {"груша", 20}, {"яблоко", 150} }; std::map<std::string, int> result; for (auto& pair : Shop1) { result[pair.first] += pair.second; } for (auto& pair : Shop2) { result[pair.first] += pair.second; } for (auto& pair : result) { cout << pair.first << " - " << pair.second << endl; } } int main() { #ifdef WIN32 system("chcp 65001"); #else setlocale(LC_ALL, "Russian"); #endif int flg = 0, user; while (flg == 0){ cout << "Введите число- номер задания(1-9), 0 - выход " << endl; cin >> user; switch(user){ case 0: flg = 1; break; case 1: number1(); break; case 2: number2(); break; case 3: number3();break; case 4: number4();break; case 5: number5(); break; case 6: number6(); break; case 7: number7(); break; case 8: number8(); break; case 9: number9(); break; default: cout << "Введите число- номер задания(1-8) " << endl; break; } } return 0; }