Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. // Lab_5.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cstdio>
  7.  
  8. using namespace std;
  9.  
  10. template <int max_size, class T> class my_string {
  11. public:
  12.     my_string () {
  13.         this->size = 0;
  14.     }
  15.  
  16.     my_string (const char* source_string) {
  17.         this->size = strlen(source_string);
  18.         for (int i = 0; i < strlen(source_string); i++) {
  19.             this->text[i] = source_string[i];
  20.         }
  21.         this->text[strlen(source_string)] = '\0';
  22.     }
  23.  
  24.     ~my_string () {
  25.         delete [] this->text;
  26.     }
  27.  
  28.     T get_length () const {
  29.         return size;
  30.     }
  31.  
  32.     my_string (const string& source_string) {
  33.         this->size = source_string->size;
  34.         strcpy(this, source_string->text);
  35.     }
  36.  
  37.     char get_char (int index) const {
  38.         return text[index];
  39.     }
  40.  
  41.     void set_char (char temp, int position) {
  42.         this->text[position] = temp;
  43.         this->size++;
  44.     }
  45.  
  46.     void read_string () {
  47.         int i = 0;
  48.         char temp;
  49.         do {
  50.             temp = getchar();
  51.             if (i < max_size) {
  52.                 this->text[i] = temp;
  53.                 i++;
  54.             }
  55.         }
  56.         while (temp != '\n');
  57.         text[i - 1] = '\0';
  58.     }
  59.  
  60.     my_string <max_size, T> operator+(const my_string& s) {
  61.         my_string <max_size, T> res;
  62.         int count = 0;
  63.         while (this->get_char(count) != '\0') {
  64.             res.set_char(this->get_char(count), count);
  65.             count++;
  66.         }
  67.  
  68.         for(int i = 0; count < max_size; i++){
  69.             if (s.get_char(i) == '\0') { res.set_char('\0', count); break; }
  70.             res.set_char(s.get_char(i), count);
  71.             count++;
  72.         }
  73.         return res;
  74.     }
  75.  
  76.     my_string& operator=(const my_string& s){
  77.         for (int i = 0; i < size; i++){
  78.             this->set_char(i, s.get_char(i));
  79.         }
  80.         return *this;
  81.     }
  82.    
  83.     void print() {
  84.         cout << text << endl;
  85.     }
  86. private:
  87.     char text[max_size];
  88.     T size;
  89. };
  90.  
  91. int _tmain(int argc, _TCHAR* argv[]) {
  92.     char* X = "some text";
  93.     char* Y = ", new text!";
  94.  
  95.     my_string<64, int> *S1 = new my_string<64, int>(X);
  96.     my_string<64, int> *S2 = new my_string<64, int>(Y);
  97.     my_string<64, int> *S3 = new my_string<64, int>();
  98.    
  99.     *S3 = *S1 + *S2;
  100.     //S1->print();
  101.     //S2->print();
  102.     //S3->print();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement