Advertisement
heroys6

Quintessention

Jul 24th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cctype>
  5. using namespace std;
  6.  
  7. class str_type {
  8.     char string[80];
  9. public:
  10.     str_type(char *str = "") { strcpy(string, str); }
  11.     str_type operator+(str_type op_r);
  12.     str_type operator+(char *str);
  13.     str_type operator=(str_type op_r);
  14.     str_type operator=(char *str);
  15.     str_type operator-(str_type op_r);
  16.     void show() { cout << string; }
  17.     friend str_type operator+(char *str, str_type obj);
  18. };
  19.  
  20. str_type str_type::operator+(str_type op_r)
  21. {
  22.     str_type temp;
  23.     strcpy(temp.string, string);
  24.     strcat(temp.string, op_r.string);
  25.     return temp;
  26. }
  27.  
  28. str_type str_type::operator+(char *str)
  29. {
  30.     str_type temp;
  31.     strcpy(temp.string, string);
  32.     strcat(temp.string, str);
  33.     return temp;
  34. }
  35.  
  36. str_type str_type::operator=(str_type op_r)
  37. {
  38.     strcpy(string, op_r.string);
  39.     return *this;
  40. }
  41.  
  42. str_type str_type::operator=(char *str)
  43. {
  44.     strcpy(string, str);
  45.     return *this;
  46. }
  47.  
  48. str_type str_type::operator-(str_type op_r)
  49. {
  50.     str_type temp;
  51.     char *s_ptr = string;
  52.     char BUF[80];
  53.     int buf_i = 0;
  54.  
  55.     while (*s_ptr != '\0') // It runs until the end of LEFT OBJECT string
  56.     {
  57.         while (isalpha(*s_ptr)) // Scaning one word
  58.         {
  59.             BUF[buf_i] = *s_ptr;
  60.             buf_i++;
  61.             s_ptr++;
  62.         }
  63.         if (buf_i > 0) // If word
  64.             BUF[buf_i] = '\0';
  65.         else {        // If symbol
  66.             BUF[buf_i] = *s_ptr;
  67.             BUF[buf_i + 1] = '\0';
  68.         }
  69.         if (strcmp(op_r.string, BUF)) // Compare word with RIGHT OBJECT string
  70.             strcat(temp.string, BUF);
  71.  
  72.         if (buf_i == 0) s_ptr++; // Next pointer position if current position is symbol
  73.         buf_i = 0; // Reset buffer for writing
  74.     }
  75.  
  76.     return temp;
  77. }
  78.  
  79. str_type operator+(char *str, str_type obj)
  80. {
  81.     str_type temp;
  82.     strcat(temp.string, str);
  83.     strcat(temp.string, obj.string);
  84.  
  85.     return temp;
  86. }
  87.  
  88. int main()
  89. {
  90.     str_type ob1("It is very-very hard test"), ob2("very"), ob_res;
  91.    
  92.     ob_res = ob1 - ob2;
  93.  
  94.     cout << "Source: "; ob1.show();
  95.     cout << "\nDelete from source: "; ob2.show();
  96.     cout << "\nResult: "; ob_res.show();
  97.  
  98.     cout << "\n\n";
  99.     str_type newob("Added: ");
  100.     newob = newob + "First";
  101.     newob.show();
  102.  
  103.     cout << "\n\n";
  104.     newob = "Also " + newob;
  105.     newob.show();
  106.  
  107.     cout << "\n";
  108.     system("pause");
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement