Algorithm Development

Hi all,

I'am trying to self-study some programming topics that were not covered in any of my previous programming classes. I would like to learn/review some of the topics that were covered in those classes and get a better understanding of what is really going on with the code. I am trying to write a program that uses classes, pointers and overloaded operators. I would really appreciate it if someone could guide me as to what the author of this problem is really asking for. I do not want any source code, but more so, just some advice as how to approach the problem. Any and all help is greatly appreciated. Thank you for your time.

The program that I am asked to write is as follows:

Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dynamic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using the '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting of words separated by blanks. Enforce the restriction that the array elements of type StirngVar contain no blanks (except for the end marker elements of type StringVar).

Your class Text will have member functions corresponding to all the member functions StringVar. The constructor with an argument of type const char a[] will initialize the Text object in the same way as described below for inputLine. If the C-string argument contains the new-line symbol '\n', that is considered an error and ends the program with an error message.

The member function inputLine will read blank separated strings and store each string in one element of the dynamic array with base type StringVar. Multiple blank spaces are treated the same as a single blank space. When outputting an object of the class Text, insert one blank between each value of type StringVar. You may either assume that no tab symbols are used or you can treat the tab symbols the same as a blank. The overloaded version of the extraction operator >> will fill only one element of the dynamic array.

I guess my first question is, what is the author asking me to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
 //This is the code that the author wanted for the class StringVar.
//It compiles and works as the author describes in the programming project.

#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <cstring>

using namespace std;

class StringVar
{
	public:
		StringVar();
		StringVar(int);
		StringVar(const char []);
		StringVar(const StringVar &);
		~StringVar();
		int length() const;
		void inputLine(istream &);
		friend ostream& operator <<(ostream &, const StringVar &);
		StringVar copyPiece(int, int);
		char oneChar(char) const; //Returns '0' if char is not found
		void setChar(const StringVar &, char, int);
		friend bool operator ==(const StringVar &, const StringVar &);
		friend StringVar operator +(const StringVar &, const StringVar &);
		friend istream& operator >>(istream &, StringVar &);
		void operator =(const StringVar &);

	private:
		char *value;
		int maxLength;

};

StringVar::StringVar() : maxLength(100)
{
	value = new char[maxLength + 1];
	value[0] = '\0';
}
StringVar::StringVar(int size) : maxLength(size)
{
	value = new char[maxLength + 1];
	value[0] = '\0';
}
StringVar::StringVar(const char a[]) : maxLength(strlen(a))
{
	value = new char[maxLength + 1];
	strcpy(value, a);
}
StringVar::StringVar(const StringVar &strObj) : maxLength(strObj.length())
{
	value = new char[maxLength + 1];
	strcpy(value, strObj.value);
}
StringVar::~StringVar()
{
	delete [] value;
}
int StringVar::length() const
{
	return strlen(value);
}
void StringVar::inputLine(istream &ins)
{
	ins.getline(value, maxLength + 1);
}
ostream& operator <<(ostream &outs, const StringVar &str)
{
	outs << str.value;
	return outs;
}
StringVar StringVar::copyPiece(int startingIndex, int endingIndex)
{
	if(startingIndex < 0 || endingIndex >= maxLength)
	{
		char ch;
		cout << "Illegal string index! Aborting program.\n";
		cin.get(ch);
		exit(1);
	}

	StringVar temp(endingIndex - startingIndex);
	for(int index = startingIndex; index < endingIndex; index++)
		temp.value[index] = value[index];

	temp.value[endingIndex - startingIndex] = '\0';

	return temp;
}
char StringVar::oneChar(char letter) const
{
	for(int index = 0; index < strlen(value); index++)
	{
		if(value[index] == letter)
			return (value[index]);
	}

	return '0';
}
void StringVar::setChar(const StringVar &str, char letter, int index)
{
	if(index <  0 || index >= str.maxLength)
	{
		char ch;
		cout << "Illegal string index! Aborting program.\n";
		cin.get(ch);
		exit(1);
	}

	str.value[index] = letter;
}
bool operator ==(const StringVar &lhs, const StringVar &rhs)
{
	return ( lhs.value == rhs.value);
}
StringVar operator +(const StringVar &lhs, const StringVar &rhs)
{
	StringVar newString(lhs.maxLength + rhs.maxLength + 1);
	char space[] = " ";

	strcat(newString.value, lhs.value);
	strcat(newString.value, space);
	strcat(newString.value, rhs.value);

	return newString;
}
istream& operator >>(istream &istr, StringVar &str)
{
	istr.getline(str.value, '\n');
	str.maxLength = strlen(str.value);

	return istr;
}
void StringVar::operator =(const StringVar &rhs)
{
	maxLength = rhs.maxLength;
	strcpy(value, rhs.value);
}


void conversation(int);

int main()
{
	using namespace std;

	bool finished;
	char ch, letter;
	StringVar first, last, subStr;

	/*conversation(30);
	cout << "End of demonstration.\n";*/

	do
	{
		cout << "Enter two names.\n\n";
		cout << "First: ";
		cin >> first;
		cout << endl;
		cout << "Second: ";
		cin >> last;
		cout << endl;

		cout << "Testing addition operator:\n";
		cout << first << " + " << last << " = " << first + last << endl << endl;

		//Testing equality operator
		cout << "Testing equality operator:\n";
		if(first == last)
			cout << first << " = " << last << endl << endl;
		else
			cout << first << " != " << last << endl << endl;

		//Testing setChar function
		cout << "Testing setChar function:\n";
		first.setChar(first, 'X', 3);
		cout << first << endl << endl;

		//Testing oneChar function
		cout << "Testing oneChar function:\n";
		letter = first.oneChar('M');
		cout << letter << endl << endl;

		//Testing copyPiece function
		cout << "Testing copyPiece function:\n";
		subStr = first.copyPiece(0, 3);
		cout << subStr << endl << endl;

		cout << "Test again?\n";
		cin >> ch;

		if(ch == 'N' || ch == 'n')
			finished = true;
		else
			finished = false;

	}while(!finished);
	
	cin.get(ch);
	return 0;
}

void conversation(int maxNameSize)
{
	using namespace std;

	StringVar yourName(maxNameSize), ourName("Borg");

	cout << "What is your name?\n";
	yourName.inputLine(cin);
	cout << "We are " << ourName << endl;
	cout << "We will meet again " << yourName << endl;
}
Topic archived. No new replies allowed.