Advertisement
epitxx

lab4.cpp

Mar 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Alpha {
  8.  
  9. private:
  10.     unsigned bin;
  11.  
  12. public:
  13.     Alpha() { bin = 0; };
  14.     Alpha(Alpha& y) { bin = y.bin; };
  15.  
  16.     Alpha(const char*);
  17.  
  18.     Alpha operator*(Alpha &);
  19.  
  20.     friend ostream& operator << (ostream&, Alpha&);
  21. };
  22.  
  23. Alpha::Alpha(const char* s) {
  24.     bin = 0;
  25.  
  26.     while (*s) {
  27.         bin |= (1 << (tolower(*s) - 'a')); s++;
  28.     }
  29. }
  30.  
  31. Alpha Alpha::operator*(Alpha &a)
  32. {
  33.     Alpha z;
  34.     z.bin = this->bin & a.bin;
  35.     return z;
  36. }
  37.  
  38. ostream& operator << (ostream& out, Alpha& z) {
  39.     unsigned bit = 1;
  40.     int i;
  41.     string str;
  42.  
  43.     for (int i = 0, k = 0; i<26; i++) {
  44.         if ((z.bin & bit) > 0)
  45.             str += (char)('a' + i);
  46.  
  47.         bit = bit << 1;
  48.     }
  49.     std::reverse(str.begin(), str.end());
  50.     out << str.c_str();
  51.  
  52.     return out;
  53. }
  54.  
  55. int main(int argc, char* argv[]) {
  56.     if (argc < 3)
  57.         return 1;
  58.  
  59.     Alpha x(argv[1]);
  60.     Alpha y(argv[2]);
  61.     Alpha z;
  62.  
  63.     z = x * y;
  64.  
  65.     cout << z;
  66.  
  67.     return (0);
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement