Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *** TEKST ***
- Направи класа за Книга за која ќе се чуваат следните податоци: име (динамички алоцирана променлива), код (цел број), цена (децимален број), година на издавање (цел број), курс на евро (децимален број кој има почетна вредност од 61.5 и може да се менува во текот на програмата) и автор (динамички алоцирана променлива). За секоја книга напиши ги потребните конструктори и методи и преоптовари ги следните оператори:
- - << принтање на една книга во формат [Книга: име код цена година автор]
- - >> внесување информации за една книга
- - < споредување на две книги според годината на издавање
- - == споредување на две книги според кодот
- - += додади години на годината на издавање
- - = тука мислам не треба објаснување
- - cenaVoEvra() ја враќа цената на една книга пресметана во евра
- Направи класа Библиотека во која ќе се чува името на библиотеката (карактерна низа), капацитет на книги во библиотеката (цел број), динамички алоцирана низа од класата Книга и бројот на книги во библиотеката (цел број). За класата преоптовари ги следните оператори:
- - += додавање книга во библиотеката
- - -= бришење книга од библиотеката
- - postariKnigi(int godina) печати ги сите книги постари од таа година
- - kolkuKnigiOdAvtor(char *nekojAvtor) колку книги се издадени од даден автор
- - vkupnaCenaVoEvra() вкупната цена на сите книги во евра
- - daliSiteKnigiPosle(int godina) принтаме дали сите книги се издадени после таа година или не
- - prosecnaCenaNaKniga() просечната цена на книга во денари
- Доколку проба да се додаде книга што веќе ја имаме во библиотеката да се фрли исклучок од класата SameBookException. Доколку пробуваме да го надминеме капацитетот на книги во библиотеката да се фрли исклучок од класата MaxCapacity.
- *** RESHENIE ***
- #include<iostream>
- using namespace std;
- class MaxCapacity{
- private:
- int kapacitet;
- public:
- MaxCapacity(){}
- MaxCapacity(int x){
- kapacitet=x;
- }
- void print(){
- cout<<"Maksimalniot kapacitet e "<<kapacitet<<endl;
- }
- };
- class SameBookException{
- private:
- char error[25];
- public:
- SameBookException(){}
- SameBookException(char error[]){
- strcpy(this->error,error);
- }
- void print(){
- cout<<error<<endl;
- }
- };
- class Kniga{
- private:
- char *ime;
- int kod;
- float cena;
- static float evro;
- int godina;
- char *avtor;
- public:
- Kniga(){
- // delete []ime;
- // delete []avtor;
- this->ime=new char[0];
- this->avtor=new char[0];
- }
- // constructor so 5 parametri
- Kniga(char *ime,int kod,float cena,int godina,char *avtor){
- this->ime=new char[strlen(ime)+1];
- strcpy(this->ime,ime);
- this->avtor=new char[strlen(avtor)+1];
- strcpy(this->avtor,avtor);
- this->kod=kod;
- this->cena=cena;
- this->godina=godina;
- }
- // constructor so 3 parametri
- Kniga(char *ime,int kod,float cena){
- this->ime=new char[strlen(ime)+1];
- strcpy(this->ime,ime);
- this->kod=kod;
- this->cena=cena;
- // delete []avtor
- // dinamicki resetirame, godina moze i ne mora
- this->avtor=new char[0];
- godina=0;
- }
- // copy constructor
- Kniga(const Kniga &novaKniga){
- this->ime=new char[strlen(novaKniga.ime)+1];
- strcpy(this->ime,novaKniga.ime);
- this->avtor=new char[strlen(novaKniga.avtor)+1];
- strcpy(this->avtor,novaKniga.avtor);
- this->kod=novaKniga.kod;
- this->cena=novaKniga.cena;
- this->godina=novaKniga.godina;
- }
- // operator =
- Kniga &operator=(Kniga &novaKniga){
- // sporeduvame dvata objekti spored aresata
- if(this != &novaKniga){
- this->ime=new char[strlen(novaKniga.ime)+1];
- strcpy(this->ime,novaKniga.ime);
- this->avtor=new char[strlen(novaKniga.avtor)+1];
- strcpy(this->avtor,novaKniga.avtor);
- this->kod=novaKniga.kod;
- this->cena=novaKniga.cena;
- this->godina=novaKniga.godina;
- }
- return *this;
- }
- int getGodina(){
- return godina;
- }
- float getCena(){
- return cena;
- }
- // setAvtor
- void setAvtor(char *novAvtor){
- avtor=new char[strlen(novAvtor)+1];
- strcpy(avtor,novAvtor);
- }
- void setGodina(int novaGodina){
- godina=novaGodina;
- }
- // output operator
- friend ostream &operator<<(ostream &output,Kniga &objekt){
- output<<"Kniga: "<<objekt.ime<<" "<<objekt.kod<<" "<<objekt.cena<<" "<<objekt.godina<<" "<<objekt.avtor<<endl;
- return output;
- }
- // input operator
- friend istream &operator>>(istream &i, Kniga &objekt){
- i>>objekt.ime>>objekt.kod>>objekt.cena>>objekt.godina>>objekt.avtor;
- return i;
- }
- bool operator<(Kniga &posleOperator){
- if(this->godina<posleOperator.godina){
- return true;
- }else{
- return false;
- }
- //return this->godina<posleOperator.godina;
- }
- bool operator==(Kniga &drugaKniga){
- // 2 nacini
- /*
- if(this->kod==drugaKniga.kod){
- return true;
- }else{
- return false;
- }
- */
- return this->kod==drugaKniga.kod;
- }
- void print(){
- cout<<"Kniga: "<<ime<<" "<<kod<<" "<<cena<<" "<<godina<<" "<<avtor<<endl;
- }
- float cenaVoEvra(){
- return cena/evro;
- }
- Kniga &operator+=(int x){
- godina+=x;
- return *this;
- }
- static void setEvro(float novKurs){
- evro = novKurs;
- }
- char* getAvtor(){
- return avtor;
- }
- ~Kniga(){
- delete []ime;
- delete []avtor;
- }
- };
- float Kniga::evro=61.5;
- class Biblioteka{
- private:
- char ime[25];
- int kapacitet;
- Kniga *niza;
- int brojKnigi;
- public:
- Biblioteka(){
- niza=new Kniga[0];
- brojKnigi=0;
- }
- Biblioteka(char ime[],int kapacitet){
- niza=new Kniga[0];
- brojKnigi=0;
- strcpy(this->ime,ime);
- this->kapacitet=kapacitet;
- }
- void print(){
- cout<<"Biblioteka: "<<ime<<" "<<kapacitet<<endl;
- cout<<"Knigi vo bibliotekata:"<<endl;
- for(int i=0;i<brojKnigi;i++){
- cout<<niza[i];
- }
- }
- Biblioteka &operator+=(Kniga &novaKniga){
- if(brojKnigi==kapacitet){
- throw MaxCapacity(kapacitet);
- }
- int flag=0;
- for(int i=0;i<brojKnigi;i++){
- if(niza[i]==novaKniga){
- flag=1;
- throw SameBookException("Knigata veke ja imame vo nizata");
- break;
- }
- }
- if(flag==0){
- // si gi zacuvuvame site dosegashni knigi
- Kniga *tempNiza=new Kniga[brojKnigi];
- for(int i=0;i<brojKnigi;i++){
- tempNiza[i]=niza[i];
- }
- // prosiruvame nizata za +1 mesto
- niza = new Kniga[brojKnigi+1];
- // si gi vrakjame site knigi nazad vo nizata
- for(int i=0;i<brojKnigi;i++){
- niza[i]=tempNiza[i];
- }
- // ne ni treba veke tempNiza
- delete []tempNiza;
- // na posledno mesto dodavame novata kniga
- niza[brojKnigi]=novaKniga;
- // pokacuvame vkupniot broj na knigi vo nizata za +1
- brojKnigi++;
- }
- return *this;
- }
- Biblioteka &operator-=(Kniga &novaKniga){
- int flag=0;
- for(int i=0;i<brojKnigi;i++){
- if(niza[i]==novaKniga){
- flag=1;
- break;
- }
- }
- if(flag==1){
- int brojac=0;
- Kniga *tempNiza=new Kniga[brojKnigi];
- for(int i=0;i<brojKnigi;i++){
- if(niza[i]==novaKniga){
- // go ripnuvame toj objekt t.e. ne go dodavame vo tempNiza
- }else{
- tempNiza[brojac]=niza[i];
- brojac++;
- }
- }
- niza=new Kniga[brojKnigi-1];
- brojKnigi--;
- for(int i=0;i<brojKnigi;i++){
- niza[i]=tempNiza[i];
- }
- delete []tempNiza;
- }
- return *this;
- }
- void postariKnigi(int nekojaGodina){
- for(int i=0;i<brojKnigi;i++){
- if(niza[i].getGodina()<nekojaGodina){
- cout<<niza[i];
- }
- }
- }
- int kolkuKnigiOdAvtor(char *nekojAvtor){
- int brojac=0;
- for(int i=0;i<brojKnigi;i++){
- if(strcmp(niza[i].getAvtor(),nekojAvtor)==0){
- brojac++;
- }
- }
- return brojac;
- }
- float vkupnaCenaVoEvra(){
- float suma=0;
- for(int i=0;i<brojKnigi;i++){
- suma+=niza[i].cenaVoEvra();
- }
- return suma;
- }
- float prosecnaCenaNaKniga(){
- float suma=0;
- for(int i=0;i<brojKnigi;i++){
- suma+=niza[i].getCena();
- }
- return suma/brojKnigi;
- }
- bool daliSiteKnigiPosle(int nekojaGodina){
- for(int i=0;i<brojKnigi;i++){
- if(niza[i].getGodina()<nekojaGodina){
- return 0;
- }
- }
- return 1;
- }
- ~Biblioteka(){
- delete []niza;
- }
- };
- int main(){
- cout<<"Testing parameter constructor"<<endl;
- Kniga lotr("The lord of the rings", 111222, 1000, 1955, "R.R. Tolkien");
- lotr.print();
- Kniga hp("Harry Potter", 222333, 1500);
- //hp.print();
- hp.setGodina(1991);
- hp.setAvtor("J.K. Rowling");
- hp.print();
- cout<<"Testing copy constructor"<<endl;
- Kniga hp2(hp);
- hp2.print();
- cout<<"Testing operator="<<endl;
- Kniga hp3;
- hp3=hp;
- hp3.print();
- cout<<"Testing operator <<"<<endl;
- cout<<hp3;
- cout<<"Testing operator >>"<<endl;
- //Kniga got;
- //cin>>got;
- Kniga got("GoT",333444,1750,1993,"R.R.Martin");
- cout<<got;
- cout<<"Testing operator <"<<endl;
- if(hp<got){
- cout<<"HP<GOT"<<endl;
- }else{
- cout<<"HP>GOT"<<endl;
- }
- cout<<"Testing operator =="<<endl;
- if(hp==got){
- cout<<"HP==GOT"<<endl;
- }else{
- cout<<"HP!=GOT"<<endl;
- }
- cout<<"Testing operator +="<<endl;
- cout<<hp;
- hp+=3;
- cout<<hp;
- cout<<"Testing static"<<endl;
- cout<<hp.cenaVoEvra()<<endl;
- Kniga::setEvro(62);
- cout<<hp.cenaVoEvra()<<endl;
- Biblioteka bm("Brakja Miladinovci", 4);
- bm.print();
- cout<<"Testirame += operator"<<endl;
- try{
- bm+=hp;
- bm+=got;
- bm+=lotr;
- bm+=lotr;
- }catch(SameBookException &objekt){
- objekt.print();
- }catch(MaxCapacity &objekt){
- objekt.print();
- }
- bm.print();
- cout<<"Testirame -= operator"<<endl;
- bm-=hp;
- bm.print();
- bm.postariKnigi(1950);
- cout<<"Od avtorot R.R.Martin se izdadeni "<<bm.kolkuKnigiOdAvtor("R.R.Martin")<<" knigi"<<endl;
- cout<<"Vkupnata cena za site knigi vo bibliotekata vo evra e "<<bm.vkupnaCenaVoEvra()<<endl;
- cout<<"Prosecnata cena na edna kniga e "<<bm.prosecnaCenaNaKniga()<<endl;
- if(bm.daliSiteKnigiPosle(1990)){
- cout<<"Site knigi se izdadeni posle "<<1990<<"-ta godina"<<endl;
- }else{
- cout<<"Ne se site knigi izdadeni posle "<<1990<<"-ta godina"<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment