0% found this document useful (0 votes)
88 views5 pages

CSC161 Exam 1 Listings

This document defines a String class with member functions for constructing, modifying, accessing elements of strings, reserving memory, and comparing strings. It also defines non-member operator overloading functions for concatenation, input/output, and comparisons. The document provides an implementation of the String class methods and a test driver program to demonstrate the class functionality.

Uploaded by

Blake Mills
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views5 pages

CSC161 Exam 1 Listings

This document defines a String class with member functions for constructing, modifying, accessing elements of strings, reserving memory, and comparing strings. It also defines non-member operator overloading functions for concatenation, input/output, and comparisons. The document provides an implementation of the String class methods and a test driver program to demonstrate the class functionality.

Uploaded by

Blake Mills
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

// CSC-161 Exam One


2. // FILE: mystring.h
3. // <comments removed for sake of listing - available in textbook>
4. #ifndef MYSTRING_H
5. #define MYSTRING_H
6. #include <cstdlib> // Provides size_t
7. using namespace std;
8. namespace csc_161_exam_one
9. {
10. class String
11. {
12. public:
13. // CONSTRUCTORS and DESTRUCTOR
14. String(const char str[ ] = "");
15. String(const String& source);
16. ~String( );
17. // MODIFICATION MEMBER FUNCTIONS
18. void operator +=(const String& addend);
19. void operator +=(const char addend[ ]);
20. void operator +=(char addend);
21. void reserve(size_t n);
22. void operator =(const String& source);
23. // CONSTANT MEMBER FUNCTIONS
24. size_t length( ) const { return current_length; }
25. char operator [ ](size_t position) const;
26. // FRIEND FUNCTIONS
27. friend ostream& operator <<(ostream& outs, const String& source);
28. friend bool operator ==(const String& s1, const String& s2);
29. friend bool operator !=(const String& s1, const String& s2);
30. friend bool operator >=(const String& s1, const String& s2);
31. friend bool operator <=(const String& s1, const String& s2);
32. friend bool operator > (const String& s1, const String& s2);
33. friend bool operator < (const String& s1, const String& s2);
34. private:
35. char *sequence;
36. size_t allocated;
37. size_t current_length;
38. };
39. // NON-MEMBER FUNCTIONS for the String class
40. String operator +(const String& s1, const String& s2);
41. istream& operator >>(istream& ins, String& target);
42. void getline(istream& ins, String& target);
43. }
44. #endif
45. // CSC-161 Exam One
46. // FILE: Modified mystring.cxx
47. // CLASS implemented: String (see mystring.h for documentation)
48. // INVARIANT for the String ADT:
49. // <removed for exam>
50. // <several Includes removed from listing>
51. #include "mystring.h"
52. namespace csc_161_exam_one
53. {
54. String::String(const char str[ ])
55. {
56. size_t need = strlen(str) + 1;
57. sequence = new char[need];
58. strcpy(sequence, str);
59. allocated = need;
60. current_length = need-1;
61. }
62. String::String(const String& source)
63. {
64. sequence = new char[source.allocated];
65. strcpy(sequence, source.sequence);
66. allocated = source.allocated;
67. current_length = source.current_length;
68. }
69. String::~String( )
70. {
71. delete [ ] sequence;
72. sequence = 0;
73. allocated = 0;
74. current_length = 0;
75. }
76. char String::operator [ ](size_t position) const
77. {
78. assert(position < current_length);
79. return sequence[position];
80. }
81. void String::operator =(const String& source)
82. {
83. reserve(source.current_length);
84. strcpy(sequence, source.sequence);
85. current_length = source.current_length;
86. }
87. void String::operator +=(const String& addend)
88. {
89. reserve(current_length + addend.current_length);
90. strcat(sequence, addend.sequence);
91. current_length += addend.current_length;
92. }
93. void String::operator +=(const char addend[ ])
94. {
95. size_t addend_length = strlen(addend);
96. reserve(current_length + addend_length);
97. strcat(sequence, addend);
98. current_length += addend_length;
99. }
100. void String::operator +=(char addend)
101. {
102. reserve(current_length + 1);
103. sequence[current_length] = addend;
104. current_length++;
105. sequence[current_length] = '\0';
106. }
107. void String::reserve(size_t n)
108. {
109. char *new_sequence;
110. if (allocated > n) return;
111. new_sequence = new char[n + 1];
112. strcpy(new_sequence, sequence);
113. delete [ ] sequence;
114. sequence = new_sequence;
115. allocated = n + 1;
116. }
117. String operator +(const String& s1, const String& s2)
118. {
119. String answer(s1);
120. answer += s2;
121. return answer;
122. }
123. ostream& operator <<(ostream& outs, const String& source)
124. {
125. outs << source.sequence;
126. return outs;
127. }
128. bool operator ==(const String& s1, const String& s2)
129. {
130. return strcmp(s1.sequence, s2.sequence) == 0;
131. }
132. // <remaining comparison operators left off of listing>
133. istream& operator >>(istream& ins, String& target)
134. {
135. char next; // Next character read from the input stream
136. // Skip any whitespace
137. while (!ins.eof( ) && isspace(ins.peek( )))
138. ins.ignore( );
139. // Set the string to the empty string and read the new string
140. target = "";
141. while (!ins.eof( ) && !isspace(ins.peek( )))
142. {
143. ins >> next;
144. target += next;
145. }
146. return ins;
147. }
148. }
149. // CSC-161 Exam One
150. // FILE: Modified stringtest.cxx
151. // An interactive test program for the new String class
152. #include "mystring.h" // Provides String class
153. using namespace std;
154. using namespace csc_161_exam_one;
155. int main( )
156. {
157. cout << "CSC-161 Exam One Test Program " << endl;
158. String s1, s2, s3("CSC-161 Exam One");
159. char choice;
160. size_t i;
161. size_t n;
162. cout << "\n(Strings s1 and s2 are initially empty while " << endl;
163. cout << "a third String s3 is set to \"CSC-161 Exam One\".)" << endl;
164. do
165. {
166. print_menu( );
167. choice = toupper(get_user_command( ));
168. switch (choice)
169. {
170. case '1':
171. cout << "Please enter a String value for s1: ";
172. getline(cin, s1);
173. break;
174. case '2':
175. cout << "Please enter a String value for s2: ";
176. getline(cin, s2);
177. break;
178. case '3':
179. cout << "Please enter a String value for s3: ";
180. getline(cin, s3);
181. break;
182. case 'C': value_parameter_test(s1); break;
183. case 'L':
184. cout << "String lengths are: "
185. << s1.length( ) << ' ' << s2.length( ) << ' ' << s3.length( )
186. << endl;
187. break;
188. case '!':
189. cout << "Characters of s1 are: ";
190. for (i = 0; i < s1.length( ); ++i)
191. cout << s1[i] << ' ';
192. cout << endl;
193. break;
194. case 'X': s1 += 'X'; break;
195. case 'Y': s1 += "YYY"; break;
196. case 'Z': s1 += s3; break;
197. case '+': value_parameter_test(s2+s3);
198. case 'R':
199. cout << "Please type a new reserve size for s1: ";
200. cin >> n;
201. s1.reserve(n);
202. break;
203. case 'I':
204. cout << "Please enter a String value for s1: ";
205. cin >> s1;
206. break;
207. case 'P':
208. cout << "s1: " << s1 << endl;
209. cout << "s2: " << s2 << endl;
210. cout << "s3: " << s3 << endl;
211. break;
212. case '=':
213. cout << "s1 == s2 is " << ((s1 == s2) ? "true" : "false") << endl;
214. cout << "s1 != s2 is " << ((s1 != s2) ? "true" : "false") << endl;
215. cout << "s1 <= s2 is " << ((s1 <= s2) ? "true" : "false") << endl;
216. cout << "s1 >= s2 is " << ((s1 >= s2) ? "true" : "false") << endl;
217. cout << "s1 < s2 is " << ((s1 < s2) ? "true" : "false") << endl;
218. cout << "s1 > s2 is " << ((s1 > s2) ? "true" : "false") << endl;
219. break;
220. case 'Q': cout << "Ridicule is the best test of truth." << endl;
221. break;
222. default: cout << choice << " is invalid." << endl;
223. }
224. }
225. while ((choice != 'Q'));
226. return EXIT_SUCCESS;
227. }

You might also like