Advertisement
ivanakarpuzova

Untitled

Sep 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class SMS
  7. {
  8. protected:
  9. int cena;
  10. char *sodrzina;
  11. char broj[20];
  12. public:
  13. SMS(char *Broj,char *Sodrzina, int Cena)
  14. {// sms[i] = new RegularSMS(tel, msg, cena, roam);
  15.  
  16.  
  17. sodrzina= new char[strlen(Sodrzina)+1];
  18. strcpy(sodrzina,Sodrzina);
  19. cena=Cena;
  20. strcpy(broj,Broj);
  21. }
  22. SMS(){};
  23. virtual ~SMS()
  24. {
  25. delete [] sodrzina;
  26. }
  27.  
  28. virtual float SMS_cena()=0;
  29.  
  30. friend ostream& operator<< (ostream &out, SMS &c)
  31. {
  32. return out<<c.broj<<": "<<c.sodrzina<<" - "<<c.SMS_cena()<<endl;
  33. }
  34. bool operator < (SMS &c)
  35. {
  36. return this->SMS_cena()<c.SMS_cena();
  37. }
  38.  
  39. };
  40.  
  41. class RegularSMS : public SMS
  42. {
  43. private:
  44. bool roaming;
  45. public:
  46. RegularSMS(char *Broj,char *Sodrzina, int Cena,bool Roaming) : SMS(Broj,Sodrzina,Cena)
  47. {
  48. sodrzina= new char[strlen(Sodrzina)+1];
  49. strcpy(sodrzina,Sodrzina);
  50. cena=Cena;
  51. strcpy(broj,Broj);
  52. roaming=Roaming;
  53. }
  54.  
  55. float SMS_cena()
  56. {
  57. int dolzina=strlen(sodrzina);
  58. int brojPoraki=1;
  59. if(dolzina>16)
  60. {
  61. brojPoraki=dolzina/16;
  62.  
  63. }
  64. else
  65. brojPoraki=1;
  66.  
  67. if(roaming==true)
  68. {
  69. return brojPoraki*cena + (brojPoraki*cena) * 1.2;
  70. }
  71. else
  72. return brojPoraki*cena + (brojPoraki*cena) * 0.18;
  73. }
  74.  
  75. };
  76.  
  77. class SpecialSMS : public SMS
  78. {
  79. private:
  80. bool humanitarni;
  81. public:
  82. SpecialSMS(char *Broj,char *Sodrzina, int Cena,bool Humanitarni) : SMS(Broj,Sodrzina,Cena)
  83. {
  84. sodrzina= new char[strlen(Sodrzina)+1];
  85. strcpy(sodrzina,Sodrzina);
  86. cena=Cena;
  87. strcpy(broj,Broj);
  88. humanitarni=Humanitarni;
  89. }
  90.  
  91. float SMS_cena()
  92. {
  93. if(humanitarni == true)
  94. {
  95. return cena;
  96. }
  97. else
  98. {
  99. return (float)cena+cena*0.8;
  100. }
  101. }
  102. };
  103.  
  104.  
  105. void najskapaSMS(SMS** sms, int n)
  106. { float suma=0;
  107. int index=0;
  108. float max=-99;
  109. for(int i=0;i<n;i++)
  110. {
  111. suma+=sms[i]->SMS_cena();
  112. if(max<sms[i]->SMS_cena())
  113. {
  114. max=sms[i]->SMS_cena();
  115. index=i;
  116. }
  117.  
  118. }
  119. cout<< "Najskapa poraka e :" << suma << endl;;
  120. cout<< *sms[index];
  121.  
  122.  
  123. }
  124.  
  125.  
  126. int main(){
  127.  
  128. char tel[20], msg[1000];
  129. int cena;
  130. float price;
  131. bool roam, hum;
  132. SMS **sms;
  133. int n;
  134. int tip;
  135.  
  136. int testCase;
  137. cin >> testCase;
  138.  
  139. if (testCase == 1){
  140. cout << "====== Testing RegularSMS class ======" << endl;
  141. cin >> n;
  142. sms = new SMS *[n];
  143.  
  144. for (int i = 0; i < n; i++){
  145. cin >> tel;
  146. cin.get();
  147. cin.getline(msg, 1000);
  148. cin >> cena;
  149. cin >> roam;
  150. cout << "CONSTRUCTOR" << endl;
  151. sms[i] = new RegularSMS(tel, msg, cena, roam);
  152. cout << "OPERATOR <<" << endl;
  153. cout << *sms[i];
  154. }
  155. cout << "OPERATOR <" << endl;
  156. cout << "Rezultat od sporedbata e: " << endl;
  157. if (*sms[0] < *sms[1])
  158. cout << *sms[0];
  159. else
  160. cout << *sms[1];
  161. }
  162. if (testCase == 2){
  163. cout << "====== Testing SpecialSMS class ======" << endl;
  164. cin >> n;
  165. sms = new SMS *[n];
  166.  
  167. for (int i = 0; i < n; i++){
  168. cin >> tel;
  169. cin.get();
  170. cin.getline(msg, 1000);
  171. cin >> cena;
  172. cin >> hum;
  173. cout << "CONSTRUCTOR" << endl;
  174. sms[i] = new SpecialSMS(tel, msg, cena, hum);
  175. cout << "OPERATOR <<" << endl;
  176. cout << *sms[i];
  177. }
  178. cout << "OPERATOR <" << endl;
  179. cout << "Rezultat od sporedbata e: " << endl;
  180. if (*sms[0] < *sms[1])
  181. cout << *sms[0];
  182. else
  183. cout << *sms[1];
  184. }
  185. if (testCase == 3){
  186. cout << "====== Testing method najskapaSMS() ======" << endl;
  187. cin >> n;
  188. sms = new SMS *[n];
  189.  
  190. for (int i = 0; i<n; i++){
  191.  
  192. cin >> tip;
  193. cin >> tel;
  194. cin.get();
  195. cin.getline(msg, 1000);
  196. cin >> cena;
  197. if (tip == 1) {
  198.  
  199. cin >> roam;
  200.  
  201. sms[i] = new RegularSMS(tel, msg, cena, roam);
  202.  
  203. }
  204. else {
  205. cin >> hum;
  206.  
  207. sms[i] = new SpecialSMS(tel, msg, cena, hum);
  208. }
  209. }
  210.  
  211. najskapaSMS(sms, n);
  212. }
  213. for (int i = 0; i<n; i++) delete sms[i];
  214. delete[] sms;
  215. return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement