Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
- class Oracion {
- public:
- char* a;
- Oracion();
- Oracion(const Oracion& o);
- Oracion& operator + (const Oracion& other);
- Oracion& operator - (const Oracion& other);
- Oracion& operator = (const Oracion& other);
- bool operator == (const Oracion& other);
- friend ostream& operator << (ostream& o, const Oracion& other);
- friend istream& operator << (istream& i, const Oracion& other);
- };
- Oracion& Oracion::operator = (const Oracion& other) {
- if (*this == other)
- return *this;
- char* aux = strdup(other.a);
- if (!aux) return *this;
- free(this->a);
- this->a = aux;
- return *this;
- }
- Oracion& Oracion::operator - (const Oracion& other) {
- size_t this_size = strlen(this->a);
- size_t other_size = strlen(other.a);
- size_t minimum_size = this_size > other_size ? this_size : other_size;
- size_t matches = 0;
- size_t removed = 0;
- for (size_t i = 0; i < minimum_size; i++) {
- if (this->a[i] != other.a[i]) {
- matches = 0;
- continue;
- }
- matches++;
- if (matches == other_size) {
- for (size_t j = 0; j < other_size; j++){
- this->a[i - j] == '*';
- removed++;
- }
- matches = 0;
- }
- }
- char* aux = (char*)malloc(sizeof(char) * (this_size - removed));
- if (!aux)
- return *this;
- size_t pos_actual = 0;
- for (size_t i = 0; i < this_size; i++) {
- if (this->a[i] != '*') {
- aux[pos_actual] = this->a[i];
- pos_actual++;
- }
- }
- aux[pos_actual] = '\0';
- free(this->a);
- this->a = aux;
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement