Advertisement
MikiStrail

ООП Лаб 9

May 13th, 2019
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.24 KB | None | 0 0
  1. 1. Камион
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. class ImaMasa
  7. {
  8. public:
  9.     virtual double vratiMasa()=0;
  10.     virtual void pecati()=0;
  11.     virtual ~ImaMasa() {}
  12. };
  13.  
  14. class PaketPijalok
  15. {
  16. protected:
  17.     static double gustina;
  18.     static double masaNaAmbalaza;
  19.     double volumenEden;
  20.     int kolicina;
  21. public:
  22.     PaketPijalok(const double volumenEden=0, const int kolicina=0)
  23.     {
  24.         this->volumenEden=volumenEden;
  25.         this->kolicina=kolicina;
  26.     }
  27.     ~PaketPijalok() {}
  28.     double vratiMasa()
  29.     {
  30.         return (volumenEden*gustina+masaNaAmbalaza)*kolicina;        
  31.     }
  32.     int getKolicina() { return kolicina; }
  33.     double getVolumen() { return volumenEden; }
  34.     double getGustina() { return gustina; }
  35. };
  36. double PaketPijalok::gustina=0.8;
  37. double PaketPijalok::masaNaAmbalaza=0.2;
  38.  
  39. class PaketSok : public PaketPijalok, public ImaMasa
  40. {
  41. private:
  42.     bool daliGaziran;
  43. public:
  44.     PaketSok(const double volumenEden=0, const int kolicina=0, const bool daliGaziran=0) : PaketPijalok(volumenEden, kolicina)
  45.     {
  46.         this->daliGaziran=daliGaziran;        
  47.     }
  48.     ~PaketSok() {}
  49.     double vratiMasa()
  50.     {
  51.         if(daliGaziran)
  52.         {
  53.             return PaketPijalok::vratiMasa();
  54.         }
  55.         else
  56.         {
  57.             return PaketPijalok::vratiMasa()+0.1*kolicina;
  58.         }
  59.     }
  60.     void pecati()
  61.     {
  62.         cout<<"Paket sok"<<endl;
  63.         cout<<"kolicina "<<kolicina<<", so po "<<getVolumen()*getGustina()<<" l(dm3)"<<endl;
  64.     }
  65. };
  66.  
  67. class PaketVino : public PaketPijalok, public ImaMasa
  68. {
  69. private:
  70.     double procentAlkohol;
  71. public:
  72.     PaketVino(const double volumenEden=0, const int kolicina=0, const double procentAlkohol=0) : PaketPijalok(volumenEden, kolicina)
  73.     {
  74.         this->procentAlkohol=procentAlkohol;        
  75.     }
  76.     ~PaketVino() {}
  77.     double vratiMasa()
  78.     {
  79.         return PaketPijalok::vratiMasa()*(0.9+procentAlkohol);
  80.     }
  81.     double getProcentAlkohol()
  82.     {
  83.         return procentAlkohol;
  84.     }
  85.     void pecati()
  86.     {
  87.         cout<<"Paket vino"<<endl;
  88.         cout<<"kolicina "<<kolicina<<", "<<procentAlkohol*100<<"% alkohol od po "<<getVolumen()*getGustina()<<" l(dm3)"<<endl;
  89.     }
  90. };
  91.  
  92. class Kamion
  93. {
  94. private:
  95.     char *registracija;
  96.     char *vozac;
  97.     ImaMasa **p;
  98.     int noP;    
  99. public:
  100.     Kamion(const char *vozac="")
  101.     {
  102.         this->vozac=new char[strlen(vozac)+1];
  103.         strcpy(this->vozac, vozac);
  104.         noP=0;
  105.         if(!(isalpha(registracija[0]) && isalpha(registracija[1]) && isalpha(registracija[6]) && isalpha(registracija[7])))
  106.         {  
  107.             throw 0;
  108.         }
  109.     }
  110.     Kamion(const char *registracija="", const char *vozac="")
  111.     {
  112.         this->registracija=new char[strlen(registracija)+1];
  113.         strcpy(this->registracija, registracija);
  114.         this->vozac=new char[strlen(vozac)+1];
  115.         strcpy(this->vozac, vozac);
  116.         noP=0;
  117.         if(!(isalpha(registracija[0]) && isalpha(registracija[1]) && isalpha(registracija[6]) && isalpha(registracija[7])))
  118.         {  
  119.             throw 0;
  120.         }
  121.     }
  122.     ~Kamion ()
  123.     {
  124.         delete [] registracija;
  125.         delete [] vozac;
  126.         for(int i=0; i<noP; i++)
  127.         {
  128.             delete p[i];
  129.         }
  130.         delete [] p;
  131.     }
  132.     void registriraj(char *registracija)
  133.     {
  134.         registracija=new char[strlen(registracija)+1];
  135.         strcpy(this->registracija, registracija);
  136.     }
  137.     void dodadiElement(ImaMasa *n)
  138.     {
  139.         ImaMasa **temp=new ImaMasa*[noP+1];
  140.         for(int i=0; i<noP; i++)
  141.         {
  142.             temp[i]=p[i];
  143.         }
  144.         temp[noP++]=n;
  145.         p=new ImaMasa*[noP];
  146.         for(int i=0; i<noP; i++)
  147.         {
  148.             p[i]=temp[i];
  149.         }
  150.     }
  151.     double vratiVkupnaMasa()
  152.     {
  153.         double total=0;
  154.         for(int i=0; i<noP; i++)
  155.         {
  156.             total+=p[i]->vratiMasa();
  157.         }
  158.         return total;
  159.     }
  160.     void pecati()
  161.     {
  162.         cout<<"Kamion so registracija "<<registracija<<" i vozac "<<vozac<<" prenesuva: "<<endl;
  163.         for(int i=0; i<noP; i++)
  164.         {
  165.             p[i]->pecati();
  166.         }
  167.     }
  168.     Kamion pretovar(char *registracija, char *vozac)
  169.     {
  170.         Kamion newTruck(registracija, vozac);
  171.         double max=p[0]->vratiMasa();
  172.         int index=0;
  173.         for(int i=1; i<noP; i++)
  174.         {
  175.             if(p[i]->vratiMasa()>max)
  176.             {
  177.                 max=p[i]->vratiMasa();
  178.                 index=i;
  179.             }
  180.         }
  181.         for(int i=0; i<noP; i++)
  182.         {
  183.             if(i!=index)
  184.             {
  185.                 newTruck.dodadiElement(p[i]);
  186.             }
  187.         }
  188.         return newTruck;
  189.     }
  190. };
  191.  
  192. int main()
  193. {
  194.     char ime[20], reg[9];
  195.     double vol;
  196.     int kol;
  197.     bool g;
  198.     double proc;
  199.     try
  200.     {
  201.         cin>>reg;
  202.         cin>>ime;
  203.         Kamion A(reg, ime);
  204.         ImaMasa **d = new ImaMasa*[5];
  205.         cin>>vol>>kol;
  206.         cin>>g;
  207.         d[0] = new PaketSok(vol, kol, g);
  208.         cin>>vol>>kol;
  209.         cin>>proc;
  210.         d[1] = new PaketVino(vol, kol, proc);
  211.         cin>>vol>>kol;
  212.         cin>>proc;
  213.         d[2] = new PaketVino(vol, kol, proc);
  214.         cin>>vol>>kol;
  215.         cin>>g;
  216.         d[3] = new PaketSok(vol, kol, g);
  217.         cin>>vol>>kol;
  218.         cin>>proc;
  219.         d[4] = new PaketVino(vol, kol, proc);
  220.         A.dodadiElement(d[0]);
  221.         A.dodadiElement(d[1]);
  222.         A.dodadiElement(d[2]);
  223.         A.dodadiElement(d[3]);
  224.         A.dodadiElement(d[4]);
  225.         A.pecati();
  226.         cout<<"Vkupna masa: "<<A.vratiVkupnaMasa()<<endl;
  227.         cin>>reg;
  228.         cin>>ime;
  229.         Kamion B = A.pretovar(reg, ime);
  230.         B.pecati();
  231.         cout<<"Vkupna masa: "<<B.vratiVkupnaMasa()<<endl;
  232.     }
  233.     catch(int)
  234.     {
  235.         cout<<"Pogresno vnesena registracija"<<endl;
  236.     }
  237. }
  238.  
  239. 2. SocialNetwork
  240. #include <iostream>
  241. #include <cstring>
  242.  
  243. using namespace std;
  244.  
  245. bool validPassword(const char * pw) {
  246.     bool uCL = false;
  247.     bool lCL = false;
  248.     bool num = false;
  249.     for(int i = 0; i<strlen(pw); i++) {
  250.         if(isupper(pw[i]))
  251.             uCL=true;
  252.         if(islower(pw[i]))
  253.             lCL=true;
  254.         if(isdigit(pw[i]))
  255.             num=true;
  256.  
  257.         if(uCL&&lCL&&num)
  258.             return true;
  259.     }
  260.     return false;
  261. }
  262.  
  263. bool validEmail(const char * email) {
  264.   //  cout<<"CE: "<<email<<" - ";
  265.     bool valid = false;
  266.     int atCnt =0;
  267.     for(int i = 0; i<strlen(email); i++) {
  268.         if(email[i]=='@'){
  269.             atCnt++;
  270.         }
  271.     }
  272.     if(atCnt==1){
  273.         return true;
  274.        
  275.     }
  276.     return false;
  277. }
  278.  
  279.  
  280.  
  281. class InvalidPassword {
  282.     char msg[50];
  283. public:
  284.     InvalidPassword(const char * msg) {
  285.         strcpy(this->msg,msg);
  286.     }
  287.     void message(){cout<<msg<<endl;}
  288. };
  289.  
  290.  
  291.  
  292. class InvalidEmail {
  293.     char msg[50];
  294. public:
  295.     InvalidEmail(const char * msg) {
  296.         strcpy(this->msg,msg);
  297.     }
  298.     void message(){cout<<msg<<endl;}
  299.  
  300. };
  301.  
  302.  
  303. class MaximumSizeLimit {
  304.     int n;
  305. public:
  306.     MaximumSizeLimit(int n) {
  307.         this->n=n;
  308.     }
  309.     void message(){cout<<"You can't add more than "<<n<<" users."<<endl;}
  310. };
  311.  
  312.  
  313. class User {
  314. protected:
  315.     char username[50];
  316.     char password[50];
  317.     char email[50];
  318.  
  319. public:
  320.     User(const char * username = "", const char * password ="", const char * email="") {
  321.         strcpy(this->username,username);
  322.         strcpy(this->password,password);
  323.         strcpy(this->email,email);
  324.     }
  325.     virtual double popularity()=0;
  326.        char * getEmail(){return email;}
  327.     char * getPassword(){return password;}
  328. };
  329.  
  330. class FacebookUser : public User {
  331. protected:
  332.     int NumOfFriends;
  333.     int NumOfLikes;
  334.     int NumOfComments;
  335.     static double COEFF_LIKES;
  336.     static double COEFF_COMMENTS;
  337. public:
  338.     FacebookUser(const char * username = "", const char * password ="", const char * email="", int NumOfFriends = 0, int NumOfLikes =0, int NumOfComments=0 ):
  339.         User(username,password,email) {
  340.         this->NumOfFriends=NumOfFriends;
  341.         this->NumOfLikes=NumOfLikes;
  342.         this->NumOfComments=NumOfComments;
  343.     }
  344.     double popularity() {
  345.         return COEFF_LIKES*NumOfLikes+COEFF_COMMENTS*NumOfComments+NumOfFriends;
  346.     }
  347. };
  348.  
  349. double FacebookUser::COEFF_LIKES=0.1;
  350. double FacebookUser::COEFF_COMMENTS=0.5;
  351.  
  352.  
  353. class TwitterUser: public User {
  354. protected:
  355.     int NumOfFollowers;
  356.     int NumOfTweets;
  357.     static double COEFF_TWEETS;
  358. public:
  359.     TwitterUser(const char * username = "", const char * password ="", const char * email="", int NumOfFollowers = 0, int NumOfTweets =0 ):
  360.         User(username,password,email) {
  361.         this->NumOfFollowers=NumOfFollowers;
  362.         this->NumOfTweets=NumOfTweets;
  363.     }
  364.     double popularity() {
  365.         return NumOfTweets*COEFF_TWEETS+NumOfFollowers;
  366.     }
  367.  
  368.  
  369. };
  370.  
  371. double TwitterUser::COEFF_TWEETS=0.5;
  372.  
  373.  
  374. class SocialNetwork {
  375. protected:
  376.     User** niza;
  377.     int totalUsers;
  378.     static int MAX_USERS;
  379. public:
  380.     SocialNetwork() {
  381.         niza=NULL;
  382.         totalUsers=0;
  383.     }
  384.     SocialNetwork(SocialNetwork *s) {
  385.         niza = new User*[s->totalUsers];
  386.         for(int i =0; i<s->totalUsers; i++) {
  387.             niza[i]=s->niza[i];
  388.         }
  389.         totalUsers=s->totalUsers;
  390.     }
  391.     SocialNetwork &operator=(SocialNetwork &s) {
  392.         niza = new User*[s.totalUsers];
  393.         for(int i =0; i<totalUsers; i++) {
  394.             niza[i]=s.niza[i];
  395.         }
  396.         totalUsers=s.totalUsers;
  397.         return *this;
  398.     }
  399.  
  400.     SocialNetwork&operator+=(User *u) {
  401.         User ** tmp = new User*[totalUsers+1];
  402.         for(int i =0; i<totalUsers; i++) {
  403.             tmp[i]=niza[i];
  404.         }
  405.         tmp[totalUsers++]=u;
  406.         delete [] niza;
  407.         niza=tmp;
  408.         return *this;
  409.     }
  410.     double avgPopularity() {
  411.         double pop_calc =0;
  412.         for(int i =0; i<totalUsers; i++) {
  413.             pop_calc+=niza[i]->popularity();
  414.         }
  415.         return pop_calc/totalUsers;
  416.     }
  417.  
  418.     void changeMaximumSize(int number) {
  419.         MAX_USERS=number;
  420.     }
  421.     static int getMAX_USERS() {
  422.         return MAX_USERS;
  423.     }
  424.     int getTotalUsers(){return totalUsers;}
  425. };
  426.  
  427. int SocialNetwork::MAX_USERS=5;
  428.  
  429.  
  430.  
  431. int main() {
  432.  
  433.     SocialNetwork network = SocialNetwork();
  434.     int n;
  435.     cin >> n;
  436.     char username[50];
  437.     char password[50];
  438.     char email[50];
  439.     int userType;
  440.     for (int i=0; i < n; ++i) {
  441.         cin >> username;
  442.         cin >> password;
  443.         cin >> email;
  444.         cin >> userType;
  445.        // cout<<"MAIL:"<<email<<endl;
  446.         if (userType == 1) {
  447.             int friends;
  448.             int likes;
  449.             int comments;
  450.             cin >> friends >> likes >> comments;
  451.  
  452.             // TODO: Try-catch
  453.             try{
  454.             User * u = new FacebookUser(username,password,email,friends,likes,comments);
  455.             if(!validPassword(u->getPassword()))
  456.                 throw InvalidPassword("Password too weak.");
  457.             else if(!validEmail(u->getEmail()))
  458.                 throw InvalidEmail("Mail is not valid.");
  459.             if(SocialNetwork::getMAX_USERS()==network.getTotalUsers())
  460.                 throw MaximumSizeLimit(SocialNetwork::getMAX_USERS());
  461.             network += u;
  462.             }
  463.            
  464.             catch (InvalidPassword ip){
  465.                 ip.message();
  466.             }
  467.             catch (InvalidEmail ie){
  468.                 ie.message();
  469.             }
  470.                         catch (MaximumSizeLimit max){
  471.                 max.message();
  472.             }
  473.         }
  474.  
  475.         else {
  476.             int followers;
  477.             int tweets;
  478.             cin >> followers >> tweets;
  479.  
  480.             // TODO: Try-catch
  481.             try{
  482.             User * u= new TwitterUser(username,password,email,followers,tweets);
  483.             if(!validPassword(u->getPassword()))
  484.                 throw InvalidPassword("Password is too weak.");
  485.             else if(!validEmail(u->getEmail()))
  486.                 throw InvalidEmail("Mail is not valid.");
  487.             if(SocialNetwork::getMAX_USERS()==network.getTotalUsers())
  488.                 throw MaximumSizeLimit(SocialNetwork::getMAX_USERS());
  489.             network += u;
  490.             }
  491.             catch (InvalidPassword ip){
  492.                 ip.message();
  493.             }
  494.             catch (InvalidEmail ie){
  495.                 ie.message();
  496.             }
  497.                         catch (MaximumSizeLimit max){
  498.                 max.message();
  499.             }
  500.         }
  501.     }
  502.     network.changeMaximumSize(6);
  503.     cin >> username;
  504.     cin >> password;
  505.     cin >> email;
  506.     int followers;
  507.     int tweets;
  508.     cin >> followers >> tweets;
  509.  
  510.     // TODO: Try-catch
  511.             try{
  512.             User * u= new TwitterUser(username,password,email,followers,tweets);
  513.             if(!validPassword(u->getPassword()))
  514.                 throw InvalidPassword("Password is too weak.");
  515.             else if(!validEmail(u->getEmail()))
  516.                 throw InvalidEmail("Mail is not valid.");
  517.             if(SocialNetwork::getMAX_USERS()==network.getTotalUsers())
  518.                 throw MaximumSizeLimit(SocialNetwork::getMAX_USERS());
  519.             network += u;
  520.             }
  521.             catch (InvalidPassword ip){
  522.                 ip.message();
  523.             }
  524.             catch (InvalidEmail ie){
  525.                 ie.message();
  526.             }
  527.             catch (MaximumSizeLimit max){
  528.                 max.message();
  529.             }
  530.  
  531.     cout << network.avgPopularity();
  532.  
  533. }
  534.  
  535. 3. Трансакции
  536.  
  537. #include<iostream>
  538. #include<cstring>
  539. using namespace std;
  540. class InvalidDateException {
  541. private:
  542.     int den;
  543.     int mesec;
  544.     int godina;
  545. public:
  546.     InvalidDateException(int den,int mesec,int godina) {
  547.         this->den=den;
  548.         this->mesec=mesec;
  549.         this->godina=godina;
  550.     }
  551.     void print() {
  552.         cout<<"Invalid Date "<<den<<"/"<<mesec<<"/"<<godina<<endl;
  553.     }
  554. };
  555. // GBP is not a supported currency
  556. class NotSupportedCurrencyException {
  557. private:
  558.     char valuta[3];
  559. public:
  560.     NotSupportedCurrencyException(char *valuta) {
  561.         strcpy(this->valuta,valuta);
  562.     }
  563.     void print() {
  564.         cout<<valuta<<" is not a supported currency"<<endl;
  565.     }
  566. };
  567.  
  568. class Transakcija {
  569. protected:
  570.     int den;
  571.     int mesec;
  572.     int godina;
  573.     double iznos;
  574.     static double EUR;
  575.     static double USD;
  576. public:
  577.     Transakcija() {}
  578.     Transakcija(int den,int mesec,int godina,double iznos) {
  579.         if((den<1 || den>31)||(mesec<1 || mesec>12)) {
  580.             throw InvalidDateException(den,mesec,godina);
  581.         }
  582.  
  583.         this->den=den;
  584.         this->mesec=mesec;
  585.         this->godina=godina;
  586.         this->iznos=iznos;
  587.     }
  588.     static void setEUR(double x) {
  589.         EUR=x;
  590.     }
  591.     static void setUSD(double x) {
  592.         USD=x;
  593.     }
  594.     virtual double voDenari()=0;
  595.     virtual void pecati()=0;
  596. };
  597. double Transakcija::EUR=61;
  598. double Transakcija::USD=50;
  599. class DenarskaTransakcija: public Transakcija {
  600. public:
  601.     DenarskaTransakcija() {}
  602.     DenarskaTransakcija(int den,int mesec,int godina,double iznos):Transakcija(den,mesec,godina,iznos) {
  603.     }
  604.     void pecati() {
  605.         //05/06/2018 2500 MKD
  606.         cout<<den<<"/"<<mesec<<"/"<<godina<<" "<<iznos<<" MKD"<<endl;
  607.     }
  608.     double voDenari() {
  609.         return iznos;
  610.     }
  611.     ~DenarskaTransakcija() {}
  612. };
  613. class DeviznaTransakcija: public Transakcija {
  614. private:
  615.     char valuta[3];
  616. public:
  617.     DeviznaTransakcija() {}
  618.     DeviznaTransakcija(int den,int mesec,int godina,double iznos,char *valuta):Transakcija(den,mesec,godina,iznos) {
  619.         if((strcmp(valuta,"EUR")!=0)&&(strcmp(valuta,"USD")!=0)) {
  620.             throw NotSupportedCurrencyException(valuta);
  621.         }
  622.         strcpy(this->valuta,valuta);
  623.     }
  624.     ~DeviznaTransakcija() {}
  625.     void pecati() {
  626.         cout<<den<<"/"<<mesec<<"/"<<godina<<" "<<iznos<<" "<<valuta<<endl;
  627.     }
  628.     double voDenari() {
  629.         if(strcmp(valuta,"EUR")==0) {
  630.             return iznos*EUR;
  631.         } else if(strcmp(valuta,"USD")==0) {
  632.             return iznos*USD;
  633.         } else {
  634.             return iznos;
  635.         }
  636.     }
  637. };
  638. class Smetka {
  639. private:
  640.     Transakcija **transakcii;
  641.     int n;
  642.     char brSmetka[15];
  643.     double saldo;
  644. public:
  645.     Smetka() {
  646.     transakcii=new Transakcija*[0];
  647.     n=0;
  648.     }
  649.     Smetka(const char *brSmetka,double saldo) {
  650.         transakcii=new Transakcija*[0];
  651.         n=0;
  652.         strcpy(this->brSmetka,brSmetka);
  653.         this->saldo=saldo;
  654.     }
  655.     // Transakcija **transakcii;
  656.     // int n=5;
  657.     Smetka &operator+=(Transakcija *p) {
  658.         Transakcija **tmp=new Transakcija*[n];
  659.         for(int i=0; i<n; i++) {
  660.             tmp[i]=transakcii[i];
  661.         }
  662.         transakcii=new Transakcija*[n+1];
  663.         for(int i=0; i<n; i++) {
  664.             transakcii[i]=tmp[i];
  665.         }
  666.         transakcii[n]=p;
  667.         delete []tmp;
  668.         n++;
  669.         return *this;
  670.     }
  671.         /*
  672.     Smetka &operator-=(Transakcija &p) {
  673.         int brojac=0;
  674.         Transakcija **tmp=new Transakcija*[n];
  675.         for(int i=0; i<n; i++) {
  676.             if(transakcii[i]!=p) {
  677.                 tmp[brojac]=transakcii[i];
  678.                 brojac++;
  679.             }
  680.         }
  681.         //delete []transakcii;
  682.         transakcii=new Transakcija*[brojac];
  683.         for(int i=0; i<brojac; i++) {
  684.             transakcii[i]=tmp[i];
  685.         }
  686.         delete []tmp;
  687.         n=brojac;
  688.         return *this;
  689.     }
  690.     */
  691.     void izvestajVoDenari() {
  692.         cout<<"Korisnikot so smetka: "<<brSmetka;
  693.         double suma=0;
  694.         for(int i=0; i<n; i++) {
  695.             suma+=transakcii[i]->voDenari();
  696.         }
  697.         cout<<" ima momentalno saldo od "<<suma+saldo;
  698.         cout<<" MKD"<<endl;
  699.     }
  700.  
  701.     void pecatiTransakcii() {
  702.         for(int i=0; i<n; i++) {
  703.             transakcii[i]->pecati();
  704.         }
  705.     }
  706.     ~Smetka() {
  707.         delete []transakcii;
  708.     }
  709.     //Invalid Date 32/12/2018;
  710.  
  711. };
  712.  
  713. int main () {
  714.  
  715.     Smetka s ("300047024112789",1500);
  716.  
  717.     int n, den, mesec, godina, tip;
  718.     double iznos;
  719.     char valuta [3];
  720.  
  721.     cin>>n;
  722.     cout<<"===VNESUVANJE NA TRANSAKCIITE I SPRAVUVANJE SO ISKLUCOCI==="<<endl;
  723.     for (int i=0; i<n; i++) {
  724.         cin>>tip>>den>>mesec>>godina>>iznos;
  725.  
  726.         try {
  727.             if (tip==2) {
  728.                 cin>>valuta;
  729.                 Transakcija * t = new DeviznaTransakcija(den,mesec,godina,iznos,valuta);
  730.                 s+=t;
  731.                 //delete t;
  732.             } else {
  733.  
  734.                 Transakcija * t = new DenarskaTransakcija(den,mesec,godina,iznos);
  735.                 s+=t;
  736.                 //delete t;
  737.  
  738.             }
  739.         } catch(InvalidDateException a) {
  740.             a.print();
  741.         } catch(NotSupportedCurrencyException a) {
  742.             a.print();
  743.         }
  744.  
  745.     }
  746.     cout<<"===PECHATENJE NA SITE TRANSAKCII==="<<endl;
  747.     s.pecatiTransakcii();
  748.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  749.     s.izvestajVoDenari();
  750.     cout<<"\n===PROMENA NA KURSOT NA EVROTO I DOLAROT===\n"<<endl;
  751.     double newEUR, newUSD;
  752.     cin>>newEUR>>newUSD;
  753.     Transakcija::setEUR(newEUR);
  754.     Transakcija::setUSD(newUSD);
  755.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  756.     s.izvestajVoDenari();
  757.     return 0;
  758. }
  759.  
  760. 4. Трансакции (simple version)
  761. #include <iostream>
  762. #include <cstring>
  763.  
  764. using namespace std;
  765.  
  766. class X{
  767. public:
  768.     virtual void print() const = 0;
  769.     virtual ~X() = 0;
  770. };
  771. X::~X(){}
  772.  
  773. class InvalidDateException : public X{
  774.     int d;
  775.     int m;
  776.     int y;
  777. public:
  778.     InvalidDateException(int d=0,int m=0,int y=0):d(d),m(m),y(y){}
  779.     void print() const{cout<<"Invalid Date "<<d<<"/"<<m<<"/"<<y<<endl; }
  780. };
  781. class NotSupportedCurrencyException : public X{
  782.     char dev[3];
  783. public:
  784.     NotSupportedCurrencyException(char *dev)
  785.     {
  786.         strcpy(this->dev,dev);
  787.     }
  788.     void print() const{ cout<<dev<<" is not a supported currency"<<endl; }
  789. };
  790. class Transakcija{
  791. protected:
  792.     int den;
  793.     int mesec;
  794.     int godina;
  795.     double iznos;
  796.     static double EUR;
  797.     static double USD;
  798. public:
  799.     Transakcija(){}
  800.     Transakcija(int den,int mesec,int godina,double iznos)
  801.     :den(den),mesec(mesec),godina(godina),iznos(iznos) {}
  802.  
  803.     virtual double voDenari() = 0;
  804.     virtual void pecati() = 0;
  805.     virtual ~Transakcija() = 0;
  806.  
  807.     static void setEUR(double e) { EUR = e;}
  808.     static void setUSD(double u) { USD = u;}
  809. };
  810. Transakcija::~Transakcija(){}
  811. double Transakcija::EUR = 61;
  812. double Transakcija::USD = 50;
  813.  
  814. class DenarskaTransakcija : public Transakcija{
  815. public:
  816.     DenarskaTransakcija(int den,int mesec,int godina,double iznos):Transakcija(den,mesec,godina,iznos){}
  817.     double voDenari() { return iznos;}
  818.     void pecati() { cout<<den<<"/"<<mesec<<"/"<<godina<<" "<<iznos<<" MKD"<<endl; }
  819.  
  820. };
  821.  
  822. class DeviznaTransakcija : public Transakcija{
  823.     char dev[3];
  824. public:
  825.     DeviznaTransakcija(int den,int mesec,int godina,double iznos,char *dev):Transakcija(den,mesec,godina,iznos)
  826.     {
  827.         strcpy(this->dev,dev);
  828.     }
  829.     double voDenari()
  830.     {
  831.         return (!strcmp(dev,"EUR")) ? iznos*Transakcija::EUR : iznos*Transakcija::USD;
  832.     }
  833.     void pecati() { cout<<den<<"/"<<mesec<<"/"<<godina<<" "<<iznos<<" "<<dev<<endl; }
  834.  
  835. };
  836.  
  837. class Smetka{
  838.     Transakcija **izvrseni;
  839.     int br;
  840.     char brSmetka[15];
  841.     double saldo;
  842. public:
  843.     Smetka(){ izvrseni=new Transakcija* [0]; br=0;}
  844.     Smetka(char *brSmetka, double saldo):saldo(saldo)
  845.     {
  846.         strcpy(this->brSmetka,brSmetka);
  847.  
  848.         izvrseni=new Transakcija* [0];
  849.         br=0;
  850.     }
  851.     Smetka(const Smetka &s):br(s.br),saldo(s.saldo)
  852.     {
  853.         strcpy(this->brSmetka,s.brSmetka);
  854.  
  855.         izvrseni=new Transakcija* [br];
  856.         for(int i=0;i<br;++i)
  857.             izvrseni[i]=s.izvrseni[i];
  858.     }
  859.     Smetka &operator=(const Smetka &s)
  860.     {
  861.         if(this!=&s)
  862.         {
  863.             br=s.br;
  864.             saldo=s.saldo;
  865.             strcpy(brSmetka,s.brSmetka);
  866.  
  867.             delete [] izvrseni;
  868.             izvrseni=new Transakcija* [br];
  869.             for(int i=0;i<br;++i)
  870.                 izvrseni[i]=s.izvrseni[i];
  871.         }
  872.         return *this;
  873.     }
  874.     ~Smetka()
  875.     {
  876.         for(int i=0;i<br;++i)
  877.             delete izvrseni[i];
  878.         delete [] izvrseni;
  879.     }
  880.  
  881.     Smetka &operator+=(Transakcija *t)
  882.     {
  883.         Transakcija **tmp = new Transakcija* [br+1];
  884.         for (int i=0;i<br;++i)
  885.             tmp[i]=izvrseni[i];
  886.         tmp[br++]=t;
  887.  
  888.         delete [] izvrseni;
  889.         izvrseni=tmp;
  890.         return *this;
  891.     }
  892.     void izvestajVoDenari()
  893.     {
  894.         double s=saldo;
  895.         cout<<"Korisnikot so smetka: "<<brSmetka<<" ima momentalno saldo od ";
  896.  
  897.         for(int i=0;i<br;++i)
  898.             s+=izvrseni[i]->voDenari();
  899.         cout<<s<<" MKD"<<endl;
  900.     }
  901.     void pecatiTransakcii()
  902.     {
  903.         for(int i=0;i<br;++i)
  904.             izvrseni[i]->pecati();
  905.     }
  906. };
  907. int main()
  908. {
  909.  
  910.     Smetka s ("300047024112789",1500);
  911.  
  912.     int n, den, mesec, godina, tip;
  913.     double iznos;
  914.     char valuta [3];
  915.  
  916.     cin>>n;
  917.     cout<<"===VNESUVANJE NA TRANSAKCIITE I SPRAVUVANJE SO ISKLUCOCI==="<<endl;
  918.     for (int i=0;i<n;i++){
  919.         cin>>tip>>den>>mesec>>godina>>iznos;
  920.  
  921.         try{
  922.         if (tip==2){
  923.             cin>>valuta;
  924.  
  925.                 if(den<1 || den>31 || mesec<1 || mesec>12)
  926.                     throw InvalidDateException(den,mesec,godina);
  927.  
  928.                 if(strcmp(valuta,"EUR")&&strcmp(valuta,"USD"))
  929.                     throw NotSupportedCurrencyException(valuta);
  930.  
  931.                 Transakcija * t = new DeviznaTransakcija(den,mesec,godina,iznos,valuta);
  932.                 s+=t;
  933.             }
  934.  
  935.             //delete t;
  936.         else {
  937.                 if(den<1 || den>31 || mesec<1 || mesec>12)
  938.                     throw InvalidDateException(den,mesec,godina);
  939.  
  940.                 Transakcija * t = new DenarskaTransakcija(den,mesec,godina,iznos);
  941.                 s+=t;
  942.             }
  943.             //delete t;
  944.         }
  945.  
  946.         catch(X &x)
  947.             {
  948.                 x.print();
  949.             }
  950.  
  951.     }
  952.     cout<<"===PECHATENJE NA SITE TRANSAKCII==="<<endl;
  953.     s.pecatiTransakcii();
  954.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  955.     s.izvestajVoDenari();
  956.  
  957.  
  958.     cout<<"\n===PROMENA NA KURSOT NA EVROTO I DOLAROT===\n"<<endl;
  959.  
  960.  
  961.     double newEUR, newUSD;
  962.     cin>>newEUR>>newUSD;
  963.     Transakcija::setEUR(newEUR);
  964.     Transakcija::setUSD(newUSD);
  965.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  966.     s.izvestajVoDenari();
  967.  
  968.  
  969.     return 0;
  970. }
  971.  
  972. 5. Броеви
  973. #include<iostream>
  974. #include<cstring>
  975.  
  976. using namespace std;
  977.  
  978. class ArithmeticException {
  979.    
  980.     public:
  981.     void message() {
  982.         cout<<"Division by zero is not allowed"<<endl;
  983.     }
  984. };
  985.  
  986. class NumbersNotDivisibleException {
  987.     private:
  988.     int divider;
  989.    
  990.     public:
  991.     NumbersNotDivisibleException (int d){
  992.         divider=d;
  993.     }
  994.    
  995.     void message() {
  996.         cout<<"Division by "<<divider<<" is not supported"<<endl;      
  997.     }
  998. };
  999.  
  1000. class ArrayFullException{
  1001.     public:
  1002.     void message() {
  1003.         cout<<"The array is full. Increase the capacity"<<endl;
  1004.     }
  1005. };
  1006.  
  1007. class IndexOutOfBoundsException {
  1008.     private:
  1009.     int index;
  1010.    
  1011.     public:
  1012.     IndexOutOfBoundsException(int i){
  1013.         index=i;
  1014.     }
  1015.    
  1016.     void message() {
  1017.         cout<<"Index "<<index<<" is out of bounds"<<endl;
  1018.     }
  1019. };
  1020.  
  1021. class NumbersIsNotPositiveException {
  1022.     private:
  1023.     int number;
  1024.    
  1025.     public:
  1026.     NumbersIsNotPositiveException(int n) {
  1027.         number=n;
  1028.     }
  1029.    
  1030.     void message() {
  1031.         cout<<"Number "<<number<<" is not positive integer"<<endl;
  1032.     }
  1033. };
  1034.  
  1035. class PositiveIntegers {
  1036.     private:
  1037.     int * numbers;
  1038.     int size;
  1039.     int capacity;
  1040.    
  1041.     void copy(const PositiveIntegers &ps){
  1042.         numbers = new int [ps.capacity];
  1043.         for (int i=0;i<ps.size;i++)
  1044.             numbers[i]=ps.numbers[i];
  1045.         size=ps.size;
  1046.         capacity=ps.capacity;
  1047.     }
  1048.    
  1049.     public:
  1050.     PositiveIntegers(int capacity=0){
  1051.         this->capacity=capacity;
  1052.         numbers = new int [capacity];
  1053.         size = 0;
  1054.     }
  1055.    
  1056.     PositiveIntegers (const PositiveIntegers &ps){
  1057.         copy(ps);
  1058.     }
  1059.    
  1060.     PositiveIntegers &operator = (const PositiveIntegers &ps){
  1061.         if (this!=&ps){
  1062.             delete [] numbers;
  1063.             copy(ps);
  1064.         }
  1065.        
  1066.         return *this;
  1067.     }
  1068.    
  1069.     ~PositiveIntegers() {
  1070.         //delete [] numbers;
  1071.     }
  1072.    
  1073.     void increaseCapacity(int c) {
  1074.         int * tmp = new int [capacity+c];
  1075.         for (int i=0;i<size;i++){
  1076.             tmp[i]=numbers[i];
  1077.         }
  1078.         delete [] numbers;
  1079.         numbers=tmp;
  1080.         capacity+=c;
  1081.     }
  1082.    
  1083.     PositiveIntegers &operator += (int newNumber){
  1084.         if (size==capacity)
  1085.             throw ArrayFullException();
  1086.         if (newNumber<=0)
  1087.             throw NumbersIsNotPositiveException(newNumber);
  1088.        
  1089.         numbers[size++]=newNumber;
  1090.         return *this;
  1091.     }
  1092.    
  1093.     PositiveIntegers operator * (int x) {
  1094.         PositiveIntegers pi (*this);
  1095.         for (int i=0;i<pi.size;i++)
  1096.             pi.numbers[i]*=x;
  1097.        
  1098.         return pi;
  1099.     }
  1100.    
  1101.     PositiveIntegers operator / (int x){
  1102.         if (x==0)
  1103.             throw ArithmeticException();
  1104.         for (int i=0;i<size;i++)
  1105.             if (numbers[i]%x!=0)
  1106.                 throw NumbersNotDivisibleException(x);
  1107.            
  1108.         PositiveIntegers pi (*this);
  1109.         for (int i=0;i<pi.size;i++)
  1110.             pi.numbers[i]/=x;
  1111.        
  1112.         return pi;
  1113.     }
  1114.    
  1115.     int &operator [] (int i){
  1116.         if (i<0 || i>this->size)
  1117.             throw IndexOutOfBoundsException(i);
  1118.        
  1119.         return numbers[i];
  1120.     }
  1121.    
  1122.     void print () {
  1123.         cout<<"Size: "<<size<<" Capacity: "<<capacity<<" Numbers: ";
  1124.         for (int i=0;i<size;i++)
  1125.             cout<<numbers[i]<<" ";
  1126.        
  1127.         cout<<endl;
  1128.     }
  1129.    
  1130.    
  1131.    
  1132.    
  1133. };
  1134.  
  1135.  
  1136. int main() {
  1137.    
  1138.     int n,capacity;
  1139.     cin >> n >> capacity;
  1140.     PositiveIntegers pi (capacity);
  1141.     for (int i=0;i<n;i++){
  1142.         int number;
  1143.         cin>>number;
  1144.         try{
  1145.             //cout<<number;
  1146.         pi+=number;
  1147.        
  1148.         }
  1149.         catch (ArrayFullException &e){
  1150.             e.message();
  1151.         }
  1152.         catch (NumbersIsNotPositiveException &e){
  1153.             e.message();
  1154.         }
  1155.     }
  1156.     cout<<"===FIRST ATTEMPT TO ADD NUMBERS==="<<endl;
  1157.     pi.print();
  1158.     int incCapacity;
  1159.     cin>>incCapacity;
  1160.     pi.increaseCapacity(incCapacity);
  1161.     cout<<"===INCREASING CAPACITY==="<<endl;
  1162.     pi.print();
  1163.    
  1164.     int n1;
  1165.     cin>>n1;
  1166.     for (int i=0;i<n1;i++){
  1167.         int number;
  1168.         cin>>number;
  1169.         try{
  1170.             //cout<<number;
  1171.         pi+=number;
  1172.        
  1173.         }
  1174.         catch (ArrayFullException &e){
  1175.             e.message();
  1176.         }
  1177.         catch (NumbersIsNotPositiveException &e){
  1178.             e.message();
  1179.         }
  1180.     }
  1181.     cout<<"===SECOND ATTEMPT TO ADD NUMBERS==="<<endl;
  1182.     pi.print();
  1183.    
  1184.     PositiveIntegers pi1;
  1185.    
  1186.     cout<<"===TESTING DIVISION==="<<endl;
  1187.    
  1188.     try{
  1189.     pi1 = pi/0;
  1190.     pi1.print();
  1191.     }
  1192.     catch (ArithmeticException &e){
  1193.         e.message();
  1194.     }
  1195.     catch (NumbersNotDivisibleException &e){
  1196.         e.message();
  1197.     }
  1198.    
  1199.     try{
  1200.     pi1 = pi/1;
  1201.     pi1.print();
  1202.     }
  1203.     catch (ArithmeticException &e){
  1204.         e.message();
  1205.     }
  1206.     catch (NumbersNotDivisibleException &e){
  1207.         e.message();
  1208.     }
  1209.    
  1210.     try{
  1211.     pi1 = pi/2;
  1212.     pi1.print();
  1213.     }
  1214.     catch (ArithmeticException &e){
  1215.         e.message();
  1216.     }
  1217.     catch (NumbersNotDivisibleException &e){
  1218.         e.message();
  1219.     }
  1220.    
  1221.     cout<<"===TESTING MULTIPLICATION==="<<endl;
  1222.     pi1 = pi*3;
  1223.     pi1.print();
  1224.    
  1225.    
  1226.     cout<<"===TESTING [] ==="<<endl;
  1227.    
  1228.     try{
  1229.     cout<<"PositiveIntegers[-1] = "<<pi[-1]<<endl;
  1230.    
  1231.     }
  1232.     catch (IndexOutOfBoundsException &e){
  1233.         e.message();
  1234.     }
  1235.    
  1236.     try{
  1237.     cout<<"PositiveIntegers[2] = "<<pi[2]<<endl;
  1238.     }
  1239.     catch (IndexOutOfBoundsException &e){
  1240.         e.message();
  1241.     }
  1242.    
  1243.     try{  
  1244.     cout<<"PositiveIntegers[3] = "<<pi[3]<<endl;
  1245.     }
  1246.     catch (IndexOutOfBoundsException &e){
  1247.         e.message();
  1248.     }
  1249.    
  1250.     try{
  1251.     cout<<"PositiveIntegers[12] = "<<pi[12]<<endl;
  1252.     }
  1253.     catch (IndexOutOfBoundsException &e){
  1254.         e.message();
  1255.     }
  1256.    
  1257.    
  1258.    
  1259.     return 0;
  1260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement