Advertisement
borisdexter

Ispitni 1 chas

Jan 25th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 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. *** ZADACATA ***
  23.  
  24.  
  25. #include<iostream>
  26. using namespace std;
  27.  
  28. class MaxCapacity{
  29. private:
  30. int kapacitet;
  31. public:
  32. MaxCapacity(){}
  33. MaxCapacity(int x){
  34. kapacitet=x;
  35. }
  36. void print(){
  37. cout<<"Maksimalniot kapacitet e "<<kapacitet<<endl;
  38. }
  39. };
  40.  
  41.  
  42. class SameBookException{
  43. private:
  44. char error[25];
  45. public:
  46. SameBookException(){}
  47. SameBookException(char error[]){
  48. strcpy(this->error,error);
  49. }
  50. void print(){
  51. cout<<error<<endl;
  52. }
  53. };
  54.  
  55. class Kniga{
  56. private:
  57. char *ime;
  58. int kod;
  59. float cena;
  60. static float evro;
  61. int godina;
  62. char *avtor;
  63. public:
  64. Kniga(){
  65. // delete []ime;
  66. // delete []avtor;
  67. this->ime=new char[0];
  68. this->avtor=new char[0];
  69. }
  70. // constructor so 5 parametri
  71. Kniga(char *ime,int kod,float cena,int godina,char *avtor){
  72. this->ime=new char[strlen(ime)+1];
  73. strcpy(this->ime,ime);
  74. this->avtor=new char[strlen(avtor)+1];
  75. strcpy(this->avtor,avtor);
  76. this->kod=kod;
  77. this->cena=cena;
  78. this->godina=godina;
  79. }
  80. // constructor so 3 parametri
  81. Kniga(char *ime,int kod,float cena){
  82. this->ime=new char[strlen(ime)+1];
  83. strcpy(this->ime,ime);
  84. this->kod=kod;
  85. this->cena=cena;
  86. // delete []avtor
  87. // dinamicki resetirame, godina moze i ne mora
  88. this->avtor=new char[0];
  89. godina=0;
  90. }
  91. // copy constructor
  92. Kniga(const Kniga &novaKniga){
  93. this->ime=new char[strlen(novaKniga.ime)+1];
  94. strcpy(this->ime,novaKniga.ime);
  95. this->avtor=new char[strlen(novaKniga.avtor)+1];
  96. strcpy(this->avtor,novaKniga.avtor);
  97. this->kod=novaKniga.kod;
  98. this->cena=novaKniga.cena;
  99. this->godina=novaKniga.godina;
  100. }
  101.  
  102. // operator =
  103. Kniga &operator=(Kniga &novaKniga){
  104. // sporeduvame dvata objekti spored aresata
  105. if(this != &novaKniga){
  106. this->ime=new char[strlen(novaKniga.ime)+1];
  107. strcpy(this->ime,novaKniga.ime);
  108. this->avtor=new char[strlen(novaKniga.avtor)+1];
  109. strcpy(this->avtor,novaKniga.avtor);
  110. this->kod=novaKniga.kod;
  111. this->cena=novaKniga.cena;
  112. this->godina=novaKniga.godina;
  113. }
  114. return *this;
  115. }
  116.  
  117.  
  118. // setAvtor
  119. void setAvtor(char *novAvtor){
  120. avtor=new char[strlen(novAvtor)+1];
  121. strcpy(avtor,novAvtor);
  122. }
  123.  
  124. void setGodina(int novaGodina){
  125. godina=novaGodina;
  126. }
  127.  
  128. // output operator
  129. friend ostream &operator<<(ostream &output,Kniga &objekt){
  130. output<<"Kniga: "<<objekt.ime<<" "<<objekt.kod<<" "<<objekt.cena<<" "<<objekt.godina<<" "<<objekt.avtor<<endl;
  131. return output;
  132. }
  133.  
  134. // input operator
  135. friend istream &operator>>(istream &i, Kniga &objekt){
  136. i>>objekt.ime>>objekt.kod>>objekt.cena>>objekt.godina>>objekt.avtor;
  137. return i;
  138. }
  139.  
  140. bool operator<(Kniga &posleOperator){
  141. if(this->godina<posleOperator.godina){
  142. return true;
  143. }else{
  144. return false;
  145. }
  146. //return this->godina<posleOperator.godina;
  147. }
  148.  
  149. bool operator==(Kniga &drugaKniga){
  150. // 2 nacini
  151. /*
  152. if(this->kod==drugaKniga.kod){
  153. return true;
  154. }else{
  155. return false;
  156. }
  157. */
  158. return this->kod==drugaKniga.kod;
  159. }
  160.  
  161. void print(){
  162. cout<<"Kniga: "<<ime<<" "<<kod<<" "<<cena<<" "<<godina<<" "<<avtor<<endl;
  163. }
  164.  
  165. float cenaVoEvra(){
  166. return cena/evro;
  167. }
  168.  
  169. Kniga &operator+=(int x){
  170. godina+=x;
  171. return *this;
  172. }
  173.  
  174. static void setEvro(float novKurs){
  175. evro = novKurs;
  176. }
  177.  
  178. ~Kniga(){
  179. delete []ime;
  180. delete []avtor;
  181. }
  182.  
  183. };
  184. float Kniga::evro=61.5;
  185.  
  186. class Biblioteka{
  187. private:
  188. char ime[25];
  189. int kapacitet;
  190. Kniga *niza;
  191. int brojKnigi;
  192. public:
  193. Biblioteka(){
  194. niza=new Kniga[0];
  195. brojKnigi=0;
  196. }
  197.  
  198. Biblioteka(char ime[],int kapacitet){
  199. niza=new Kniga[0];
  200. brojKnigi=0;
  201. strcpy(this->ime,ime);
  202. this->kapacitet=kapacitet;
  203. }
  204.  
  205. void print(){
  206. cout<<"Biblioteka: "<<ime<<" "<<kapacitet<<endl;
  207. cout<<"Knigi vo bibliotekata:"<<endl;
  208. for(int i=0;i<brojKnigi;i++){
  209. cout<<niza[i];
  210. }
  211. }
  212.  
  213. Biblioteka &operator+=(Kniga &novaKniga){
  214. if(brojKnigi==kapacitet){
  215. throw MaxCapacity(kapacitet);
  216. }
  217. int flag=0;
  218. for(int i=0;i<brojKnigi;i++){
  219. if(niza[i]==novaKniga){
  220. flag=1;
  221. throw SameBookException("Knigata veke ja imame vo nizata");
  222. break;
  223. }
  224. }
  225. if(flag==0){
  226. // si gi zacuvuvame site dosegashni knigi
  227. Kniga *tempNiza=new Kniga[brojKnigi];
  228. for(int i=0;i<brojKnigi;i++){
  229. tempNiza[i]=niza[i];
  230. }
  231. // prosiruvame nizata za +1 mesto
  232. niza = new Kniga[brojKnigi+1];
  233. // si gi vrakjame site knigi nazad vo nizata
  234. for(int i=0;i<brojKnigi;i++){
  235. niza[i]=tempNiza[i];
  236. }
  237. // ne ni treba veke tempNiza
  238. delete []tempNiza;
  239. // na posledno mesto dodavame novata kniga
  240. niza[brojKnigi]=novaKniga;
  241. // pokacuvame vkupniot broj na knigi vo nizata za +1
  242. brojKnigi++;
  243. }
  244. return *this;
  245. }
  246.  
  247. Biblioteka &operator-=(Kniga &novaKniga){
  248. int flag=0;
  249. for(int i=0;i<brojKnigi;i++){
  250. if(niza[i]==novaKniga){
  251. flag=1;
  252. break;
  253. }
  254. }
  255. if(flag==1){
  256. int brojac=0;
  257. Kniga *tempNiza=new Kniga[brojKnigi];
  258. for(int i=0;i<brojKnigi;i++){
  259. if(niza[i]==novaKniga){
  260. // go ripnuvame toj objekt t.e. ne go dodavame vo tempNiza
  261. }else{
  262. tempNiza[brojac]=niza[i];
  263. brojac++;
  264. }
  265. }
  266. niza=new Kniga[brojKnigi-1];
  267. brojKnigi--;
  268. for(int i=0;i<brojKnigi;i++){
  269. niza[i]=tempNiza[i];
  270. }
  271. delete []tempNiza;
  272. }
  273. return *this;
  274. }
  275.  
  276. ~Biblioteka(){
  277. delete []niza;
  278. }
  279. };
  280.  
  281. int main(){
  282. cout<<"Testing parameter constructor"<<endl;
  283. Kniga lotr("The lord of the rings", 111222, 1000, 1955, "R.R. Tolkien");
  284. lotr.print();
  285. Kniga hp("Harry Potter", 222333, 1500);
  286. //hp.print();
  287. hp.setGodina(1991);
  288. hp.setAvtor("J.K. Rowling");
  289. hp.print();
  290. cout<<"Testing copy constructor"<<endl;
  291. Kniga hp2(hp);
  292. hp2.print();
  293. cout<<"Testing operator="<<endl;
  294. Kniga hp3;
  295. hp3=hp;
  296. hp3.print();
  297.  
  298. cout<<"Testing operator <<"<<endl;
  299. cout<<hp3;
  300.  
  301. cout<<"Testing operator >>"<<endl;
  302. //Kniga got;
  303. //cin>>got;
  304. Kniga got("GoT",333444,1750,1993,"R.R.Martin");
  305. cout<<got;
  306.  
  307. cout<<"Testing operator <"<<endl;
  308. if(hp<got){
  309. cout<<"HP<GOT"<<endl;
  310. }else{
  311. cout<<"HP>GOT"<<endl;
  312. }
  313.  
  314. cout<<"Testing operator =="<<endl;
  315. if(hp==got){
  316. cout<<"HP==GOT"<<endl;
  317. }else{
  318. cout<<"HP!=GOT"<<endl;
  319. }
  320.  
  321. cout<<"Testing operator +="<<endl;
  322. cout<<hp;
  323. hp+=3;
  324. cout<<hp;
  325.  
  326.  
  327.  
  328. cout<<"Testing static"<<endl;
  329. cout<<hp.cenaVoEvra()<<endl;
  330. Kniga::setEvro(62);
  331. cout<<hp.cenaVoEvra()<<endl;
  332.  
  333.  
  334.  
  335. Biblioteka bm("Brakja Miladinovci", 4);
  336. bm.print();
  337.  
  338. cout<<"Testirame += operator"<<endl;
  339. try{
  340. bm+=hp;
  341. bm+=got;
  342. bm+=lotr;
  343. bm+=lotr;
  344. }catch(SameBookException &objekt){
  345. objekt.print();
  346. }catch(MaxCapacity &objekt){
  347. objekt.print();
  348. }
  349. bm.print();
  350.  
  351. cout<<"Testirame -= operator"<<endl;
  352. bm-=hp;
  353. bm.print();
  354. return 0;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement