fork download
  1. /*************************/
  2. /*/ ---CPP11 ISO--- /*/
  3. /*/ /*/
  4. /*/ dodaj ako ti nije /*/
  5. /*/ vec defaultno /*/
  6. /*/ pri kompilaciji /*/
  7. /*/ -std=c++11 /*/
  8. /*************************/
  9.  
  10. #include <iostream>
  11. #include <vector>
  12. #include <algorithm>
  13.  
  14. using namespace std;
  15. typedef unsigned int uint;
  16.  
  17. #define NOT_IN_ORDER 0xff // definiraj invariant (ono sto ne pase)
  18. // vidi nize i bit ce jasnije
  19.  
  20.  
  21. // struct Menu nosi osnovne podatke
  22. struct Menu
  23. {
  24. Menu(string menuItem , float menuPrice);
  25.  
  26. string MenuItem;
  27. float MenuPrice;
  28.  
  29. };
  30.  
  31. Menu::Menu(string menuItem , float menuPrice)
  32. : MenuItem(menuItem) , MenuPrice(menuPrice)
  33. {
  34. }
  35.  
  36.  
  37. // u klasi Restaurant rijesavamo cijeli zadatak
  38. class Restaurant
  39. {
  40. public:
  41. Restaurant();
  42.  
  43. // serve i display su dostupni
  44. // ostalo nema potrebe biti public
  45. // jer samo Restaurant interno
  46. // barata onime sto je pod private
  47. void serve();
  48. void display();
  49.  
  50.  
  51. private:
  52. vector<Menu> m_menus
  53. {
  54. // c++11 dopusta iniciranje u samoj klasi
  55. // iskoristit cemo to upravo sad
  56. Menu("Plain_Egg",1.45),
  57. Menu("Bacon_and_Egg",2.45),
  58. Menu("Muffin",0.99),
  59. Menu("French_Toast",1.45),
  60. Menu("Fruit_Basket",2.49),
  61. Menu("Cereal",0.69),
  62. Menu("Coffee",0.50),
  63. Menu("Tea",0.75)
  64. };
  65.  
  66. // ovime vrsimo provjeru unesenog MenuItem-a
  67. uint processWhat(string what);
  68.  
  69. // u order punimo index odnosno offsete m_menus-a
  70. // vidi nize da bude jasnije
  71. vector<uint> order;
  72.  
  73. };
  74.  
  75. Restaurant::Restaurant()
  76. {
  77. cout<< "u ponudi restauranta imamo :\n\n";
  78.  
  79. // c++11 auto je dobra stvar koja ubrzava code
  80. // dakle iskoristimo ga
  81. for(auto x : m_menus)
  82. cout<< x.MenuItem << " " << x.MenuPrice << endl;
  83. }
  84.  
  85.  
  86. void Restaurant::serve()
  87. {
  88. cout<< "\nchoose menu\n\n";
  89.  
  90. string terminateString = "done";
  91. string what = "";
  92.  
  93. while(what != terminateString)
  94. {
  95. cin>> what;
  96.  
  97. uint offset = processWhat(what);
  98. if(offset != NOT_IN_ORDER)
  99. {
  100. // ukoliko je narudzba jednaka posojecem menu
  101. // u order stavljamo offset iz m_menus-a
  102. // vidi jos nize
  103. order.push_back(offset);
  104. cout<< "OK , another one or type done for exit\n";
  105. }
  106. else
  107. cout<< "not in order , try again or type done for exit\n";
  108.  
  109. }
  110.  
  111. display();
  112. }
  113.  
  114.  
  115. uint Restaurant::processWhat(string what)
  116. {
  117. vector<Menu>::iterator it = find_if(m_menus.begin(),m_menus.end(), [&] (Menu a)
  118. { //c++11 lambda
  119. return (a.MenuItem == what);
  120. }
  121. );
  122. // ukoliko menu koji je user upisao postoji
  123. // njegov offset u m_menus-u dobijes pointer aritmetikom
  124. // it - m_menus.begin() a ukoliko menu ne postoji vrati invariant
  125. return (it == m_menus.end() ? NOT_IN_ORDER : (it - m_menus.begin()));
  126. }
  127.  
  128.  
  129. void Restaurant::display()
  130. {
  131. // na kraju ispis svega
  132. float price = 0;
  133.  
  134. cout<< "\nNARUDZBA SADRZI :\n";
  135. for(auto x : order)
  136. {
  137. cout<< m_menus[x].MenuItem << endl;
  138. price += m_menus[x].MenuPrice;
  139. }
  140.  
  141. cout<< "\n\n\nTOTAL : " << price << "$\nDODJITE NAM OPET\n\n";
  142. }
  143.  
  144.  
  145.  
  146. int main()
  147. {
  148. Restaurant BUG_RESTAURANT;
  149. BUG_RESTAURANT.serve();
  150.  
  151.  
  152. // pozdrav i sretno
  153. return 0;
  154. }
  155.  
Success #stdin #stdout 0s 3440KB
stdin
Bacon_and_Egg Muffin Coffee done
stdout
u ponudi restauranta imamo :

Plain_Egg 1.45
Bacon_and_Egg 2.45
Muffin 0.99
French_Toast 1.45
Fruit_Basket 2.49
Cereal 0.69
Coffee 0.5
Tea 0.75

choose menu

OK , another one or type done for exit
OK , another one or type done for exit
OK , another one or type done for exit
not in order , try again or type done for exit

NARUDZBA SADRZI :
Bacon_and_Egg
Muffin
Coffee



TOTAL : 3.94$
DODJITE NAM OPET