Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. //--------------- MYSTRING.CPP ---------------
  2. // The class definition for fractions.
  3. //
  4. // Michael Arciola
  5.  
  6. #include "mystring.h"
  7. #include <typeinfo>
  8.  
  9.  
  10. MyString::MyString() // -------------------------------------------------- // #1
  11. {
  12. length = 0;
  13. data = new char[length + 1];
  14. data[0] = '';
  15. }
  16.  
  17. MyString::MyString(const char* s1)
  18. {
  19. length = strlen(s1);
  20. data = new char[length + 1];
  21.  
  22. for(int i = 0; i < length; i++) {
  23. data[i] = s1[i];
  24. //cout << s1[i];
  25. //cout << data[i];
  26. }
  27.  
  28. data[length + 1] = '';
  29. }
  30.  
  31. MyString::MyString(int s1)
  32. {
  33. int temp;
  34. char c = 'm';
  35. MyString final;
  36. int count = 0;
  37. length = 0;
  38. data = new char[length + 1];
  39. int s2;
  40. s2 = s1;
  41.  
  42. while (s1 != 0)
  43. {
  44. temp = s1 % 10;
  45. s1 = s1 / 10;
  46. c = temp + '0';
  47. count++;
  48. }
  49.  
  50. length = count;
  51.  
  52. for (int i = length - 1; i >= 0; i--)
  53. {
  54. temp = s2 % 10;
  55. s2 = s2 / 10;
  56. data[i] = temp + '0';
  57. }
  58.  
  59.  
  60. data[count] = '';
  61.  
  62. }
  63.  
  64. // Destructor ----------------------------------------------------------- // #2
  65. MyString::~MyString()
  66. {
  67. delete [] data;
  68. }
  69.  
  70. // Copy Constructor
  71. MyString::MyString(const MyString& s1)
  72. {
  73. length = s1.length;
  74. data = new char[length + 1];
  75.  
  76. for (int i = 0; i < length; i++)
  77. {
  78. data[i] = s1.data[i];
  79. }
  80.  
  81. data[length + 1] = '';
  82. }
  83.  
  84. // Assignment Operator
  85. MyString& MyString::operator=(const MyString& s1)
  86. {
  87. if (this != &s1)
  88. {
  89. delete [] data;
  90.  
  91. length = s1.length;
  92. data = new char[length + 1];
  93.  
  94. for (int i = 0; i < length; i++)
  95. {
  96. data[i] = s1.data[i];
  97. }
  98.  
  99. data[length + 1] = '';
  100. }
  101.  
  102. return *this;
  103. }
  104.  
  105. ostream& operator<< (ostream& os, const MyString& s1) // --------------- -// #3
  106. {
  107. for (int i = 0; i < s1.length; i++)
  108. {
  109. os << s1.data[i];
  110. }
  111.  
  112. return os;
  113. }
  114.  
  115. istream& operator>> (istream& is, MyString& s1)
  116. {
  117. // NEED HELP
  118.  
  119. /*
  120. is >> s1;
  121.  
  122. return is;
  123. */
  124. }
  125.  
  126. istream& getline (istream& is, MyString& s1, char delim)
  127. {
  128. // NEED HELP
  129. }
  130.  
  131. bool operator< (const MyString& s1, const MyString& s2) // ------------- // #4
  132. {
  133. if (strcmp(s1.data, s2.data) < 0) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139.  
  140. bool operator> (const MyString& s1, const MyString& s2)
  141. {
  142. return s2 < s1;
  143. }
  144.  
  145. bool operator<=(const MyString& s1, const MyString& s2)
  146. {
  147. return !(s2<s1);
  148. }
  149.  
  150. bool operator>=(const MyString& s1, const MyString& s2)
  151. {
  152. return !(s1<s2);
  153. }
  154.  
  155. bool operator==(const MyString& s1, const MyString& s2)
  156. {
  157. return (s1<=s2) && (s1>=s2);
  158. }
  159.  
  160. bool operator!=(const MyString& s1, const MyString& s2)
  161. {
  162. return !(s1==s2);
  163. }
  164.  
  165. MyString operator+ (const MyString& s1, const MyString& s2) // ------------ // #5
  166. {
  167. MyString newstring;
  168. int len = s1.length + s2.length;
  169.  
  170. newstring.grow(len);
  171.  
  172. for (int i = 0; i < s1.length; i++)
  173. {
  174. newstring.data[i] = s1.data[i];
  175. }
  176.  
  177. for (int i = 0; i < s2.length; i++)
  178. {
  179. newstring.data[s1.length + i] = s2.data[i];
  180. }
  181.  
  182.  
  183. return newstring;
  184. }
  185.  
  186. MyString& MyString::operator+=(const MyString& s1)
  187. {
  188. int start = length;
  189. int growth = s1.length;
  190. int totallen = s1.length + length;
  191.  
  192. grow(growth, start);
  193.  
  194. cout << "Start: " << start << endl;
  195. cout << "Length: " << length << endl;
  196.  
  197. for (int i = start; start < length; i++)
  198. {
  199. cout << "data[i]: " << data[i] << endl;
  200. cout << "s1.data[i]: " << s1.data[i] << endl;
  201. data[i] = s1.data[i];
  202. }
  203. }
  204.  
  205. char& MyString::operator[] (unsigned int index) // ------------------------ // #6
  206. {
  207. return data[index];
  208. }
  209.  
  210. const char& MyString::operator[] (unsigned int index) const
  211. {
  212. return data[index];
  213. }
  214.  
  215. int MyString::getLength() const // ------------------------------------------ // #7
  216. {
  217. return length;
  218. }
  219.  
  220. const char* MyString::getCString() const
  221. {
  222. return data;
  223. }
  224.  
  225. MyString MyString::substring(unsigned int start) const // ------------------------- // #8
  226. {
  227. MyString temp;
  228.  
  229. for (int i = start; i < length; i++)
  230. {
  231. temp.data[i] = data[i];
  232. cout << temp.data[i];
  233. }
  234.  
  235. return temp;
  236. }
  237.  
  238. MyString MyString::substring(unsigned int start, unsigned int size) const
  239. {
  240. MyString temp;
  241.  
  242. if (size + start > length) {
  243. size = length;
  244. } else {
  245. size += start;
  246. }
  247.  
  248. for (int i = start; i < size; i++)
  249. {
  250. if (i != size)
  251. {
  252. temp.data[i] = data[i];
  253. cout << temp.data[i];
  254. }
  255. }
  256.  
  257. return temp;
  258. }
  259.  
  260. MyString& MyString::insert(unsigned int index, const MyString& s) // ---------- // #9
  261. {
  262. if (index > length) {
  263. index = length;
  264. }
  265.  
  266. grow(s.length, index);
  267. int len = s.length;
  268. int ph = 0;
  269.  
  270. for (int i = index; i < index + len; i++) {
  271. data[i] = s.data[ph];
  272. ph += 1;
  273. }
  274. }
  275.  
  276. int MyString::indexOf(const MyString& s) const // ------------------------------// #10
  277. {
  278. int final;
  279.  
  280. if (strstr(data, s.data) == 0) {
  281. final = -1;
  282. } else {
  283. final = strstr(data, s.data) - data;
  284. }
  285.  
  286. return final;
  287. }
  288.  
  289. void MyString::grow(int len) {
  290.  
  291. char* temp = new char[length + len + 1];
  292.  
  293. for (int i = 0; i < length; i++)
  294. {
  295. temp[i] = data[i];
  296. }
  297.  
  298. /* FOR TESTING
  299. for (int i = length; i < length + len; i++)
  300. {
  301. temp[i] = 'm';
  302. }
  303. */
  304.  
  305. temp[length + len + 1] = '';
  306.  
  307. data = temp;
  308. length = length + len + 1;
  309. }
  310.  
  311. void MyString::grow(int len, int index) {
  312.  
  313. char* temp = new char[length + len + 1];
  314. int ph = 0;
  315.  
  316. for (int i = 0; i < index; i++)
  317. {
  318. temp[i] = data[i];
  319. ph += 1;
  320. }
  321.  
  322. for (int i = index; i < index + len; i++)
  323. {
  324. temp[i] = ' ';
  325. }
  326.  
  327. for (int i = index + len; i < length + len + 1; i++)
  328. {
  329. temp[i] = data[ph];
  330. ph += 1;
  331. }
  332.  
  333. temp[length + len + 1] = '';
  334.  
  335. data = temp;
  336. length = length + len + 1;
  337. }
  338.  
  339. void MyString::reverse() {
  340.  
  341. char temp;
  342.  
  343. for (int i = 0; i < length/2; i++) { // Loops through array, add value to temp, and move it to the other side of the array
  344. temp = data[i];
  345. data[i] = data[length - i -1];
  346. data[length - i -1] = temp;
  347. cout << data[i] << endl;
  348.  
  349. }
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement