Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.84 KB | None | 0 0
  1. /*
  2. ============================================================================================
  3. Преоптоварување на оператори:
  4.  
  5. // =
  6. Class& operator= (const Class &c)
  7. {
  8. arg=c.arg;
  9. return *this;
  10. }
  11.  
  12. // +=
  13. Class& operator+= (const Class &c)
  14. {
  15. arg+=c.arg;
  16. return *this;
  17. }
  18.  
  19. // -=
  20. Class& operator-= (const Class &c)
  21. {
  22. arg-=c.arg;
  23. return *this;
  24. }
  25.  
  26. // +
  27. friend Class operator+ (const Class &c1, const Class &c2)
  28. {
  29. Class tmp;
  30. tmp.arg=c1.arg + c2.arg;
  31. return tmp;
  32. }
  33.  
  34. // ==
  35. friend bool operator== (const Class &c1, const Class &c2)
  36. {
  37. if(c1.arg==c2.arg) return true;
  38. else return false;
  39. }
  40.  
  41. // ==
  42. bool operator== (const Class &c)
  43. {
  44. if(arg=c.arg) return true;
  45. else return false;
  46. }
  47.  
  48. // []
  49. char& operator[] (int i) const
  50. {
  51. return cptr[i];
  52. }
  53.  
  54. // ++
  55. Class& operator++ () const
  56. {
  57. arg++;
  58. return *this;
  59. }
  60.  
  61. // --
  62. Class& operator--() const
  63. {
  64. arg--;
  65. return *this;
  66. }
  67.  
  68. // postfix ++
  69. Class operator++ (int) const
  70. {
  71. Class t(*this);
  72. arg++;
  73. return t;
  74. }
  75.  
  76. // postfix --
  77. Class operator-- (int) const
  78. {
  79. Class t(*this);
  80. arg--;
  81. return t;
  82. }
  83.  
  84. // <<
  85. friend ostream& operator<< (ostream &out, const Class &c)
  86. {
  87. return out<<c.arg<<...<<endl;
  88. }
  89.  
  90. // >>
  91. friend istream& operator>> (istream &in, Class &c)
  92. {
  93. return in>>c.arg>>...;
  94. }
  95.  
  96. // Class& ставаме кога вршиме промена директно на тековниот објект (*this),
  97. а инаку ако само сакаме да вратиме нов објект од таа класа пишуваме само Class operator
  98.  
  99.  
  100.  
  101.  
  102.  
  103. void pecati() {
  104.         cout<<memorija<<"MB-"<<"\""<<ime<<"\""<<endl; // pechatenje vo navodnici
  105.     }
  106.  
  107.  ///////////////////////////////////////////////////////////////////////////////////////////////////////
  108.  
  109.  
  110.  Momche(char *ime="", char *prezime="", int godini=0){ // Constructor
  111.         strcpy(this->ime, ime);
  112.         strcpy(this->prezime, prezime);
  113.         this->godini = godini;
  114.     }
  115.  
  116.     Momche(const Momche &d){ //copy constructor
  117.         strcpy(ime, d.ime);
  118.         strcpy(prezime, d.prezime);
  119.         godini = d.godini;
  120.     }
  121.  
  122. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123.  
  124.  void set_from(char *from){
  125.         strcpy(this->from, from);
  126.     }
  127.  
  128.     char* get_from(){
  129.         return this->from;
  130.     }
  131.  
  132.     void set_kluc(int kluc){
  133.         this->kluc = kluc;
  134.     }
  135.  
  136.     int get_kluc(){
  137.         return this->kluc;
  138.     }
  139.  
  140. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141.  
  142.  this->text = new char[strlen(text)+1]; //Dinamichka alokacija za char
  143.         strcpy(this->text, text);
  144.  
  145. /////////////////////////////////////////////////////////////////////////////////////////////////
  146.  
  147. Lista(const Lista &l){
  148.         this->broevi = l.broevi;
  149.         nizaBroevi = new int[l.broevi]; //Dinamichka alokacija za int
  150.         for(int i=0; i<l.broevi; i++)
  151.             nizaBroevi[i]=l.nizaBroevi[i];
  152.     }
  153.  
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155.  
  156.  Lista& operator = (Lista &l){ //Preoptovaruvanje na =
  157.         if(this!= &l){
  158. return *this;
  159. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160.  
  161.  
  162.  
  163. class Repozitorium{
  164. private:
  165.  OperativenSistem *niza;
  166.   public:
  167.     Repozitorium(char *ime,int brOperativni=0, OperativenSistem *niza=0){
  168.         this->brOperativni = brOperativni;
  169.         this->niza = new OperativenSistem[brOperativni];}
  170.  
  171.  
  172. Repozitorium(const Repozitorium &r){ //////////dinamichka alokacija int
  173.  brOperativni= r.brOperativni;
  174.  
  175.         niza = new OperativenSistem[brOperativni];
  176.         for(int i=0; i<brOperativni; i++)
  177.             niza[i]=r.niza[i];
  178.  
  179.  
  180.     }
  181.  
  182. ==================================================================================================
  183.  
  184.  
  185.  
  186. Потребно е да се имплементира класа за компјутерска игра (Game), што содржи информации за:
  187.  
  188. име на играта (низа од макс. 100 знаци)
  189. цена на играта (децимален број)
  190. дали е играта купена на распродажба (bool променлива).
  191. Од класата Game да се изведе класа SubscriptionGame, што дополнително ќе чува:
  192.  
  193. месечен надоместок за играње (децимален број).
  194. датум кога играта е купена (месец и година како позитивни цели броеви)
  195. За класите Game и SubscriptionGame да се преоптоварат операторите за печатење (<< ) и читање (>>).
  196. Да се дефинира и операторот == кој ќе споредува игри според нивното име.
  197.  
  198. Да се дефинира класа за корисник (User) во која се чуваат:
  199.  
  200. кориснично име на корисникот (низа од макс. 100 знаци)
  201. колекција од игри кои се купени од корисникот (динамички алоцирана низа).
  202. Да се преоптовари операторот += кој ќе овозможи додавање на нова игра во колекцијата на игри.
  203. Притоа ако корисникот ја има веќе купено играта, потребно е да се креира исклучок од типот
  204. ExistingGame.
  205. Класата за имплементација на исклучоци потребно е има соодветен конструктор
  206. и метода message за печатење на порака на екран.
  207.  
  208. Да се креира и метода total_spent() во класата User која ќе
  209. пресметува колку пари корисникот потрошил за својата колекција од игри.
  210. Доколку играта е купена на распродажба, цената на играта е
  211. 30% од стандарната цена. Доколку играта е од типот SubscriptionGame,
  212. потребно е да се вкалкулира и сумата потрошена за месечниот
  213. надоместок (број_на_изминати_месеци x цена_на_месечен_надоместок)
  214. без да се земе во предвид моменталниот месец (мај 2018).
  215.  
  216. Да се преоптовари и оператоторот за печатење на корисникот, која печати информации
  217. во сл. формат (види тест примери 7 до 12):
  218.  
  219.  
  220.  
  221.  
  222.  
  223. #include <iostream>
  224. #include <cstring>
  225.  
  226. using namespace std;
  227.  
  228. class ExistingGame{
  229. private:
  230. char msg[256];
  231.  
  232. public:
  233. ExistingGame(char msg_txt[]){
  234. strncpy(this->msg, msg_txt, 255);
  235. this->msg[255] = '\0';
  236. }
  237.  
  238. void message(){
  239. std::cout<<this->msg<<std::endl;
  240. }
  241. };
  242.  
  243. class Game {
  244.  
  245. protected:
  246. char name[100];
  247. float price;
  248. bool on_sale;
  249. public:
  250. Game(){
  251. name[0] = '\0';
  252. }
  253. Game(char *n, float p, bool s=false){
  254. strncpy(name, n, 99);
  255. this->name[99] = '\0';
  256.  
  257. price = p;
  258.  
  259. on_sale = s;
  260. }
  261.  
  262. virtual float get_price(){
  263. if (on_sale){
  264. return price * 0.3F;
  265. }
  266.  
  267. return price;
  268. }
  269.  
  270. bool operator==(Game& g){
  271. return !strcmp(this->name, g.name);
  272. }
  273.  
  274. friend ostream & operator<<(ostream & o, const Game& g);
  275. friend istream & operator>>(istream & i, Game& g);
  276. };
  277.  
  278. class SubscriptionGame : public Game {
  279. protected:
  280. float monthly_fee;
  281. int month, year;
  282.  
  283. public:
  284. SubscriptionGame(){
  285. name[0] = '\0';
  286. }
  287.  
  288. SubscriptionGame(char *n, float p, bool s, float mf, int m, int y):
  289. Game(n, p, s),
  290. monthly_fee(mf),
  291. month(m),
  292. year(y) { }
  293.  
  294. float get_price(){
  295. float price = Game::get_price();
  296.  
  297. int months=0;
  298. if (year<2018){
  299. months = (12 - this->month) + (2017 - year)*12 + 5;
  300. }
  301. else {
  302. months = 5 - this->month;
  303. }
  304.  
  305. price += months * monthly_fee;
  306.  
  307. return price;
  308. }
  309.  
  310. friend ostream & operator<<(ostream & o, SubscriptionGame& sg);
  311. friend istream & operator>>(istream & i, SubscriptionGame& g);
  312.  
  313. };
  314.  
  315. ostream & operator<<(ostream & o, const Game& g) {
  316. o<<"Game: "<< g.name <<", regular price: $" << g.price;
  317.  
  318. if (g.on_sale){
  319. o<<", bought on sale";
  320. }
  321. return o;
  322. }
  323.  
  324. ostream & operator<<(ostream & o, SubscriptionGame& sg) {
  325.  
  326. Game * tmp = dynamic_cast<Game*>(&sg);
  327.  
  328. o << *tmp;
  329.  
  330. o<<", monthly fee: $"<< sg.monthly_fee
  331. <<", purchased: "<< sg.month << "-" << sg.year<<std::endl;
  332.  
  333. return o;
  334. }
  335.  
  336. istream & operator>>(istream & is, Game &g){
  337.  
  338. is.get();
  339. is.getline(g.name,100);
  340. is>>g.price>>g.on_sale;
  341.  
  342. return is;
  343. }
  344.  
  345. istream & operator>>(istream & is, SubscriptionGame &g){
  346. is.get();
  347. is.getline(g.name,100);
  348. is>>g.price>>g.on_sale;
  349. is>>g.monthly_fee >> g.month >> g.year;
  350. return is;
  351. }
  352.  
  353. class User {
  354. private:
  355. void obj_copy(const User * orig, User * cpy){
  356. strcpy(cpy->username, orig->username);
  357. cpy->num_games = orig->num_games;
  358.  
  359. cpy->games = new Game*[cpy->num_games];
  360.  
  361. for (int i=0; i< cpy->num_games; ++i){
  362. cpy->games[i] = new Game(*(orig->games[i]));
  363. }
  364. }
  365.  
  366. protected:
  367. char username[100];
  368. Game ** games;
  369. int num_games;
  370. public:
  371. User (char const * const un="") {
  372. strncpy(this->username, un, 99);
  373. this->username[99] = '\0';
  374. games = 0;
  375. num_games = 0;
  376. }
  377.  
  378. User (const User& orig){
  379. obj_copy(&orig,this);
  380. }
  381.  
  382. ~User(){
  383. for (int i=0; i < this->num_games; ++i){
  384. delete this->games[i];
  385. }
  386. delete [] games;
  387. }
  388.  
  389. User& operator=(User & orig){
  390. if (&orig != this){
  391.  
  392. for (int i=0; i < this->num_games; ++i){
  393. delete orig.games[i];
  394. }
  395.  
  396. delete [] orig.games;
  397.  
  398. obj_copy(&orig, this);
  399. }
  400. return *this;
  401. }
  402.  
  403. User& operator+=(Game&g){
  404.  
  405. Game ** new_games = new Game*[this->num_games+1];
  406.  
  407. for (int i=0; i < (this->num_games); ++i) {
  408. if ( (*(this->games[i])) == g){
  409. throw ExistingGame("The game is already in the collection");
  410. }
  411.  
  412. new_games[i] = games[i];
  413. }
  414.  
  415. for (int i=0; i < (this->num_games); ++i) {
  416. new_games[i] = games[i];
  417. }
  418.  
  419. SubscriptionGame * sg = dynamic_cast< SubscriptionGame* >(&g);
  420.  
  421. if(sg){
  422.  
  423. new_games[num_games] = new SubscriptionGame(*sg);
  424. }
  425. else {
  426. //cout<<"Game"<<endl;
  427. new_games[num_games] = new Game(g);
  428. }
  429.  
  430. delete [] this->games;
  431. this->games = new_games;
  432. this->num_games++;
  433.  
  434. //cout<<"User: "<< this->username<<endl;
  435.  
  436. // for (int i=0; i<this->num_games;++i){
  437. // cout<< *(this->games[i]);
  438. // }
  439.  
  440. return *this;
  441. }
  442.  
  443. Game& get_game(int i){
  444. return (*(this->games[i]));
  445. }
  446.  
  447. float total_spent(){
  448. float sum = 0.0f;
  449. for (int i=0; i<this->num_games; ++i){
  450. sum += games[i]->get_price();
  451. }
  452. return sum;
  453. }
  454.  
  455. char const * const get_username(){
  456. return this->username;
  457. }
  458.  
  459. int get_games_number(){
  460. return this->num_games;
  461. }
  462.  
  463. };
  464.  
  465. ostream & operator<<(ostream & o, User& u) {
  466.  
  467. o<<"\nUser: "<<u.get_username()<<"\n";
  468.  
  469. for (int i=0; i < u.get_games_number(); ++i){
  470. Game * g;
  471. SubscriptionGame * sg;
  472. g = &(u.get_game(i));
  473.  
  474. sg = dynamic_cast<SubscriptionGame *> (g);
  475.  
  476. if (sg){
  477. cout<<"- "<<(*sg);
  478. }
  479. else {
  480. cout<<"- "<<(*g);
  481. }
  482. cout<<"\n";
  483. }
  484. return o;
  485. }
  486.  
  487. int main() {
  488. int test_case_num;
  489.  
  490. cin>>test_case_num;
  491.  
  492. // for Game
  493. char game_name[100];
  494. float game_price;
  495. bool game_on_sale;
  496.  
  497. // for SubscritionGame
  498. float sub_game_monthly_fee;
  499. int sub_game_month, sub_game_year;
  500.  
  501. // for User
  502. char username[100];
  503. int num_user_games;
  504.  
  505. if (test_case_num == 1){
  506. cout<<"Testing class Game and operator<< for Game"<<std::endl;
  507. cin.get();
  508. cin.getline(game_name,100);
  509. //cin.get();
  510. cin>>game_price>>game_on_sale;
  511.  
  512. Game g(game_name, game_price, game_on_sale);
  513.  
  514. cout<<g;
  515. }
  516. else if (test_case_num == 2){
  517. cout<<"Testing class SubscriptionGame and operator<< for
  518. SubscritionGame"<<std::endl;
  519. cin.get();
  520. cin.getline(game_name, 100);
  521.  
  522. cin>>game_price>>game_on_sale;
  523.  
  524. cin>>sub_game_monthly_fee;
  525. cin>>sub_game_month>>sub_game_year;
  526.  
  527. SubscriptionGame sg(game_name, game_price, game_on_sale,
  528. sub_game_monthly_fee, sub_game_month, sub_game_year);
  529. cout<<sg;
  530. }
  531. else if (test_case_num == 3){
  532. cout<<"Testing operator>> for Game"<<std::endl;
  533. Game g;
  534.  
  535. cin>>g;
  536.  
  537. cout<<g;
  538. }
  539. else if (test_case_num == 4){
  540. cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
  541. SubscriptionGame sg;
  542.  
  543. cin>>sg;
  544.  
  545. cout<<sg;
  546. }
  547. else if (test_case_num == 5){
  548. cout<<"Testing class User and operator+= for User"<<std::endl;
  549. cin.get();
  550. cin.getline(username,100);
  551. User u(username);
  552.  
  553. int num_user_games;
  554. int game_type;
  555. cin >>num_user_games;
  556.  
  557. try {
  558.  
  559. for (int i=0; i<num_user_games; ++i){
  560.  
  561. cin >> game_type;
  562.  
  563. Game *g;
  564. // 1 - Game, 2 - SubscriptionGame
  565. if (game_type == 1){
  566. cin.get();
  567. cin.getline(game_name, 100);
  568.  
  569. cin>>game_price>>game_on_sale;
  570. g = new Game(game_name, game_price, game_on_sale);
  571. }
  572. else if (game_type == 2){
  573. cin.get();
  574. cin.getline(game_name, 100);
  575.  
  576. cin>>game_price>>game_on_sale;
  577.  
  578. cin>>sub_game_monthly_fee;
  579. cin>>sub_game_month>>sub_game_year;
  580. g = new SubscriptionGame(game_name, game_price, game_on_sale,
  581. sub_game_monthly_fee, sub_game_month, sub_game_year);
  582. }
  583.  
  584. //cout<<(*g);
  585.  
  586. u+=(*g);
  587. }
  588. }catch(ExistingGame &ex){
  589. ex.message();
  590. }
  591.  
  592. cout<<u;
  593.  
  594. }
  595. else if (test_case_num == 6){
  596. cout<<"Testing exception ExistingGame for User"<<std::endl;
  597. cin.get();
  598. cin.getline(username,100);
  599. User u(username);
  600.  
  601. int num_user_games;
  602. int game_type;
  603. cin >>num_user_games;
  604.  
  605. for (int i=0; i<num_user_games; ++i){
  606.  
  607. cin >> game_type;
  608.  
  609. Game *g;
  610. // 1 - Game, 2 - SubscriptionGame
  611. if (game_type == 1){
  612. cin.get();
  613. cin.getline(game_name, 100);
  614.  
  615. cin>>game_price>>game_on_sale;
  616. g = new Game(game_name, game_price, game_on_sale);
  617. }
  618. else if (game_type == 2){
  619. cin.get();
  620. cin.getline(game_name, 100);
  621.  
  622. cin>>game_price>>game_on_sale;
  623.  
  624. cin>>sub_game_monthly_fee;
  625. cin>>sub_game_month>>sub_game_year;
  626. g = new SubscriptionGame(game_name, game_price,
  627. game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  628. }
  629.  
  630. //cout<<(*g);
  631.  
  632. try {
  633. u+=(*g);
  634. }
  635. catch(ExistingGame &ex){
  636. ex.message();
  637. }
  638. }
  639.  
  640. cout<<u;
  641.  
  642. }
  643. else if (test_case_num == 7){
  644. cout<<"Testing total_spent method() for User"<<std::endl;
  645. cin.get();
  646. cin.getline(username,100);
  647. User u(username);
  648.  
  649. int num_user_games;
  650. int game_type;
  651. cin >>num_user_games;
  652.  
  653. for (int i=0; i<num_user_games; ++i){
  654.  
  655. cin >> game_type;
  656.  
  657. Game *g;
  658. // 1 - Game, 2 - SubscriptionGame
  659. if (game_type == 1){
  660. cin.get();
  661. cin.getline(game_name, 100);
  662.  
  663. cin>>game_price>>game_on_sale;
  664. g = new Game(game_name, game_price, game_on_sale);
  665. }
  666. else if (game_type == 2){
  667. cin.get();
  668. cin.getline(game_name, 100);
  669.  
  670. cin>>game_price>>game_on_sale;
  671.  
  672. cin>>sub_game_monthly_fee;
  673. cin>>sub_game_month>>sub_game_year;
  674. g = new SubscriptionGame(game_name, game_price,
  675. game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  676. }
  677.  
  678. //cout<<(*g);
  679.  
  680. u+=(*g);
  681. }
  682.  
  683. cout<<u;
  684.  
  685. cout<<"Total money spent: $"<<u.total_spent()<<endl;
  686. }
  687. }
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement