Advertisement
Garey

Tema12

Dec 18th, 2018
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6. #include <tuple>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. #ifndef _AUTOMOBILE_HPP_
  12. #define _AUTOMOBILE_HPP_
  13.  
  14. class Automobile {
  15. protected:
  16.     size_t power;
  17.    
  18.     string model;
  19.     string number;
  20.     string owner;
  21.  
  22. public:
  23.     Automobile() : power(0), model(""), number(""), owner("") {};
  24.     Automobile(size_t power, string model, string number, string owner) :
  25.         power(power),
  26.         model(model),
  27.         number(number),
  28.         owner(owner) {};
  29.  
  30.     Automobile(const Automobile &obj) : power(obj.power), model(obj.model), number(obj.number), owner(obj.owner) {};
  31.  
  32.     virtual ~Automobile();
  33.  
  34.     inline auto getNumber() -> string {
  35.         return this->number;
  36.     }
  37.  
  38.     auto operator==(const Automobile &obj) -> bool {
  39.         return (this->owner == obj.owner);
  40.     }
  41.  
  42.     auto operator<(const Automobile &obj) -> bool {
  43.         return (this->number < obj.number);
  44.     }
  45.  
  46.     friend auto operator<<(ostream &output, const Automobile &obj) -> ostream & {
  47.  
  48.         output << "Owner: " << obj.owner << endl;
  49.         output << "Model: " << obj.model << endl;
  50.         output << "Number: " << obj.number << endl;
  51.         output << "Power: " << obj.power << endl;
  52.         output << "City: " << obj.code_to_city[obj.getCode(obj.number)] << endl;
  53.  
  54.         return output;
  55.     }
  56.  
  57.     friend auto operator>>(istream &input, Automobile &obj) -> istream & {
  58.  
  59.         input >> obj.owner;
  60.         input >> obj.model;
  61.         input >> obj.number;
  62.         input >> obj.power;
  63.  
  64.         obj.city_code = obj.number.substr(0, 2);
  65.         obj.city_code.erase(remove_if(city_code.begin(), city_code.end(), isspace), city_code.end());
  66.  
  67.         return input;
  68.     }
  69.  
  70.     /*!
  71.      * Statics
  72.      */
  73.     static auto initStatics() -> void {
  74.         code_to_city["V"] = "Varna";
  75.         code_to_city["S"] = "Sofia";
  76.         code_to_city["PV"] = "Plovdiv";
  77.  
  78.         city_to_code.insert(pair<string, string>("Varna", "V"));
  79.         city_to_code.insert(pair<string, string>("Sofia", "S"));
  80.         city_to_code.insert(pair<string, string>("Plovdiv", "PV"));
  81.     }
  82.  
  83.  
  84.     static string city_code;
  85.  
  86.     static map<string, string> code_to_city;
  87.     static multimap<string, string> city_to_code;
  88.  
  89.     static auto getCode(string number) -> string;
  90. };
  91.  
  92. #endif
  93.  
  94. ------------------
  95.  
  96. #include "Automobile.hpp"
  97.  
  98. Automobile::~Automobile() {}
  99.  
  100. /*!
  101.  * Statics
  102.  */
  103.  
  104. string Automobile::city_code;
  105. map<string, string> Automobile::code_to_city;
  106. multimap<string, string> Automobile::city_to_code;
  107.  
  108. auto Automobile::getCode(string number) -> string {
  109.     city_code = number.substr(0, 2);
  110.  
  111.     city_code.erase(remove_if(city_code.begin(), city_code.end(), isspace), city_code.end());
  112.  
  113.     return city_code;
  114. }
  115.  
  116. --------------------------
  117.  
  118. #include "Automobile.hpp"
  119.  
  120. int main() {
  121.  
  122.     Automobile test;
  123.  
  124.     cout << test.getCode("V 5400");
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement