Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string.h>
  4.  
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class String {
  10. private:
  11.     char* str;
  12.     int current_size;
  13.     int n; 
  14.        
  15. public:
  16.     String(const String&);
  17.     String();
  18.     ~String();
  19.     char* getStr(istream&);
  20.     String(fstream&);
  21.     bool init(fstream&);
  22.     void show();
  23.     char* getStr()const;
  24.     void strcpy(char*);
  25.     char getElem(int i){ return str[i];}
  26.     char operator[](int i)const{ return str[i];}
  27.     const int getLength()const{ return n;} 
  28.     const int getSize()const{ return current_size;}
  29.     const String& operator=(const String &);
  30.     void concat(char*);
  31.     String operator+(String&);
  32.     bool operator>(const String&); 
  33.     bool operator<(const String&);
  34.     friend ostream &operator<<(ostream &, const String&);
  35. };
  36.  
  37. String::~String(){
  38.     if(str)
  39.         delete[]str;
  40. }
  41.  
  42. bool String::init(fstream& file){
  43.     str = getStr(file);
  44.     if(str)
  45.         return true;
  46.     return false;
  47. }
  48.  
  49.  
  50. bool String::operator>(const String &right){
  51.     return (strcmp(str, right.getStr())>0);
  52. }
  53.  
  54.  
  55. bool String::operator<(const String &right){
  56.     return (strcmp(str, right.getStr())<0);
  57. }
  58.  
  59. const String& String::operator=(const String &right){
  60.     strcpy(right.getStr());
  61.     return *this;
  62. }
  63.  
  64. ostream & operator<<(ostream &output, const String &elem){
  65.     output << elem.getStr() ;
  66.  
  67. }
  68.  
  69.  
  70. String String::operator+(String &right){
  71.     String newString(*this);
  72.     newString.concat(right.getStr());
  73.     return newString;
  74. }
  75.  
  76.  
  77. void String::concat(char* str2){
  78.     int len = strlen(str2);
  79.     if(len <= current_size - n + 1){
  80.         memcpy(&str[n], str2, len+1);
  81.         return ;   
  82.     }
  83.     char *tmp = new char[n];
  84.     memcpy(tmp, str, n);
  85.     delete[] str;
  86.     str = new char[n+len+1];
  87.     memcpy(str, tmp, n);
  88.     memcpy(&str[n], str2, len+1);
  89.     n += len;
  90.     current_size = n;
  91. }
  92.  
  93. char *String::getStr(istream& file){
  94.     const size_t buff = 10;
  95.     current_size = buff;
  96.     char *buffer = new char[current_size];
  97.     n = 0;
  98.     char c = 0;
  99.     while(file.get(c) && c != ' ' && c!= '\n' && !file.eof()){  
  100.         if (n + 1 > current_size){
  101.             current_size *= 2;
  102.             char *new_buffer = new char[current_size];
  103.             memcpy(new_buffer, buffer, n);
  104.             delete [] buffer;
  105.             buffer = new_buffer;
  106.         }
  107.         buffer[n++] = c;
  108.     }
  109.     char *final_buffer = new char[n+1];
  110.     memcpy(final_buffer, buffer, n);
  111.     final_buffer[n] = '\0';
  112.     delete [] buffer;
  113.     if(file.eof() && n == 0)
  114.         return NULL;
  115.     return final_buffer;
  116. }
  117.  
  118. char* String::getStr() const{
  119.     return str;
  120. }
  121.  
  122.  
  123. void String::show() {
  124.     cout << "\nSTR: " << str << "\n n: "<< n << "\n size: " << current_size << "\n\n";
  125.        
  126. }
  127.  
  128. void String::strcpy(char *str2){
  129.     n = strlen(str2);
  130.     current_size = n;
  131.     str = new char[n+1];
  132.     for(int i = 0; i < n; i++)
  133.         str[i] = str2[i];
  134.     str[n] = '\0';
  135. }
  136.  
  137. String::String(const String &oth){
  138.     strcpy(oth.getStr());  
  139. }
  140.  
  141. String::String():
  142. str(NULL), current_size(0), n(0){}
  143.  
  144. String::String(fstream& file){
  145.     n = 0;
  146.     current_size = 0;
  147.     str = getStr(file);
  148. }
  149.  
  150. class Testbench{
  151. private:
  152.     String*  arr;
  153.     int size;
  154.     int curr;
  155. public:
  156.     int getNum(){ return curr; }
  157.     Testbench(int, fstream&);
  158.     ~Testbench();
  159.     void show();
  160.     void sort();
  161.     void swap(String&, String&);
  162.     bool mem(int);
  163.     bool init(fstream&);
  164.     Testbench();
  165.     void concatChet();
  166.     String operator[](int i){ return arr[i]; }
  167. };
  168.  
  169. bool Testbench::mem(int newSize){
  170.     if(arr == NULL){
  171.         arr = new String[newSize];
  172.     }else{ 
  173.         String* tmp = new String[size];
  174.         for(int i = 0; i < size; i++)
  175.             tmp[i] = arr[i];
  176.         delete [] arr;
  177.         arr = new String[newSize];
  178.         for(int i = 0; i < size; i++)
  179.             arr[i] = tmp[i];
  180.         delete [] tmp;
  181.         }
  182.     size = newSize;
  183.     return arr;
  184. }
  185.  
  186. Testbench::~Testbench(){
  187.     if(arr)
  188.         delete [] arr;
  189. }
  190.  
  191. void Testbench::swap(String &str1, String& str2){
  192.     String str3 = str1;
  193.     str1 = str2;
  194.     str2 = str3;
  195. }
  196.  
  197. void Testbench::sort(){
  198.     cout << "\nSORT";
  199.     for(int i = 0; i < curr; i++)
  200.         for(int j = 0; j < curr; j++)
  201.             if(arr[i] < arr[j])
  202.                 swap(arr[i], arr[j]);
  203. }
  204.  
  205. Testbench::Testbench(int N, fstream &file){
  206.     size = N;
  207.     curr = N;
  208.     arr = new String[size];
  209.     for(int i = 0; i < N; i++){
  210.         arr[i].init(file);
  211.     }
  212. }
  213.  
  214. Testbench::Testbench():
  215. arr(NULL), size(0){}
  216.  
  217.  
  218. bool Testbench::init(fstream &file){
  219.     size = 1;
  220.     mem(size);
  221.     curr = 0;
  222.     while(1){
  223.         if(size == curr && mem(size*2));
  224.         /*
  225.         if(size == curr) mem(size*2);
  226.         */
  227.         if(arr[curr++].init(file)) 
  228.             continue;
  229.         curr--;
  230.         break;
  231.     }
  232. }
  233.  
  234.  
  235. void Testbench::show(){
  236.     cout << "\nSHOW\n";
  237.     for(int i = 0; i < curr; i++)
  238.         cout << arr[i] << " ";
  239. }
  240.  
  241.  
  242. void Testbench::concatChet(){
  243.     for(int i = 0; i < getNum() - 1; i+=2){
  244.         arr[i] = arr[i] + arr[i+1];
  245.     }
  246. }
  247.  
  248. int main(int argc, char* argv[]) {
  249.     fstream file("./test");
  250.     Testbench tb;
  251.     tb.init(file);
  252.     tb.show();
  253.     tb.sort();
  254.     tb.show();
  255.     tb.concatChet();
  256.     tb.show();
  257.     file.close();
  258.     return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement