Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 7.64 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. user defined string::reserve()
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <string>
  5. #include "mystring.h"
  6. #define string mystring
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void check (string s, string name){
  12.     cout << "checking " << name << endl;
  13.     cout << name << " contains " << s << endl;
  14.     cout << name << " capacity() is " << s.capacity() << endl;
  15.     cout << name << " length() is " << s.length() << endl;
  16.     cout << name << " size() is " << s.size() << endl;
  17.     cout << name << " max_size() is " << s.max_size() << endl << endl;
  18.  
  19. }
  20.  
  21. int main(int argc, char** argv) {
  22.  
  23.     cout<<"This is Lab 5"<<endl;
  24.  
  25.     string s1("Hello, World!");//step 2
  26.     string s1name("s1");//step 3
  27.  
  28.     check(s1,s1name);//step 4
  29.     check(s1,s1name);//step 5
  30.  
  31.     cout << "---Testing assignment operator---nn";
  32. {
  33. string s2;
  34. s2=s1;
  35. string s2name("s2");
  36. check(s2,s2name);
  37. if(s1==s2)
  38.     cout<<"comparison truen";
  39. else
  40.     cout<<"comparison falsen";
  41. }
  42.  
  43.     check(s1,s1name);
  44.  
  45.  
  46.  
  47.     string s3("check assignment");
  48.     s3=s3;//checking to see if operator= is used when they are the same object.
  49.  
  50.     check(s3,"s3");
  51.  
  52.     cout<<"Lab 5 ends"<<endl;//step6
  53.  
  54.  
  55.  
  56. //    //clear check
  57. //    s3.clear();
  58. //    check(s3,"s3");
  59.  
  60.  
  61. //    if(s1==s3)
  62. //        cout<<"comparison truen";
  63. //    else
  64. //        cout<<"comparison falsen";
  65.  
  66. // reserve check
  67. //    mystring::size_type res;
  68. //    res=40;
  69.  
  70.     s3.reserve(40);//still working on reserve
  71.     check(s3,"s3");
  72.  
  73.     cout<<"in main buf size"<<s3.capacity()<<endl;
  74.  
  75.     s3.reserve(5);
  76.     check(s3,"s3");
  77.  
  78.  
  79.  
  80. //    char* test=s3.begin();
  81. //    cout<<&test<<endl;
  82. //    cout<<&s3<<endl;
  83.  
  84.  
  85. //empty check
  86. //    string s4;
  87. //    
  88. //    if (s4.empty())
  89. //        cout<<"Empty is truen";
  90. //    else
  91. //        cout<<"Empty is falsen";
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. #ifndef MYSTRING_H
  107. #define MYSTRING_H
  108.  
  109. #include <iostream>
  110. #include <math.h>
  111. #include <cstring>
  112.  
  113. using namespace std;
  114.  
  115. class mystring {
  116. public:
  117.     // types with scope in this class
  118.     typedef unsigned int size_type;
  119.     typedef char * iterator;
  120.     typedef const char * const_iterator;
  121.     static const long int npos = 1073741824;
  122.  
  123.     // default constructor
  124.     mystring();//good
  125.     // other constructors
  126.     mystring(const char *);//good
  127.     // copy constructor
  128.     mystring(const mystring& orig);//
  129.  
  130.     // destructor
  131.     ~mystring();////
  132.  
  133.     // iterators
  134.  
  135.     iterator begin();//good
  136.     iterator end();//good
  137.  
  138.     //=== memory related ===
  139.  
  140.     // change buffer size to n
  141.     void reserve(size_type n);
  142.  
  143.     size_type size() const;////good returns len
  144.     size_type length() const;////good returns len
  145.     size_type capacity() const;////good returns buf_size
  146.     size_type max_size() const;////good
  147.     bool empty() const;////good
  148.  
  149.     //=== overloading operators ===
  150.  
  151.      // assignment operator
  152.     mystring& operator=(const mystring&);////
  153. //    mystring& operator=(const char *);
  154.  
  155.     // array notation
  156.  
  157.     char operator[](size_type pos) const;
  158.     char& operator[](size_type pos);
  159.  
  160.     // append
  161.  
  162.     mystring& operator+=(const mystring& str);
  163.     mystring& operator+=(const char * str);
  164.  
  165.     //=== methods that modifiy the string
  166.  
  167.     void clear();////good
  168.  
  169.     void push_back(char c);
  170.  
  171.  
  172.     mystring& append(const mystring& str);
  173.     mystring& append(const char * str);
  174.  
  175.     mystring& insert(size_type pos, const mystring& str);
  176.     mystring& insert(size_type pos, const char * str);
  177.  
  178.     mystring& replace(size_type start, size_type span, const mystring& str);
  179.     mystring& replace(size_type start, size_type span, const char * str);
  180.  
  181.     //=== conversion to c string
  182.  
  183.     const char * c_str() const;//
  184.  
  185.  
  186. private:
  187.     // pointer to the memory location where string is stored as a c-style
  188.     // string
  189.     char * ptr_buffer;
  190.     // the size of the memory in terms of bytes or characters capable of going into it currently
  191.     size_type buf_size;
  192.     // number of characters currently in the memory not including the
  193.     // terminating null character
  194.     size_type len;
  195.  
  196.  
  197. };
  198.  
  199.  
  200.  
  201.  
  202. #include "mystring.h"
  203.  
  204. // default constructor provided for lab 5
  205. mystring::mystring() {
  206.     ptr_buffer = new char[1];
  207.     *ptr_buffer = '';
  208.     buf_size = 1;
  209.     len = 0;
  210. }
  211.  
  212. // constructor from c-style string or "abc" provided for lab 5
  213. mystring::mystring(const char * s) {
  214. len = strlen(s);
  215. buf_size = len + 1;
  216. ptr_buffer = new char[buf_size];
  217. strcpy(ptr_buffer, s);
  218.  
  219. }
  220.  
  221. // copy constructor to be implemented in lab 5
  222. mystring::mystring(const mystring& orig) {
  223.  
  224.     len=orig.length();
  225.     ptr_buffer=new char[len+1];
  226.     buf_size=len+1;
  227.  
  228.     for(int n=0 ;n<buf_size; n++ )
  229.     {
  230.         ptr_buffer[n]=orig.ptr_buffer[n];
  231.     }
  232.     ptr_buffer[buf_size]='';
  233. }
  234.  
  235.  
  236.  
  237. void mystring::reserve(size_type n)
  238. {
  239.     cout<<"cccccc:"<<capacity()<<endl;
  240.     if( n > capacity() )
  241. {
  242.     const char* temp = ptr_buffer;
  243.     ptr_buffer = new char[n];
  244.     memcpy(ptr_buffer, temp, len+1);
  245.     delete [] temp;
  246.     buf_size=n;
  247.     cout<<"bbbbbuf size"<<buf_size<<endl;
  248. }
  249.  
  250.  
  251.  
  252. //    char *temp;
  253. //    
  254. //    temp=new char[n];
  255. //    
  256. //    int i=0;
  257. //    
  258. //    for(;i<=len;i++)
  259. //    {
  260. //        temp[i]=ptr_buffer[i];
  261. //        
  262. //    }
  263. //    buf_size=n;
  264. //    
  265. //    delete [] ptr_buffer;
  266. //    
  267. //    ptr_buffer=temp;
  268. //    
  269.  
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278. mystring::iterator mystring::begin()//think is working correctly
  279. {
  280.     iterator it=ptr_buffer;
  281.  
  282.     return it;
  283. }
  284.  
  285. mystring::iterator mystring::end()//think is working correctly
  286. {
  287.     iterator it=ptr_buffer+len;
  288.  
  289.     return it;
  290. }
  291.  
  292.  
  293.  
  294. // one of the over loaded assignment operator to be implemented // assignment 3 (or for lab 5 if you have more time)
  295. mystring& mystring::operator=(const mystring& orig){
  296.     if(this!=&orig)
  297. //    {
  298. //        cout<<"this==&mystring  if statment activatedn";//comment out after testing
  299. //        break;
  300. //    }
  301.     {
  302.     delete this->ptr_buffer;
  303.  
  304.     this->len=orig.len;//length();
  305.     this->ptr_buffer=new char((this->len)+1);
  306.     this->buf_size=(this->buf_size)+1;
  307.     cout<<"Using assignment operator="<<endl;
  308.     for(int n=0;n<this->buf_size;n++)
  309.     {
  310.         this->ptr_buffer[n]=orig.ptr_buffer[n];
  311.     }
  312.     this->ptr_buffer[buf_size]='';
  313.     return *this;
  314.  
  315.     }
  316. }
  317.  
  318. // some simple methods provided for lab 5
  319.  mystring::size_type mystring::size() const {
  320.      return len;
  321.  }
  322.  mystring::size_type mystring::length() const{
  323.      return len;
  324.  }
  325.  mystring::size_type mystring::capacity() const{
  326.        return buf_size;
  327.  }
  328.  mystring::size_type mystring::max_size() const{
  329.        return (int)pow(2,30) -4 ;
  330.  }
  331.  
  332.  
  333.  
  334.  
  335.  bool mystring::empty() const
  336.  {
  337.      if(len==0)
  338.          return true;
  339.      else
  340.          return false;
  341.  }
  342. // destructor to free space implemented for lab 5
  343. mystring::~mystring() {
  344.     delete [] ptr_buffer;
  345. }
  346. // provided for lab 5 so we may cout mystring
  347. ostream& operator<<(ostream& out, const mystring& str) {
  348.     out << str.c_str();
  349.     return out;
  350. }
  351. // provided for lab 5 to support the implementation of <<
  352. const char * mystring::c_str() const {
  353.     return ptr_buffer;
  354. }
  355.  
  356.  
  357.  
  358. char mystring::operator[](size_type pos) const
  359. {
  360.     return *(ptr_buffer+pos);
  361.  
  362.  
  363.  
  364. }
  365.  
  366.  
  367.  
  368.  
  369. void mystring::clear()
  370. {
  371.     char *temp;
  372.     temp=new char[1];
  373.     temp[0]='';
  374.  
  375.     ptr_buffer=temp;
  376.  
  377.  
  378.     buf_size=0;
  379.     len=0;
  380. }
  381.  
  382.  
  383.  
  384.  
  385.  void mystring::push_back(char c)
  386.  {
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  }
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. bool operator==(const mystring& lhs, const mystring& rhs)
  401. {
  402.    if(lhs.length()==rhs.length())
  403.    {
  404.        for(int i=0; i<lhs.length();i++)
  405.        {
  406.            if(lhs[i]!=rhs[i])
  407.                return false;
  408.  
  409.        }
  410.        return true;
  411.    }
  412.  
  413.    return false;
  414.  
  415. }
  416.        
  417. if( n > capacity() )
  418. {
  419.     const char* temp = ptr_buffer;
  420.     buf_size = n;
  421.     ptr_buffer = new char[buf_size];
  422.     memcpy(ptr_buffer, temp, length()+1);
  423.     delete [] temp;
  424. }
  425.        
  426. void mystring::reserve(size_type n)
  427. {
  428.     if (n <= buf_size)
  429.     {
  430.         return;
  431.     }
  432.        
  433. if (n <= len + 1) // remember the terminator is not included in len
  434.     {
  435.         return;
  436.     }