Advertisement
Ifrail

Less3 Task7

Nov 21st, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. /*Экономические сложности*/
  2.  
  3. /*
  4. Знаменательный день превратился в знаменательную неделю и все новые и новые фракции присоединяются к Галактическому Содружеству, но пока политики празднуют, у экономистов и торговых компаний раскалывается голова. Валют стало еще больше, и вслед за буллами, старсами и борками, добавились лурки и кванги. А что дальше будет, мы даже боимся представить. К тому же из-за появления новых валют курс космокредита постоянно меняется.
  5. Чтобы решить эту проблему мы выслали промышленного шпиона в Лирианский Консорциум, чтобы тот смог выкрасть коды их торговых модулей. Всем известно, что они лучшие в этом.
  6.  
  7. Вот, что ему удалось раздобыть:
  8.  
  9. class SpaceCurrency {
  10. public:
  11.     // Курс валюты по отношению к космокредиту
  12.     double rate = 0;
  13.     string name;
  14.  
  15.     void read() {
  16.         cin >> name >> rate;
  17.     }
  18.  
  19.     int toCosmoCredits(int count) {
  20.         return count*rate;
  21.     }
  22. };
  23.  
  24. Так же они использовали vector<SpaceCurrency>, чтобы решить проблему с конвертированием валют. Мы надеемся, что вы сможете разобраться в сложившейся ситуации и доработать наши торговые модули.
  25.  
  26. Входные данные:
  27. Корабль:
  28.  * как и ранее
  29. Валюты:
  30.  * количество пар (int) пары - "имя" (string) "курс к космокредиту" (double)
  31. Команды:
  32.  * как и ранее
  33. */
  34.  
  35.  
  36. #include <iostream>
  37. #include <cmath>
  38. #include <string>
  39. #include <vector>
  40.  
  41. using namespace std;
  42.  
  43. // координаты объекта в космосе
  44. class SpacePoint {
  45. public:
  46.     int X, Y;
  47.  
  48.     int distance(SpacePoint point) {
  49.         return sqrt(pow(X - point.X, 2) + pow(Y - point.Y, 2));
  50.     }
  51.  
  52.     void read() {
  53.         cin >> X >> Y;
  54.     }
  55.  
  56.     void print() {
  57.         cout << "Cordinates: x=" << X << " y=" << Y << endl;
  58.     }
  59. };
  60.  
  61. class SpaceCurrency {
  62. public:
  63.     // Курс валюты по отношению к космокредиту
  64.     double rate = 0;
  65.     string name;
  66.  
  67.     void read() {
  68.         cin >> name >> rate;
  69.     }
  70.  
  71.     int toCosmoCredits(int count) {
  72.         return count*rate;
  73.     }
  74. };
  75.  
  76.  
  77.  
  78. // Денежный счет
  79. class CreditAccount {
  80. public:
  81.     // Галактические кредиты
  82.     int credits;
  83.     vector<SpaceCurrency> currencies;
  84.  
  85.     void putCredits(int count) {
  86.         credits += count;
  87.     }
  88.  
  89.     void putCurrency(string name, int count) {
  90.         for (int i = 0; i < currencies.size(); i++) {
  91.             if (currencies[i].name  == name) {
  92.                 credits += currencies[i].toCosmoCredits(count);
  93.                 break;
  94.             }
  95.         }
  96.     }
  97.  
  98.     void read() {
  99.         cin >> credits;
  100.         int ccount;
  101.         cin >> ccount;
  102.         for (int i = 0; i < ccount; i++) {
  103.             SpaceCurrency sc;
  104.             sc.read();
  105.             currencies.push_back(sc);
  106.         }
  107.     }
  108.    
  109.     void printSatus() {
  110.         cout << "Account: " << credits << " credits" << endl;
  111.     }
  112. };
  113.  
  114. class SpaceShip {
  115. public:
  116.     // Остаток топлива
  117.     int fuelLeft;
  118.     // Вместимость топливного бака (максимум топлива)
  119.     int fuelMax;
  120.     // Название корабля
  121.     string name;
  122.     //Координаты корабля
  123.     SpacePoint pos;
  124.     // Денежный счет
  125.     CreditAccount account;
  126.  
  127.     void printStatus() {
  128.         cout << "STATUS: " << name << endl;
  129.         pos.print();
  130.         cout << "Fuel: " << fuelLeft << " from " << fuelMax << endl;
  131.         account.printSatus();
  132.     }
  133.  
  134.     void flight(SpacePoint pt) {
  135.         fuelLeft -= pos.distance(pt);
  136.         if (fuelLeft < 0)
  137.             fuelLeft = 0;
  138.         pos = pt;
  139.     }
  140.  
  141.  
  142.     void prepare() {
  143.         cin >> name >> fuelLeft >> fuelMax;
  144.         pos.read();
  145.         account.read();
  146.     }
  147. };
  148.  
  149. int main() {
  150.  
  151.     SpaceShip ship;
  152.     ship.prepare();
  153.  
  154.     int comCount;
  155.     cin >> comCount;
  156.  
  157.     for (int i = 0; i < comCount; i++) {
  158.         string com;
  159.         cin >> com;
  160.  
  161.         if (com == "flight") {
  162.             SpacePoint pt;
  163.             pt.read();
  164.             ship.flight(pt);
  165.         }
  166.  
  167.         if (com == "buy") {
  168.             int count;
  169.             cin >> count;
  170.             ship.account.credits -= count;
  171.         }
  172.  
  173.         if (com == "cache") {
  174.             int count;
  175.             string name;
  176.             cin >> count >> name;
  177.            
  178.             if (name == "credits")
  179.                 ship.account.putCredits(count);
  180.             else
  181.                 ship.account.putCurrency(name, count);
  182.         }
  183.  
  184.         if (com == "status")
  185.             ship.printStatus();
  186.     }
  187. }
  188.  
  189. /*
  190. Тест 1
  191. Ввод:
  192. Enterprise 120 120 167 120 0
  193. 5 borks 4 stars 0.5 bools 3.5 qvangs 12 lurs 0.2
  194. 4
  195. cache 300 borks
  196. cache 1000 lurs
  197. cache 10 qvangs
  198. status
  199. Вывод:
  200. STATUS: Enterprise
  201. Cordinates: x=167 y=120
  202. Fuel: 120 from 120
  203. Account: 1520 credits
  204.  
  205. Тест 2
  206. Ввод:
  207. Enterprise 120 120 167 120 0
  208. 4 stars 0.4 bools 3 qvangs 10 lurs 0.1
  209. 6
  210. flight 130 114
  211. cache 300 bools
  212. buy 600
  213. cache 1000 lurs
  214. cache 4200 stars
  215. status
  216. Вывод:
  217. STATUS: Enterprise
  218. Cordinates: x=130 y=114
  219. Fuel: 83 from 120
  220. Account: 2080 credits
  221.  
  222. Тест 3
  223. Ввод:
  224. Enterprise 120 120 167 120 0
  225. 4 stars 0.5 bools 3.5 qvangs 12 lurs 0.2
  226. 12
  227. flight 130 114
  228. cache 300 bools
  229. buy 600
  230. cache 1000 lurs
  231. flight 100 90
  232. cache 1000 stars
  233. buy 1000
  234. cache 1000 qvangs
  235. flight 90 90
  236. buy 100
  237. cache 1000 credits
  238. status
  239. Вывод:
  240. STATUS: Enterprise
  241. Cordinates: x=90 y=90
  242. Fuel: 35 from 120
  243. Account: 13050 credits
  244. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement