Advertisement
tomasaccini

Untitled

Jul 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. class Oracion {
  8. public:
  9.     char* a;
  10.     Oracion();
  11.     Oracion(const Oracion& o);
  12.     Oracion& operator + (const Oracion& other);
  13.     Oracion& operator - (const Oracion& other);
  14.     Oracion& operator = (const Oracion& other);
  15.     bool operator == (const Oracion& other);
  16.  
  17.     friend ostream& operator << (ostream& o, const Oracion& other);
  18.     friend istream& operator << (istream& i, const Oracion& other);
  19. };
  20.  
  21. Oracion& Oracion::operator = (const Oracion& other) {
  22.     if (*this == other)
  23.         return *this;
  24.     char* aux = strdup(other.a);
  25.     if (!aux) return *this;
  26.     free(this->a);
  27.     this->a = aux;
  28.     return *this;
  29. }
  30.  
  31. Oracion& Oracion::operator - (const Oracion& other) {
  32.     size_t this_size = strlen(this->a);
  33.     size_t other_size = strlen(other.a);
  34.     size_t minimum_size = this_size > other_size ? this_size : other_size;
  35.  
  36.     size_t matches = 0;
  37.     size_t removed = 0;
  38.     for (size_t i = 0; i < minimum_size; i++) {
  39.         if (this->a[i] != other.a[i]) {
  40.             matches = 0;
  41.             continue;
  42.         }
  43.         matches++;
  44.         if (matches == other_size) {
  45.             for (size_t j = 0; j < other_size; j++){
  46.                 this->a[i - j] == '*';
  47.                 removed++;
  48.             }
  49.             matches = 0;
  50.         }
  51.     }
  52.  
  53.     char* aux = (char*)malloc(sizeof(char) * (this_size - removed));
  54.     if (!aux)
  55.         return *this;
  56.     size_t pos_actual = 0;
  57.     for (size_t i = 0; i < this_size; i++) {
  58.         if (this->a[i] != '*') {
  59.             aux[pos_actual] = this->a[i];
  60.             pos_actual++;
  61.         }
  62.     }
  63.     aux[pos_actual] = '\0';
  64.     free(this->a);
  65.     this->a = aux;
  66.     return *this;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement