Guest User

Untitled

a guest
Mar 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h> // dla konwersji inta
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. class String{
  10.     int size_of_tab;
  11.     int size_of_text;
  12. public:
  13.     char *text;
  14.  
  15.     String(): size_of_tab(0), size_of_text(0), text(nullptr){}
  16.     String(char* text){
  17.         int size=0;
  18.         while(text[size]!='\0') size++;
  19.         size++;
  20.         this->text= new char[size];
  21.         this->size_of_text=size-1;
  22.         this->size_of_tab=size;
  23.         for(int i=0; i<size; i++){
  24.             this->text[i]=text[i];
  25.         }
  26.     }
  27.     String(const String& copy){
  28.  
  29.         this->size_of_text=copy.size_of_text;
  30.         this->size_of_tab=copy.size_of_tab;
  31.         this->text= new char[this->size_of_tab];
  32.  
  33.         for(int i=0; i<=this->size_of_text; i++){
  34.             this->text[i]=copy.text[i];
  35.         }
  36.     }
  37.     ~String(){
  38.         delete [] text;
  39.     }
  40.  
  41.     String& operator=(const String& string){
  42.         delete [] this->text;
  43.         this->text= new char[string.size_of_text];
  44.         for(int i=0; i<=string.size_of_text; i++){
  45.             this->text[i]=string.text[i];
  46.         }
  47.         this->size_of_text=string.size_of_text;
  48.         this->size_of_tab=string.size_of_tab;
  49.         return *this;
  50.     }
  51.  
  52.  
  53.     String operator+(const String& string){
  54.         String tmp;
  55.         int size=(this->size_of_text + string.size_of_text + 1);
  56.         tmp.text= new char[size];
  57.         tmp.size_of_text=size-1;
  58.         tmp.size_of_tab=size;
  59.         //        strcpy(gdzie, z czego);
  60.         //        strncat(gdzie dodac, z czego, ile znakow);
  61.  
  62.         int i;
  63.         for( i=0 ; i<this->size_of_text; i++){
  64.             tmp.text[i]=this->text[i];
  65.         }
  66.         int j;
  67.         for(j=0; j<string.size_of_text; j++){
  68.             tmp.text[i+j]=string.text[j];
  69.         }
  70.  
  71.         tmp.text[tmp.size_of_text]='\0';
  72.         return tmp;
  73.     }
  74.  
  75.     String operator+(const char *text){
  76.         String tmp;
  77.         int size=0;
  78.         while(text[size]!='\0') size++;
  79.         size++;
  80.  
  81.         size+=this->size_of_text;
  82.         tmp.text= new char[size];
  83.         tmp.size_of_text=size-1;
  84.         tmp.size_of_tab=size;
  85.  
  86.         for(int i=0; i<this->size_of_text; i++){
  87.             tmp.text[i]=this->text[i];
  88.         }
  89.         for(int i=this->size_of_text; i<=tmp.size_of_text; i++){
  90.             tmp.text[i]=text[i-this->size_of_text];
  91.         }
  92.         return tmp;
  93.     }
  94.  
  95.  
  96.     bool operator==(const char *text){
  97.         bool ret=0;
  98.         int size=0;
  99.         while(text[size]!='\0') size++;
  100.         if(this->size_of_text==size) ret=1;
  101.         if(ret==1){
  102.             for(int i=0; i<this->size_of_text; i++){
  103.                 if(this->text[i]!=text[i]){
  104.                     ret=0;
  105.                     break;
  106.                 }
  107.             }
  108.         }
  109.         return ret;
  110.     }
  111.  
  112.     bool operator==(const String& string){
  113.         bool ret=0;
  114.         if(this->size_of_text==string.size_of_text) ret=1;
  115.         if(ret==1){
  116.             for(int i=0; i<this->size_of_text; i++){
  117.                 if(this->text[i]!=string.text[i]){
  118.                     ret=0;
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.         return ret;
  124.     }
  125.  
  126.  
  127.     bool operator>(const String& string){
  128.         int size= ((this->size_of_text > string.size_of_text)? string.size_of_text : this->size_of_text);
  129.         bool ret=1;
  130.         for(int i=0; i<size; i++){
  131.             if(this->text[i] < string.text[i]){
  132.                 ret=0;
  133.                 break;
  134.             }
  135.         }
  136.         //jesli jest szansa ze beda takie same to ma zadecydowac dlugosc
  137.         if(ret==1){
  138.             bool same=1;
  139.             for(int i=0; i<size; i++){
  140.                 if(this->text[i]!=string.text[i]){
  141.                     same=0;
  142.                     break;
  143.                 }
  144.             }
  145.             if(same==1) ret=((this->size_of_text > string.size_of_text) ? 1 : 0);
  146.         }
  147.  
  148.         return ret;
  149.     }
  150.  
  151.     bool operator<(const String& string){
  152.         int size= ((this->size_of_text > string.size_of_text)? string.size_of_text : this->size_of_text);
  153.         bool ret=1;
  154.         for(int i=0; i<size; i++){
  155.             if(this->text[i] > string.text[i]){
  156.                 ret=0;
  157.                 break;
  158.             }
  159.         }
  160.         //jesli jest szansa ze beda takie same to ma zadecydowac dlugosc
  161.         if(ret==1){
  162.             bool same=1;
  163.             for(int i=0; i<size; i++){
  164.                 if(this->text[i]!=string.text[i]){
  165.                     same=0;
  166.                     break;
  167.                 }
  168.             }
  169.             if(same==1) ret=((string.size_of_text > this->size_of_text) ? 1 : 0);
  170.         }
  171.  
  172.         return ret;
  173.     }
  174.  
  175.  
  176.     char& operator[](size_t index){
  177.         if( index > static_cast<size_t>(this->size_of_text)){
  178.             cout<<endl<<"INDEX OUT OF RANGE!"<<endl;
  179.             exit(EXIT_FAILURE);
  180.         }
  181.         return this->text[index];
  182.     }
  183.  
  184.     const char& operator[](size_t index) const {
  185.         if( index > static_cast<unsigned>(this->size_of_text) ){
  186.             cout<<endl<<"INDEX OUT OF RANGE!"<<endl;
  187.             exit(EXIT_FAILURE);
  188.         }
  189.         return this->text[index];
  190.     }
  191.  
  192.     const int& operator()(){
  193.         return this->size_of_text;
  194.     }
  195.  
  196.     int operator()(const char* text){  //zwracajcy pozycje tekstu text ????????????
  197.         int ret=-1;
  198.         int size=0;
  199.         while(text[size]!='\0') size++;
  200.         int i;
  201.         cout<<endl<<text[0]<<this->text[0]<<endl;
  202.         for(i=0; i<this->size_of_text; i++){
  203.             if(text[0]==this->text[i]){
  204.                 for(int j=0; j<size; j++){
  205.                     if(text[j]!=this->text[i+j]  || (i+j)>this->size_of_text ){
  206.                         ret=-1;
  207.                         break;
  208.                     }
  209.                     ret=i;
  210.                 }
  211.             }
  212.             if(ret==i) break;
  213.         }
  214.         return ret;
  215.     }
  216.     //CHYBA CHODZILO O TO V V V V V V V V
  217.     operator const char*(){
  218.         return this->text;
  219.     }
  220.  
  221.  
  222.     operator bool(){
  223.         return this->size_of_text==0? false : true;
  224.     }
  225.     //jawny operator konwersji do std::string
  226.  
  227.     explicit operator std::string() const {
  228.         string tmp;
  229.         for(int i=0; i<= this->size_of_text; i++){
  230.             tmp+=this->text[i];
  231.         }
  232.         return tmp;
  233.     }
  234.  
  235.  
  236.     //preinkrementacja
  237.     String& operator++(){
  238.         this->size_of_text++;
  239.         if(this->size_of_text+1>this->size_of_tab){
  240.             char *copy= this->text;
  241.             this->size_of_tab*=1.5;
  242.             this->text= new char[this->size_of_tab];
  243.  
  244.             for(int i=0; i<this->size_of_text-1; i++){
  245.                 this->text[i]=copy[i];
  246.             }
  247.             delete [] copy;
  248.         }
  249.         this->text[this->size_of_text-1]= 32; // 32 to spacja w ASCII
  250.         this->text[this->size_of_text]='\0';
  251.         return *this;
  252.     }
  253.     //postinkrementacja
  254.     String operator++(int){
  255.         String before_incrementation(*this);
  256.         ++(*this);
  257.         return before_incrementation;
  258.     }
  259.     //predekrementacja
  260.     String& operator--(){
  261.         this->size_of_text--;
  262.         char *copy= this->text;
  263.         this->size_of_tab=this->size_of_text+1;
  264.         this->text= new char[this->size_of_tab];
  265.  
  266.         for(int i=0; i<this->size_of_text; i++){
  267.             this->text[i]=copy[i];
  268.         }
  269.         delete [] copy;
  270.  
  271.         this->text[this->size_of_text]='\0';
  272.         return *this;
  273.     }
  274.     //postdekrementacja
  275.     String operator--(int){
  276.         String before_decrementation(*this);
  277.         --(*this);
  278.         return before_decrementation;
  279.     }
  280.  
  281.  
  282.     String& operator<<(double number){
  283.         char text_number[25];
  284.         sprintf(text_number, "%lf", number);
  285.         int size=0;
  286.         while(text_number[size]!='\0') size++;
  287.         cout<<"ROZMIAR: "<<size<<endl;
  288.         this->size_of_text+=size;
  289.         if(this->size_of_text+1>this->size_of_tab){
  290.             char *copy= this->text;
  291.             while(this->size_of_text+1>this->size_of_tab) this->size_of_tab*=1.5;
  292.             this->text= new char[this->size_of_tab];
  293.  
  294.             for(int i=0; i<this->size_of_text-size; i++){
  295.                 this->text[i]=copy[i];
  296.             }
  297.             delete [] copy;
  298.         }
  299.         for(int i=0; i<size; i++){
  300.             this->text[this->size_of_text-size+i]= text_number[i];
  301.         }
  302.         this->text[this->size_of_text]='\0';
  303.         return *this;
  304.     }
  305.  
  306.     String& operator<<(int number){
  307.         char text_number[20];
  308.         itoa(number,text_number,10);
  309.         int size=0;
  310.         while(text_number[size]!='\0') size++;
  311.  
  312.         this->size_of_text+=size;
  313.         if(this->size_of_text+1>this->size_of_tab){
  314.             char *copy= this->text;
  315.             while(this->size_of_text+1>this->size_of_tab) this->size_of_tab*=1.5;
  316.             this->text= new char[this->size_of_tab];
  317.  
  318.             for(int i=0; i<this->size_of_text-size; i++){
  319.                 this->text[i]=copy[i];
  320.             }
  321.             delete [] copy;
  322.         }
  323.         for(int i=0; i<size; i++){
  324.             this->text[this->size_of_text-size+i]= text_number[i];
  325.         }
  326.         this->text[this->size_of_text]='\0';
  327.         return *this;
  328.     }
  329.  
  330.  
  331.  
  332.     friend std::istream& operator>>(std::istream& is, String& string);
  333.     friend std::ostream& operator<<(std::ostream& os,const String& string);
  334.  
  335. };
  336.  
  337. std::istream& operator>>(std::istream& is, String& string){
  338.     char c;
  339.     char *copy=nullptr;
  340.     string.size_of_text=0;
  341.     string.size_of_tab=2;
  342.     delete [] string.text;
  343.     string.text= new char[string.size_of_tab];
  344.  
  345.     while(is.get(c) && c!='\n'){
  346.         string.size_of_text++;
  347.  
  348.         if(string.size_of_text+1>string.size_of_tab){
  349.             copy=string.text;
  350.             string.size_of_tab*=1.5;
  351.             string.text= new char[string.size_of_tab];
  352.  
  353.             for(int i=0; i<string.size_of_text-1; i++){
  354.                 string.text[i]=copy[i];
  355.             }
  356.             delete [] copy;
  357.         }
  358.  
  359.         string.text[string.size_of_text-1]=c;
  360.         string.text[string.size_of_text]='\0';
  361.     }
  362.     return is;
  363. }
  364.  
  365. std::ostream& operator<<(std::ostream& os, const String& string){
  366.     return os<<string.text;
  367. }
  368.  
  369.  
  370.  
  371. int main()
  372. {
  373.  
  374.     String s2="abca";
  375.  
  376.     string a=(string)s2;
  377.     cout<<a<<endl<<endl;
  378.  
  379.     return 0;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment