Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include "product.h"
- int main()
- {
- ProductArray pa;
- pa.add(Product("balalayka", 803, 100000));
- pa.add(Product("doshirak", 2005, 10000000));
- pa.add(Product("hren", 1994, 100000));
- pa.add(Product("hren 1", 1993, 99999));
- pa.add(Product("kolbasa", 1989, 100000));
- pa.add(Product("", 1233, 100000));
- pa.add(Product("hren", 1994, 100000));
- pa.printMostExp();
- system("pause");
- return 0;
- }
- product.h
- //#ifndef Product_h
- #define Product_h
- // #define _CRT_SECURE_NO_WARNINGS
- #include <vector>
- #define NAME_LEN 32
- #define ARR_MAX 32
- class Product
- {
- private:
- char *name;
- int year;
- int cost;
- public:
- Product();
- ~Product();
- Product(char const * nameNew, int yearNew, int costNew);
- Product(const Product &prod);
- Product &operator=(const Product &prod);
- void setName(char const *nameNew);
- void setYear(int yearNew);
- void setCost(int costNew);
- int getYear() const;
- int getCost() const;
- char const * getName() const;
- void print();
- };
- class ProductArray
- {
- private:
- std::vector<Product> arr;
- int yearMax;
- public:
- ProductArray();
- void add(Product const & prod);
- void printMostExp();
- };
- product.cpp
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include "product.h"
- Product::Product() : name(NULL)
- {
- ;
- }
- Product::Product(char const *nameNew, int yearNew, int costNew) : name(NULL)
- {
- setName(nameNew);
- setYear(yearNew);
- setCost(costNew);
- }
- Product::Product(const Product &prod)
- {
- setName(prod.getName());
- setYear(prod.getYear());
- setCost(prod.getCost());
- }
- Product::~Product()
- {
- delete[] name;
- }
- Product &Product::operator=(const Product &prod)
- {
- setName(prod.getName());
- setYear(prod.getYear());
- setCost(prod.getCost());
- return *this;
- }
- ProductArray::ProductArray() : yearMax(1995)
- {
- }
- char const * Product::getName() const
- {
- return name;
- }
- void Product::setName(char const *nameNew)
- {
- if (name != NULL)
- free(NULL);
- int len = strlen(nameNew);
- name = new char[len + 1];
- strcpy(name, nameNew);
- //strcpy_s(name, len, nameNew);
- }
- void Product::setYear(int yearNew)
- {
- year = yearNew;
- }
- void Product::setCost(int costNew)
- {
- cost = costNew;
- }
- void Product::print()
- {
- std::cout << name << " " << year << " " << cost << " ";
- }
- int Product::getYear() const
- {
- return year;
- }
- int Product::getCost() const
- {
- return cost;
- }
- void ProductArray::add(Product const &prod)
- {
- arr.push_back(prod);
- }
- void ProductArray::printMostExp()
- {
- bool isFound = false;
- Product *maxProd = NULL;
- /* search minimal product */
- for (size_t i = 0; i < arr.size(); ++i)
- {
- if (arr[i].getYear() <= yearMax)
- {
- if (!isFound || (arr[i].getCost() > maxProd->getCost()))
- {
- maxProd = &arr[i];
- isFound = true;
- }
- }
- }
- for (size_t i = 0; i < arr.size(); ++i)
- {
- if (arr[i].getYear() <= yearMax)
- {
- if (arr[i].getCost() == maxProd->getCost())
- {
- arr[i].print();
- std::cout << std::endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement