Advertisement
Guest User

PicaBurek(LabZadaci)

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