Advertisement
borisdexter

Ispitna 1

Jan 26th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. *** TEKSTOT ***
  2.  
  3. Направи класа за Книга за која ќе се чуваат следните податоци: име (динамички алоцирана променлива), код, цена, курс на евро кое може да се менува во текот на програмата i ke bide isto za site knigi i pocetna vrednost 61.5 година на издавање и автор (динамички алоцирана променлива). За секоја книга преоптовари ги следните оператори:
  4. - <<
  5. - >>
  6. - < sporeduva dve knigi spored godinata na izdavanje
  7. - == споредување на две книги според кодот
  8. - += додади години на годината на издавање
  9. - =
  10. - cenaVoEvra()
  11.  
  12. Направи класа Библиотека во која ќе се чува името на библиотеката, капацитет на книги во библиотеката, динамички алоцирана низа од Книги и бројот на книги во библиотеката. За класата преоптовари ги следните оператори:
  13. - += додавање книга во библиотеката
  14. - -= бришење книга од библиотеката
  15. - postariKnigi(int godina) печати ги сите книги постари од таа година
  16. - kolkuKnigiOdAvtor(char *nekojAvtor) кој е авторот
  17. - pecatiSiteKnigi()
  18. - vkupnaCenaVoEvra()
  19. - daliSiteKnigiPosle1950()
  20. - prosecnaCenaNaKniga()
  21.  
  22.  
  23. #include<iostream>
  24. using namespace std;
  25.  
  26. class MaxCapacity{
  27. private:
  28. int kapacitet;
  29. public:
  30. MaxCapacity(){}
  31. MaxCapacity(int x){
  32. kapacitet=x;
  33. }
  34. void print(){
  35. cout<<"Maksimalniot kapacitet e "<<kapacitet<<endl;
  36. }
  37. };
  38.  
  39.  
  40. class SameBookException{
  41. private:
  42. char error[25];
  43. public:
  44. SameBookException(){}
  45. SameBookException(char error[]){
  46. strcpy(this->error,error);
  47. }
  48. void print(){
  49. cout<<error<<endl;
  50. }
  51. };
  52.  
  53. class Kniga{
  54. private:
  55. char *ime;
  56. int kod;
  57. float cena;
  58. static float evro;
  59. int godina;
  60. char *avtor;
  61. public:
  62. Kniga(){
  63. // delete []ime;
  64. // delete []avtor;
  65. this->ime=new char[0];
  66. this->avtor=new char[0];
  67. }
  68. // constructor so 5 parametri
  69. Kniga(char *ime,int kod,float cena,int godina,char *avtor){
  70. this->ime=new char[strlen(ime)+1];
  71. strcpy(this->ime,ime);
  72. this->avtor=new char[strlen(avtor)+1];
  73. strcpy(this->avtor,avtor);
  74. this->kod=kod;
  75. this->cena=cena;
  76. this->godina=godina;
  77. }
  78. // constructor so 3 parametri
  79. Kniga(char *ime,int kod,float cena){
  80. this->ime=new char[strlen(ime)+1];
  81. strcpy(this->ime,ime);
  82. this->kod=kod;
  83. this->cena=cena;
  84. // delete []avtor
  85. // dinamicki resetirame, godina moze i ne mora
  86. this->avtor=new char[0];
  87. godina=0;
  88. }
  89. // copy constructor
  90. Kniga(const Kniga &novaKniga){
  91. this->ime=new char[strlen(novaKniga.ime)+1];
  92. strcpy(this->ime,novaKniga.ime);
  93. this->avtor=new char[strlen(novaKniga.avtor)+1];
  94. strcpy(this->avtor,novaKniga.avtor);
  95. this->kod=novaKniga.kod;
  96. this->cena=novaKniga.cena;
  97. this->godina=novaKniga.godina;
  98. }
  99.  
  100. char* getAvtor(){
  101. return avtor;
  102. }
  103. // operator =
  104. Kniga &operator=(Kniga &novaKniga){
  105. // sporeduvame dvata objekti spored aresata
  106. if(this != &novaKniga){
  107. this->ime=new char[strlen(novaKniga.ime)+1];
  108. strcpy(this->ime,novaKniga.ime);
  109. this->avtor=new char[strlen(novaKniga.avtor)+1];
  110. strcpy(this->avtor,novaKniga.avtor);
  111. this->kod=novaKniga.kod;
  112. this->cena=novaKniga.cena;
  113. this->godina=novaKniga.godina;
  114. }
  115. return *this;
  116. }
  117.  
  118.  
  119. // setAvtor
  120. void setAvtor(char *novAvtor){
  121. avtor=new char[strlen(novAvtor)+1];
  122. strcpy(avtor,novAvtor);
  123. }
  124.  
  125. void setGodina(int novaGodina){
  126. godina=novaGodina;
  127. }
  128.  
  129. // output operator
  130. friend ostream &operator<<(ostream &output,Kniga &objekt){
  131. output<<"Kniga: "<<objekt.ime<<" "<<objekt.kod<<" "<<objekt.cena<<" "<<objekt.godina<<" "<<objekt.avtor<<endl;
  132. return output;
  133. }
  134.  
  135. // input operator
  136. friend istream &operator>>(istream &i, Kniga &objekt){
  137. i>>objekt.ime>>objekt.kod>>objekt.cena>>objekt.godina>>objekt.avtor;
  138. return i;
  139. }
  140.  
  141. bool operator<(Kniga &posleOperator){
  142. if(this->godina<posleOperator.godina){
  143. return true;
  144. }else{
  145. return false;
  146. }
  147. //return this->godina<posleOperator.godina;
  148. }
  149.  
  150. bool operator==(Kniga &drugaKniga){
  151. // 2 nacini
  152. /*
  153. if(this->kod==drugaKniga.kod){
  154. return true;
  155. }else{
  156. return false;
  157. }
  158. */
  159. return this->kod==drugaKniga.kod;
  160. }
  161.  
  162. void print(){
  163. cout<<"Kniga: "<<ime<<" "<<kod<<" "<<cena<<" "<<godina<<" "<<avtor<<endl;
  164. }
  165.  
  166. float cenaVoEvra(){
  167. return cena/evro;
  168. }
  169.  
  170. Kniga &operator+=(int x){
  171. godina+=x;
  172. return *this;
  173. }
  174.  
  175. static void setEvro(float novKurs){
  176. evro = novKurs;
  177. }
  178.  
  179. int getGodina(){
  180. return godina;
  181. }
  182.  
  183. ~Kniga(){
  184. delete []ime;
  185. delete []avtor;
  186. }
  187.  
  188. };
  189. float Kniga::evro=61.5;
  190.  
  191. class Biblioteka{
  192. private:
  193. char ime[25];
  194. int kapacitet;
  195. Kniga *niza;
  196. int brojKnigi;
  197. public:
  198. Biblioteka(){
  199. niza=new Kniga[0];
  200. brojKnigi=0;
  201. }
  202.  
  203. Biblioteka(char ime[],int kapacitet){
  204. niza=new Kniga[0];
  205. brojKnigi=0;
  206. strcpy(this->ime,ime);
  207. this->kapacitet=kapacitet;
  208. }
  209.  
  210. void print(){
  211. cout<<"Biblioteka: "<<ime<<" "<<kapacitet<<endl;
  212. cout<<"Knigi vo bibliotekata:"<<endl;
  213. for(int i=0;i<brojKnigi;i++){
  214. cout<<niza[i];
  215. }
  216. }
  217.  
  218. Biblioteka &operator+=(Kniga &novaKniga){
  219. if(brojKnigi==kapacitet){
  220. throw MaxCapacity(kapacitet);
  221. }
  222. int flag=0;
  223. for(int i=0;i<brojKnigi;i++){
  224. if(niza[i]==novaKniga){
  225. flag=1;
  226. throw SameBookException("Knigata veke ja imame vo nizata");
  227. break;
  228. }
  229. }
  230. if(flag==0){
  231. // si gi zacuvuvame site dosegashni knigi
  232. Kniga *tempNiza=new Kniga[brojKnigi];
  233. for(int i=0;i<brojKnigi;i++){
  234. tempNiza[i]=niza[i];
  235. }
  236. // prosiruvame nizata za +1 mesto
  237. niza = new Kniga[brojKnigi+1];
  238. // si gi vrakjame site knigi nazad vo nizata
  239. for(int i=0;i<brojKnigi;i++){
  240. niza[i]=tempNiza[i];
  241. }
  242. // ne ni treba veke tempNiza
  243. delete []tempNiza;
  244. // na posledno mesto dodavame novata kniga
  245. niza[brojKnigi]=novaKniga;
  246. // pokacuvame vkupniot broj na knigi vo nizata za +1
  247. brojKnigi++;
  248. }
  249. return *this;
  250. }
  251.  
  252. Biblioteka &operator-=(Kniga &novaKniga){
  253. int flag=0;
  254. for(int i=0;i<brojKnigi;i++){
  255. if(niza[i]==novaKniga){
  256. flag=1;
  257. break;
  258. }
  259. }
  260. if(flag==1){
  261. int brojac=0;
  262. Kniga *tempNiza=new Kniga[brojKnigi];
  263. for(int i=0;i<brojKnigi;i++){
  264. if(niza[i]==novaKniga){
  265. // go ripnuvame toj objekt t.e. ne go dodavame vo tempNiza
  266. }else{
  267. tempNiza[brojac]=niza[i];
  268. brojac++;
  269. }
  270. }
  271. niza=new Kniga[brojKnigi-1];
  272. brojKnigi--;
  273. for(int i=0;i<brojKnigi;i++){
  274. niza[i]=tempNiza[i];
  275. }
  276. delete []tempNiza;
  277. }
  278. return *this;
  279. }
  280.  
  281. void postariKnigi(int nekojaGodina){
  282. cout<<"Knigi postari od "<<nekojaGodina<<" se:"<<endl;
  283. for(int i=0;i<brojKnigi;i++){
  284. if(niza[i].getGodina()<nekojaGodina){
  285. cout<<niza[i];
  286. }
  287. }
  288. }
  289.  
  290. int kolkuKnigiOdAvtor(char *nekojAvtor){
  291. int brojac=0;
  292. for(int i=0;i<brojKnigi;i++){
  293. if(strcmp(niza[i].getAvtor(),nekojAvtor)==0){
  294. brojac++;
  295. }
  296. }
  297. return brojac;
  298. }
  299.  
  300. float vkupnaCenaVoEvra(){
  301. float suma=0;
  302. for(int i=0;i<brojKnigi;i++){
  303. suma+=niza[i].cenaVoEvra();
  304. }
  305. return suma;
  306. }
  307.  
  308. void daliSiteKnigiPosle(int nekojaGodina){
  309. bool flag=true;
  310. for(int i=0;i<brojKnigi;i++){
  311. if(niza[i].getGodina()<=nekojaGodina){
  312. flag=false;
  313. break;
  314. }
  315. }
  316. if(flag){
  317. cout<<"Site knigi se postari od "<<nekojaGodina<<endl;
  318. }else{
  319. cout<<"Ne se site knigi postari od "<<nekojaGodina<<endl;
  320. }
  321. }
  322.  
  323. float prosecnaCena(){
  324. float suma=0;
  325. for(int i=0;i<brojKnigi;i++){
  326. suma+=niza[i].cenaVoEvra();
  327. }
  328. return suma/brojKnigi;
  329. }
  330.  
  331.  
  332.  
  333. ~Biblioteka(){
  334. delete []niza;
  335. }
  336. };
  337.  
  338. int main(){
  339. cout<<"Testing parameter constructor"<<endl;
  340. Kniga lotr("The lord of the rings", 111222, 1000, 1955, "R.R. Tolkien");
  341. lotr.print();
  342. Kniga hp("Harry Potter", 222333, 1500);
  343. //hp.print();
  344. hp.setGodina(1991);
  345. hp.setAvtor("J.K. Rowling");
  346. hp.print();
  347. cout<<"Testing copy constructor"<<endl;
  348. Kniga hp2(hp);
  349. hp2.print();
  350. cout<<"Testing operator="<<endl;
  351. Kniga hp3;
  352. hp3=hp;
  353. hp3.print();
  354.  
  355. cout<<"Testing operator <<"<<endl;
  356. cout<<hp3;
  357.  
  358. cout<<"Testing operator >>"<<endl;
  359. //Kniga got;
  360. //cin>>got;
  361. Kniga got("GoT",333444,1750,1993,"R.R.Martin");
  362. cout<<got;
  363.  
  364. cout<<"Testing operator <"<<endl;
  365. if(hp<got){
  366. cout<<"HP<GOT"<<endl;
  367. }else{
  368. cout<<"HP>GOT"<<endl;
  369. }
  370.  
  371. cout<<"Testing operator =="<<endl;
  372. if(hp==got){
  373. cout<<"HP==GOT"<<endl;
  374. }else{
  375. cout<<"HP!=GOT"<<endl;
  376. }
  377.  
  378. cout<<"Testing operator +="<<endl;
  379. cout<<hp;
  380. hp+=3;
  381. cout<<hp;
  382.  
  383.  
  384.  
  385. cout<<"Testing static"<<endl;
  386. cout<<hp.cenaVoEvra()<<endl;
  387. Kniga::setEvro(62);
  388. cout<<hp.cenaVoEvra()<<endl;
  389.  
  390.  
  391.  
  392. Biblioteka bm("Brakja Miladinovci", 4);
  393. bm.print();
  394.  
  395. cout<<"Testirame += operator"<<endl;
  396. try{
  397. bm+=hp;
  398. bm+=got;
  399. bm+=lotr;
  400. bm+=lotr;
  401. }catch(SameBookException &objekt){
  402. objekt.print();
  403. }catch(MaxCapacity &objekt){
  404. objekt.print();
  405. }
  406. bm.print();
  407.  
  408. cout<<"Testirame -= operator"<<endl;
  409. bm-=hp;
  410. bm.print();
  411.  
  412. bm.postariKnigi(1950);
  413. cout<<"R.R.Martin e avtor na "<<bm.kolkuKnigiOdAvtor("R.R.Martin")<<" knigi"<<endl;
  414. cout<<"Cenata vo evra za site knigi e "<<bm.vkupnaCenaVoEvra()<<endl;
  415. bm.daliSiteKnigiPosle(1950);
  416. cout<<"Prosecna cena na kniga e "<<bm.prosecnaCena()<<endl;
  417. return 0;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement