Advertisement
Guest User

lab4

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <cctype>
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class Alpha
  8. {
  9.     private:
  10.         unsigned bin;
  11.     public:
  12.         Alpha() {bin=0;};
  13.         Alpha(Alpha& y) {bin=y.bin;};
  14.         Alpha(char*);
  15.         Alpha operator +(Alpha&);
  16.         Alpha operator -(Alpha&);
  17.         Alpha operator &(Alpha&);
  18.         Alpha operator ~();
  19.         operator char*();
  20.         friend ostream& operator << (ostream&, Alpha&);
  21. };
  22.  
  23. Alpha::Alpha(char* s)
  24. {
  25.     bin=0;
  26.     while(*s)
  27.     {
  28.         bin |= (1 << (tolower(*s) - 'a'));
  29.         s++;
  30.     }
  31. }
  32.  
  33. Alpha Alpha::operator + (Alpha& y)
  34. {
  35.     Alpha z;
  36.     z.bin = bin | y.bin;
  37.     return(z);
  38. }
  39.  
  40. Alpha Alpha::operator - (Alpha& y)
  41. {
  42.     Alpha z;
  43.     z.bin = bin - y.bin;
  44.     return(z);
  45. }
  46.  
  47.  
  48. Alpha Alpha::operator & (Alpha& y)
  49. {
  50.     Alpha z;
  51.     z.bin = bin & y.bin;
  52.     return(z);
  53. }
  54.  
  55. Alpha Alpha::operator ~ ()
  56. {
  57.     Alpha q;
  58.     q.bin = ~bin;
  59.     return(q);
  60. }
  61.  
  62. ostream& operator << (ostream& out, Alpha& z)
  63. {
  64.     unsigned bit=1;
  65.     int i;
  66.     for(i=27; i>1; i--)
  67.     {
  68.         if((z.bin & bit)>0)
  69.             out<<(char)('a'+i);
  70.         bit=bit<<1;
  71.     }
  72.     return out;
  73. }
  74.  
  75. Alpha::operator char*()
  76. {
  77.     static char s[32];
  78.     unsigned w=bin;
  79.     int i=0, j=0;
  80.     while(w>0)
  81.     {
  82.         if(w & 1)
  83.             s[j++]='a'+i;
  84.         i++;
  85.         w = w >> 1;
  86.     }
  87.     s[j++]='\n';
  88.     s[j]='\0';
  89.     return (s);
  90. }
  91.  
  92. int main (int argc, char* argv[])
  93. {
  94.     Alpha x(argv[1]);
  95.     Alpha y(argv[2]);
  96.     Alpha z;
  97.     z=x&y;
  98.     z=x-z;
  99.     z=~z;
  100.     cout<<z<<endl;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement