borisdexter

Испитна задача 1

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