velimir

Korisnik

Jun 15th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3.  
  4. using namespace std;
  5.  
  6. class BadInputException{
  7. public:
  8.     void pecati(){
  9.     cout<<"Bezbednosniot kod e vo losh format"<<endl;
  10.     }
  11. };
  12.  
  13. class Korisnik {
  14. protected:
  15.     char ime[30];
  16.     char lozinka[10];
  17.     bool daliBezbednosen;
  18.     static int T;
  19. public:
  20.     Korisnik(char* ime,char * lozinka) {
  21.         strcpy(this->ime,ime);
  22.         strcpy(this->lozinka,lozinka);
  23.         this->daliBezbednosen=false;
  24.     }
  25.     static void setT(int t){
  26.         T = t;
  27.     }
  28.     virtual ~Korisnik(){}
  29.     virtual Korisnik& operator+=(char *kod){
  30.         return *this;
  31.     }
  32.     virtual int numerickaTezina(){
  33.         int tez=0;
  34.         for(int i=0; i<strlen(lozinka); i++){
  35.             if(isdigit(lozinka[i]))tez++;
  36.             else if(!isalpha(lozinka[i]))tez+=T;
  37.         }
  38.         return tez;
  39.     }
  40.     friend ostream& operator<<(ostream &o, Korisnik &k){
  41.         return o << k.numerickaTezina() << ":" << k.ime << " ****" << endl;
  42.     }
  43.     char *getIme(){
  44.         return ime;
  45.     }
  46.     bool getDaliBezbednosen(){
  47.         return daliBezbednosen;
  48.     }
  49. };
  50.  
  51. int Korisnik::T=3;
  52.  
  53. class SpecijalenKorisnik : public Korisnik{
  54.     char *kod;
  55. public:
  56.     SpecijalenKorisnik(char* ime,char * lozinka) : Korisnik(ime, lozinka){
  57.         daliBezbednosen = true;
  58.     }
  59.     int numerickaTezina(){
  60.         return Korisnik::numerickaTezina() + strlen(kod);
  61.     }
  62.     SpecijalenKorisnik& operator+=(char *k){
  63.         bool TF = false;
  64.         for(int i=0; i<strlen(k); i++){
  65.             if(!isalpha(k[i]))TF=true;
  66.         }
  67.         try{
  68.             if(!TF) throw BadInputException();
  69.         }
  70.         catch(BadInputException BIE){
  71.             BIE.pecati();
  72.             strcat(k, "123");
  73.         }
  74.         delete [] kod;
  75.         kod = new char[strlen(k)+1];
  76.         strcpy(kod, k);
  77.         return *this;
  78.     }
  79.     friend ostream& operator<<(ostream &o, SpecijalenKorisnik &sk){
  80.         return o << sk.numerickaTezina() << ":" << sk.ime << " ****" << endl;
  81.     }
  82. };
  83.  
  84. class VebStranica {
  85. private:
  86.     char adresa[30];
  87.     Korisnik *korisnici[20];
  88.     int broj;
  89.     static const int MIN = 5;
  90. public:
  91.     VebStranica(char *naziv, Korisnik** korisnici,int broj ) {
  92.         strcpy(this->adresa,adresa);
  93.         for (int i=0; i<broj; i++) {
  94.             //ako korisnikot ima bezbednosen kod
  95.             if (korisnici[i]->getDaliBezbednosen()) {
  96.                 this->korisnici[i]=new SpecijalenKorisnik(*dynamic_cast<SpecijalenKorisnik*>(korisnici[i]));
  97.             } else this->korisnici[i]=new Korisnik(*korisnici[i]);
  98.         }
  99.         this->broj=broj;
  100.     }
  101.     ~VebStranica() {
  102.         for (int i=0; i<broj; i++) delete korisnici[i];
  103.     }
  104.  
  105.     void pecatiKorisnici(){
  106.         cout << "Na stranicata code.finki.ukim.mk slednite korisnici se najaveni so bezbedna lozinka:\n";
  107.         for(int i=0; i<broj; i++){
  108.             if(korisnici[i]->numerickaTezina()>=MIN){
  109.                 cout << *korisnici[i];
  110.             }
  111.         }
  112.     }
  113.     void postaviBezbednosenKod(char *ime, char *kod){
  114.         /*for(int i=0; i<broj; i++){
  115.             if(strcmp(ime, korisnici[i]->getIme())){
  116.                 bool tf = true;
  117.                 for(int j=0; j<strlen(kod); j++){
  118.                     if(!isalpha(kod[i])){
  119.                         tf = false;
  120.                     }
  121.                     if(tf){
  122.                         strcat(kod, "123");
  123.                         *korisnici[i]+=kod;
  124.                         throw BadInputException();
  125.                     }
  126.                     *korisnici[i]+=kod;
  127.                 }
  128.             }
  129.         }*/
  130.         for(int i=0; i<broj; i++){
  131.             if(!strcmp(ime, korisnici[i]->getIme())&&korisnici[i]->getDaliBezbednosen()){
  132.                 *dynamic_cast<SpecijalenKorisnik*>(korisnici[i])+=kod;
  133.             }
  134.         }
  135.     }
  136. };
  137.  
  138. int main() {
  139.  
  140.     Korisnik **niza;
  141.     int n,m;
  142.     char ime[30],kod[10],lozinka[10];
  143.     bool daliBezbednosen;
  144.     cin>>n;
  145.     niza=new Korisnik*[n];
  146.     for (int i=0; i<n; i++) {
  147.         cin>>ime;
  148.         cin>>lozinka;
  149.         cin>>daliBezbednosen;
  150.         if (!daliBezbednosen)
  151.             niza[i]=new Korisnik(ime,lozinka);
  152.         else
  153.             niza[i]=new SpecijalenKorisnik(ime,lozinka);
  154.     }
  155.  
  156.     VebStranica strana("code.finki.ukim.mk",niza,n);
  157.     for (int i=0; i<n; i++) delete niza[i];
  158.     delete [] niza;
  159.     cin>>m;
  160.     for (int i=0; i<m; i++) {
  161.         cin>>ime>>kod;
  162.         strana.postaviBezbednosenKod(ime,kod);
  163.     }
  164.  
  165.     Korisnik::setT(2);
  166.  
  167.     strana.pecatiKorisnici();
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment