Advertisement
emilpopovski

Exceptions 2

May 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.98 KB | None | 0 0
  1. 1. #include <iostream>
  2. #include <cstring>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. class Korisnik
  7. {
  8. public:
  9.     char username[20];
  10.     char password[20];
  11.     bool code;
  12.     static int T;
  13.  
  14. public:
  15.     Korisnik(char username[20]="", char password[20]="", bool code=false)
  16.     {
  17.         strcpy(this->username, username);
  18.         strcpy(this->password, password);
  19.         this->code = code;
  20.     }
  21.  
  22.     static void setT(int tezina)
  23.     {
  24.         Korisnik::T = tezina;
  25.     }
  26.  
  27.     char *getUsername(){return this->username;}
  28.  
  29.     virtual int presmetajTezina()
  30.     {
  31.         int n = strlen(this->password);
  32.         int suma = 0;
  33.  
  34.         for(int i=0; i<n; i++)
  35.         {
  36.             char current = this->password[i];
  37.             if((current >= 'a'&&current <='z') || (current >= 'A' && current <='Z'))
  38.             {
  39.                 suma += 0;
  40.             }
  41.             else if((current >= '0' && current <='9'))
  42.             {
  43.                 suma += 1;
  44.             }
  45.             else
  46.             {
  47.                 suma += Korisnik::T;
  48.             }
  49.         }
  50.  
  51.         return suma;
  52.     }
  53.  
  54.     friend ostream& operator<<(ostream &out, Korisnik &k)
  55.     {
  56.         out << k.presmetajTezina() << ":" << k.username << " ****" << endl;
  57.  
  58.         return out;
  59.     }
  60.  
  61.     ~Korisnik()
  62.     {
  63.     }
  64. };
  65.  
  66. int Korisnik::T;
  67.  
  68. class BadInputException
  69. {
  70. public:
  71.     char *msg()
  72.     {
  73.         return "Bezbednosniot kod e vo losh format";
  74.     }
  75. };
  76.  
  77. class SpecijalenKorisnik : public Korisnik
  78. {
  79. public:
  80.     char * securityCode;
  81.  
  82. public:
  83.     SpecijalenKorisnik(char username[20]="", char password[20]="", bool code=false, char *securityCode=""):Korisnik(username, password, code)
  84.     {
  85.         this->securityCode = new char[strlen(securityCode) + 1];
  86.         strcpy(this->securityCode, securityCode);
  87.     }
  88.  
  89.     SpecijalenKorisnik &operator+=(char *securityCode)
  90.     {
  91.         bool flag = true;
  92.         int n = strlen(securityCode);
  93.  
  94.         for(int i=0;i<n;i++)
  95.         {
  96.             if(!isalpha(securityCode[i]))
  97.             {
  98.                 flag = false;
  99.                 break;
  100.             }
  101.         }
  102.  
  103.         if(flag)
  104.         {
  105.             throw BadInputException();
  106.         }
  107.  
  108.         delete this->securityCode;
  109.         this->securityCode = new char[strlen(securityCode) + 1];
  110.         strcpy(this->securityCode, securityCode);
  111.  
  112.         return *this;
  113.     }
  114.  
  115.     int presmetajTezina()
  116.     {
  117.         int suma = Korisnik::presmetajTezina();
  118.  
  119.         return suma + strlen(securityCode);
  120.     }
  121.  
  122.     friend ostream& operator<<(ostream &out, SpecijalenKorisnik &k)
  123.     {
  124.         out << k.presmetajTezina() << ":" << k.username << " ****" << endl;
  125.  
  126.         return out;
  127.     }
  128.  
  129.     ~SpecijalenKorisnik()
  130.     {
  131.         delete this->securityCode;
  132.     }
  133. };
  134.  
  135. class VebStranica
  136. {
  137. private:
  138.     char url[30];
  139.     Korisnik *korisnici[20];
  140.     int n;
  141.     static int MIN;
  142.  
  143. public:
  144.     VebStranica(char url[30]="", Korisnik *korisnici[20]=NULL, int n=0)
  145.     {
  146.         strcpy(this->url, url);
  147.  
  148.         for(int i=0;i<n;i++)
  149.         {
  150.             SpecijalenKorisnik *sk = dynamic_cast<SpecijalenKorisnik *> (korisnici[i]);
  151.  
  152.             if(sk)
  153.                 this->korisnici[i] = new SpecijalenKorisnik(sk->username, sk->password, sk->code, sk->securityCode);
  154.             else
  155.                 this->korisnici[i] = new Korisnik(korisnici[i]->username, korisnici[i]->password, korisnici[i]->code);
  156.         }
  157.  
  158.         this->n = n;
  159.     }
  160.  
  161.     bool bezbednaLozinka(Korisnik *k)
  162.     {
  163.         if(k->presmetajTezina() >= MIN)
  164.             return true;
  165.  
  166.         return false;
  167.     }
  168.  
  169.     void pecatiKorisnici()
  170.     {
  171.         cout << "Na stranicata " << this->url << " slednite korisnici se najaveni so bezbedna lozinka:" << endl;
  172.         for(int i=0;i<this->n;i++)
  173.         {
  174.             if(bezbednaLozinka(this->korisnici[i]))
  175.                 cout << *(this->korisnici[i]);
  176.         }
  177.     }
  178.  
  179.     void postaviBezbednosenKod(char *username, char *code)
  180.     {
  181.         for(int i=0;i<this->n;i++)
  182.         {
  183.             if(strcmp(this->korisnici[i]->getUsername(), username)==0)
  184.             {
  185.                 SpecijalenKorisnik *sk = dynamic_cast<SpecijalenKorisnik*>(korisnici[i]);
  186.  
  187.                 if(sk)
  188.                 {
  189.                     *(sk)+=code;
  190.                 }
  191.             }
  192.         }
  193.     }
  194. };
  195.  
  196. int VebStranica::MIN = 5;
  197.  
  198. int main()
  199. {
  200.     Korisnik **niza;
  201.     int n,m;
  202.     char ime[30],kod[10],lozinka[10];
  203.     bool daliBezbednosen;
  204.     cin>>n;
  205.     niza=new Korisnik*[n];
  206.     for (int i=0; i<n; i++)
  207.     {
  208.         cin>>ime;
  209.         cin>>lozinka;
  210.         cin>>daliBezbednosen;
  211.         if (!daliBezbednosen)
  212.         {
  213.             niza[i]=new Korisnik(ime,lozinka);
  214.         }
  215.         else
  216.             niza[i]=new SpecijalenKorisnik(ime,lozinka);
  217.     }
  218.  
  219.     VebStranica strana("code.finki.ukim.mk",niza,n);
  220.     for (int i=0; i<n; i++) delete niza[i];
  221.     delete [] niza;
  222.     cin>>m;
  223.     for (int i=0; i<m; i++)
  224.     {
  225.         cin>>ime>>kod;
  226.         try
  227.         {
  228.             strana.postaviBezbednosenKod(ime,kod);
  229.         }catch(BadInputException e)
  230.         {
  231.             cout << e.msg() << endl;
  232.             strcat(kod, "123");
  233.             strana.postaviBezbednosenKod(ime, kod);
  234.         }
  235.     }
  236.  
  237.     Korisnik::setT(2);
  238.  
  239.     //cout << "PECATI KORISNICI" << endl;
  240.     strana.pecatiKorisnici();
  241.  
  242.     return 0;
  243. }
  244. 2. #include<iostream>
  245. #include<cstring>
  246. using namespace std;
  247.  
  248. class IndexOutOfBoundException
  249. {
  250. public:
  251.     void print()
  252.     {
  253.         cout<<"Ne moze da se vnesat uste dopolnitelni aktivnosti"<<endl;
  254.     }
  255. };
  256.  
  257. class UcesnikKurs
  258. {
  259. protected:
  260.     char ime[20];
  261.     int finalenIspit;
  262.     bool daliAktivnosti;
  263.  
  264. public:
  265.     UcesnikKurs(char ime[20]="",int finalenIspit=0,bool daliAktivnosti=false)
  266.     {
  267.         strcpy(this->ime,ime);
  268.         this->finalenIspit=finalenIspit;
  269.         this->daliAktivnosti=daliAktivnosti;
  270.     }
  271.     friend ostream &operator<<(ostream &out,UcesnikKurs &u)
  272.     {
  273.         out<<u.ime<<" - "<<u.osvoeniPoeni()<<endl;
  274.         return out;
  275.     }
  276.     virtual int osvoeniPoeni()
  277.     {
  278.         return this->finalenIspit;
  279.     }
  280.      bool getDaliAktivnosti()
  281.      {
  282.          return this->daliAktivnosti;
  283.      }
  284.     int getFinalenIspit()
  285.     {
  286.         return this->finalenIspit;
  287.     }
  288.     char *getIme()
  289.     {
  290.         return this->ime;
  291.     }
  292.  
  293. };
  294. class UcesnikKursDopolnitelen : public UcesnikKurs
  295. {
  296. private:
  297.     int brAktivnosti;
  298.     int *poeniAktivnosti;
  299.     const static int M;
  300. public:
  301.     int getBrAktivnosti()
  302.     {
  303.         return this->brAktivnosti;
  304.     }
  305.     int *getPoeniAktivnosti()
  306.     {
  307.         return this->poeniAktivnosti;
  308.     }
  309.     UcesnikKursDopolnitelen(char ime[20]="",int finalenIspit=0,bool daliAktivnosti=false,int brAktivnosti=0,int *poeniAktivnosti=0):UcesnikKurs(ime,finalenIspit,daliAktivnosti)
  310.     {
  311.         for(int i=0;i<brAktivnosti;i++)
  312.         {
  313.             this->poeniAktivnosti[i]=poeniAktivnosti[i];
  314.         }
  315.         this->brAktivnosti=brAktivnosti;
  316.     }
  317.     int osvoeniPoeni()
  318.     {
  319.         int suma=0;
  320.         for(int i=0;i<this->brAktivnosti;i++)
  321.         {
  322.             suma+=poeniAktivnosti[i];
  323.         }
  324.         return this->finalenIspit*0.8+suma;
  325.     }
  326.     friend ostream &operator<<(ostream &out,UcesnikKursDopolnitelen &k)
  327.     {
  328.         out<<k.ime<<" - "<<k.osvoeniPoeni()<<endl;
  329.         return out;
  330.     }
  331.     UcesnikKursDopolnitelen &operator+=(int p)
  332.     {
  333.  
  334.         if(this->brAktivnosti>=M)
  335.             throw IndexOutOfBoundException();
  336.  
  337.         int *tmp=new int[this->brAktivnosti+1];
  338.         for(int i=0;i<this->brAktivnosti;i++)
  339.         {
  340.             tmp[i]=poeniAktivnosti[i];
  341.         }
  342.         delete [] this->poeniAktivnosti;
  343.         poeniAktivnosti=tmp;
  344.         poeniAktivnosti[brAktivnosti]=p;
  345.         this->brAktivnosti++;
  346.  
  347.         return *this;
  348.     }
  349. };
  350. const int UcesnikKursDopolnitelen::M=5;
  351. class Kurs
  352. {
  353. private:
  354.     char ime[30];
  355.     UcesnikKurs *ucesnici[10];
  356.     int n;
  357.     static int P;
  358. public:
  359.     static void setP(int pp)
  360.     {
  361.         P=pp;
  362.     }
  363.  
  364.     Kurs(char *ime, UcesnikKurs** ucesnici,int n )
  365.     {
  366.       strcpy(this->ime,ime);
  367.       for (int i=0;i<n;i++)
  368.       {
  369.         UcesnikKursDopolnitelen *u=dynamic_cast<UcesnikKursDopolnitelen*>(ucesnici[i]);
  370.         if (u){
  371.             this->ucesnici[i]=new UcesnikKursDopolnitelen(*dynamic_cast<UcesnikKursDopolnitelen*>(ucesnici[i]));
  372.         }
  373.         else this->ucesnici[i]=new UcesnikKurs(*ucesnici[i]);
  374.       }
  375.       this->n=n;
  376.     }
  377.  
  378.     void pecatiUcesnici()
  379.     {
  380.         cout<<"Na kursot "<<this->ime<<" polozeni se:"<<endl;
  381.         for(int i=0;i<this->n;i++)
  382.         {
  383.             if(ucesnici[i]->osvoeniPoeni() >= P)
  384.                 cout<<*(ucesnici[i]);
  385.         }
  386.     }
  387.     void dodadiPoeniDopolnitelnaAktivnost(char *name,int points)
  388.     {
  389.         for(int i=0;i<this->n;i++)
  390.         {
  391.             if(strcmp(ucesnici[i]->getIme(),name)==0)
  392.             {
  393.                 UcesnikKursDopolnitelen *u=dynamic_cast<UcesnikKursDopolnitelen*>(ucesnici[i]);
  394.                 if(u)
  395.                 {
  396.                      *(u)+=points;
  397.  
  398.                }
  399.             }
  400.         }
  401.  
  402.     }
  403.  
  404. };
  405. int Kurs::P=50;
  406.  
  407. int main(){
  408.  
  409. UcesnikKurs **niza;
  410. int n,m,poeni;
  411. char ime[10];
  412. bool daliDopolnitelnaAktivnost;
  413. cin>>n;
  414. niza=new UcesnikKurs*[n];
  415. for (int i=0;i<n;i++){
  416.    cin>>ime;
  417.    cin>>poeni;
  418.    cin>>daliDopolnitelnaAktivnost;
  419.    if (!daliDopolnitelnaAktivnost)
  420.     niza[i]=new UcesnikKurs(ime,poeni);
  421.    else
  422.     niza[i]=new UcesnikKursDopolnitelen(ime,poeni);
  423. }
  424.  
  425. Kurs programiranje("Programiranje",niza,n);
  426. for (int i=0;i<n;i++) delete niza[i];
  427. delete [] niza;
  428.  
  429. cin>>m;
  430. for (int i=0;i<m;i++){
  431.    cin>>ime>>poeni;
  432.     try{
  433.    programiranje.dodadiPoeniDopolnitelnaAktivnost(ime,poeni);
  434.     }
  435.     catch(IndexOutOfBoundException e)
  436.     {
  437.         e.print();
  438.          programiranje.dodadiPoeniDopolnitelnaAktivnost(ime,poeni);
  439.     }
  440. }
  441.  
  442. Kurs::setP(60);
  443.  
  444. programiranje.pecatiUcesnici();
  445.  
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement