Ignore the following:

Using this is a template to study from. I just want one area where my code is printed neatly. (Please do not delete, Admins) Thank you!

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
#include <iostream>
#include <string>
using namespace std;

class SparseArray {
public:
	SparseArray();	//	construct an SparseArray with no nonzero elements
	unsigned get(unsigned long long index);	//	return an element from the array
	SparseArray & set(unsigned long long index, unsigned value);	//	set array element to value
private:
	unsigned nonZeroEls;
	unsigned long long indexArray[20];
	unsigned valArray[20];
};  //  SparseArray

bool die(const string & msg);

int main() {
	SparseArray a;
	unsigned long long index = 1;
	for (unsigned i = 0; i <= 20; i++, index *= 4) {
		a.set(index, i);
		cout << "Index: " << index << endl;
		cout << "Value: " << i << endl;
	}

	unsigned total = 0;
	index = 1;
	for (unsigned i = 0; i <= 40; i++, index *= 2) {
		total += a.get(index);
		cout << a.get(index) << endl;
	}
	cout << total;
}   //  main

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}

SparseArray::SparseArray() {
	nonZeroEls = 0;
	for (unsigned count = 0; count < 20; count++) {
		indexArray[count] = 0;
		valArray[count] = 0;
	}
}

unsigned SparseArray::get(unsigned long long index) {
	bool ok = false;
	for (unsigned count = 0; count < 20; count++) {
		if (index == indexArray[count]) {
			ok = true;
			return valArray[count];
		}
	}
	if (ok == false)
		return 0;
}

SparseArray & SparseArray::set(unsigned long long index, unsigned value) {
	if (index > 99999999999999999)
		die("Number too large");
	if (get(index) == 0 && value != 0 && nonZeroEls >= 20)
		die("Too many values in array already.");

	if (value == 0) {
		if (get(index) != 0) {
			for (unsigned count = 0; count < 20; count++) {
				if (indexArray[count] == index) {
					for (unsigned count1 = count; count1 < 20; count1++) {
						valArray[count1] = valArray[count1 + 1];
						indexArray[count1] = indexArray[count + 1];
					}
				}
			}
			nonZeroEls--;
		}
	}

	if (value != 0) {
		if (get(index) != 0)
		for (unsigned count = 0; count < 20; count++) {
			if (indexArray[index] == index && value != 0) {
				valArray[count] = value;
			}
		}
		
		if (get(index) == 0 && nonZeroEls < 20) {
			nonZeroEls++;
			for (unsigned count = 0; count < 20; count++) {
				if (indexArray[count] == 0) {
					//	nonZeroEls++;
					valArray[count] = value;
					indexArray[count] = index;
					break;
				}
			}
		}
	}
	return *this;
}
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
#include <iostream>
#include <string>
using namespace std;

class Rational {
public:
	Rational(unsigned long long numerator = 0, unsigned long long denominator = 1);
	unsigned long long getNumerator() const;
	unsigned long long getDenominator() const;

	double getFloatingValue() const;
	const Rational & output() const;
	Rational add(const Rational & other) const;
	static unsigned long long gcf(unsigned long long a, unsigned long long b);
private:
	unsigned long long NUM, DENOM;
	unsigned long long simplify();
};  //  class Rational

bool die(const string & msg);

int main() {
	Rational(4668, 47580);
	cout << "Simplified numerator: " << Rational(4668, 47580).getNumerator() << endl;
	cout << "Simplified denominator: " << Rational(4668, 47580).getDenominator() << endl;

	cout << endl << endl;
	cout << Rational(4668, 47580).getNumerator() << endl;
	cout << "--------" << "   =  " << Rational(4668, 47580).getFloatingValue() << endl;
	cout << Rational(4668, 47580).getDenominator() << endl << endl;

	cout << "Output function: " << endl;
	Rational(4668, 47580).output();
	cout << endl << endl;

	cout << "Add function: " << endl;
	Rational(1, 2).add(Rational(2, 3)).output();
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}

unsigned long long Rational::simplify() {
	unsigned temp;
	temp = NUM;
	NUM = NUM / gcf(NUM, DENOM);
	DENOM = DENOM / gcf(temp, DENOM);
	return 0;
}

Rational::Rational(unsigned long long numerator, unsigned long long denominator) {
	NUM = numerator;
	DENOM = denominator;
	if (denominator = 0)
		die("Denominator can't be equal to '0'.");
	simplify();
}

unsigned long long Rational::getNumerator() const {
	return NUM;
}

unsigned long long Rational::getDenominator() const {
	return DENOM;
}


double Rational::getFloatingValue() const {
	double fVal;
	fVal = static_cast<double>(NUM) / static_cast<double>(DENOM);
	return fVal;
}

const Rational & Rational::output() const {
	if (DENOM == 1)
		cout << NUM << endl;
	else
		cout << NUM << " / " << DENOM << endl;
	return *this;
}

Rational Rational::add(const Rational & other) const {
	unsigned long long tempNum, tempDenom;
	tempNum = NUM*other.DENOM + other.NUM*DENOM;
	tempDenom = DENOM*other.DENOM;
	return Rational(tempNum, tempDenom);
}

unsigned long long Rational::gcf(unsigned long long a, unsigned long long b) {
	while (a > 0) {
		unsigned long long newA = b%a, newB = a;
		a = newA, b = newB;
	}
	return b;
}
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <string>
using namespace std;

class Rational {
public:
	Rational & operator+=(const Rational & rhs); // add rhs to lhs, return reference to lhs
	Rational operator+(const Rational & rhs) const; // return sum of Rat + Rat
	Rational operator+(unsigned long long rhs) const; // return sum of Rat + int
	Rational operator*(const Rational & rhs) const; // return Rat * Rat
	Rational operator/(const Rational & rhs) const; // return Rat / Rat
	bool operator<(const Rational & rhs) const; // return true iff lhs < rhs
	bool operator<(unsigned long long rhs) const; // return true iff lhs < rhs
	bool operator<=(const Rational & rhs) const; // return true iff lhs <= rhs
	bool operator>(const Rational & rhs) const; // return true iff lhs > rhs
	bool operator>=(const Rational & rhs) const; // return true iff lhs >= rhs
	bool operator==(const Rational & rhs) const; // return true iff lhs == rhs
	bool operator!=(const Rational & rhs) const; // return true iff lhs != rhs
	friend ostream & operator<<(ostream & out, const Rational & rat);

	Rational(unsigned long long numerator = 0, unsigned long long denominator = 1);
	unsigned long long getNumerator() const;
	unsigned long long getDenominator() const;

	double getFloatingValue() const;
	const Rational & output() const;
	Rational add(const Rational & other) const;
	static unsigned long long gcf(unsigned long long a, unsigned long long b);
private:
	unsigned long long NUM, DENOM;
	unsigned long long simplify();
};  //  class Rational

Rational operator+(unsigned long long lhs, const Rational & rhs); // return sum of int + Rat
Rational operator-(const Rational & lhs, const Rational & rhs); // return Rat - Rat
bool operator<(unsigned long long lhs, const Rational & rhs); // return true iff lhs < rhs


bool die(const string & msg);

int main() {
	Rational(4668, 47580);
	cout << "Simplified numerator: " << Rational(4668, 47580).getNumerator() << endl;
	cout << "Simplified denominator: " << Rational(4668, 47580).getDenominator() << endl;

	cout << endl << endl;
	cout << Rational(4668, 47580).getNumerator() << endl;
	cout << "--------" << "   =  " << Rational(4668, 47580).getFloatingValue() << endl;
	cout << Rational(4668, 47580).getDenominator() << endl << endl;

	cout << "Output function: " << endl;
	Rational(4668, 47580).output();
	cout << endl << endl;

	cout << "Add function: " << endl;
	Rational(1, 2).add(Rational(2, 3)).output();
	cout << endl;

	cout << "HW#5 tests: " << endl;
	cout << Rational(1, 2) + Rational(2, 3) << endl;
	cout << Rational(1, 2) + 15 << endl;
	cout << 15 + Rational(1, 2) << endl;
	cout << (Rational(1, 2) += Rational(1, 2)) << endl;
	cout << Rational(1, 2) - Rational(1, 2) << endl;
	cout << (Rational(1, 2) * Rational(1, 2)) << endl;
	cout << (Rational(1, 2) / Rational(1, 2)) << endl;
	cout << (Rational(1, 2) < Rational(1, 8)) << endl;
	cout << (Rational(1, 2) > Rational(1, 8)) << endl;
	cout << (2 < Rational(1, 8)) << endl;
	cout << (Rational(1, 2) < Rational(1, 8)) << endl;
}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}

unsigned long long Rational::simplify() {
	unsigned temp;
	temp = NUM;
	NUM = NUM / gcf(NUM, DENOM);
	DENOM = DENOM / gcf(temp, DENOM);
	return 0;
}

Rational::Rational(unsigned long long numerator, unsigned long long denominator) {
	NUM = numerator;
	DENOM = denominator;
	if (denominator = 0)
		die("Denominator can't be equal to '0'.");
	simplify();
}

unsigned long long Rational::getNumerator() const {
	return NUM;
}

unsigned long long Rational::getDenominator() const {
	return DENOM;
}


double Rational::getFloatingValue() const {
	double fVal;
	fVal = static_cast<double>(NUM) / static_cast<double>(DENOM);
	return fVal;
}

const Rational & Rational::output() const {
	if (DENOM == 1)
		cout << NUM << endl;
	else
		cout << NUM << " / " << DENOM << endl;
	return *this;
}

Rational Rational::add(const Rational & other) const {
	unsigned long long tempNum, tempDenom;
	tempNum = NUM*other.DENOM + other.NUM*DENOM;
	tempDenom = DENOM*other.DENOM;
	unsigned temp;
	temp = tempNum;
	tempNum = tempNum / gcf(tempNum, tempDenom);
	tempDenom = tempDenom / gcf(temp, tempDenom);
	return Rational(tempNum, tempDenom);

}

unsigned long long Rational::gcf(unsigned long long a, unsigned long long b) {
	while (a > 0) {
		unsigned long long newA = b%a, newB = a;
		a = newA, b = newB;
	}
	return b;
}

Rational Rational::operator+(const Rational & rhs) const {
	return add(rhs);
}

Rational Rational::operator+(unsigned long long rhs) const {
	return add(Rational(rhs, 1));
}

Rational operator+(unsigned long long lhs, const Rational & rhs) {
	return Rational(lhs, 1).add(rhs);
}

Rational & Rational::operator+=(const Rational & rhs) {
	NUM = (NUM*rhs.getDenominator()) + (DENOM*rhs.getNumerator());
	DENOM = DENOM*rhs.getDenominator();
	return *this;
}

Rational operator-(const Rational & lhs, const Rational & rhs) {
	if (lhs.getFloatingValue() < rhs.getFloatingValue())
		die("Can't have negative unsigned values.");
	unsigned long long Left = 0, Right = 0, subNum = 0, subDenom = 0;
	Left = lhs.getNumerator()*rhs.getDenominator();
	Right = rhs.getNumerator()*lhs.getDenominator();
	subNum = Left - Right;
	subDenom = rhs.getDenominator()*lhs.getDenominator();
	return Rational(subNum, subDenom);
}

Rational Rational::operator*(const Rational & rhs) const {
	unsigned long long newNum = 0, newDenom = 0;
	newNum = NUM*rhs.getNumerator();
	newDenom = DENOM*rhs.getDenominator();
	return Rational(newNum, newDenom);
}

Rational Rational::operator/(const Rational & rhs) const {
	if (DENOM == 0 || rhs.getDenominator() == 0)
		die("Can't divide by zero.");
	return Rational(rhs.getDenominator()*NUM, rhs.getNumerator()*DENOM);
}

bool Rational::operator<(const Rational & rhs) const {
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
		< (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;

}

bool Rational::operator<(unsigned long long rhs) const {
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM) < rhs))
		return true;
	else
		return false;
}

bool operator<(unsigned long long lhs, const Rational & rhs) {  // return true iff lhs < rhs
	if (lhs < (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;
}

bool Rational::operator<=(const Rational & rhs) const { // return true iff lhs <= rhs
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
		<= (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;
}

bool Rational::operator>(const Rational & rhs) const { // return true iff lhs > rhs
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
> (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
return true;
	else
		return false;
}

bool Rational::operator>=(const Rational & rhs) const { // return true iff lhs >= rhs
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
		>= (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;
}

bool Rational::operator==(const Rational & rhs) const { // return true iff lhs == rhs
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
		== (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;
}

bool Rational::operator!=(const Rational & rhs) const { // return true iff lhs != rhs
	if ((static_cast<double>(NUM) / static_cast<double>(DENOM))
		!= (static_cast<double>(rhs.getNumerator()) / static_cast<double>(rhs.getDenominator())))
		return true;
	else
		return false;
}

ostream & operator<<(ostream & out, const Rational & rat) {
	if (rat.getDenominator() == 1)
		return out << rat.getNumerator();
	else
		return out << rat.getNumerator() << " / " << rat.getDenominator();
}
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
#include <iostream>
#include <algorithm>
#include <string>
#include <new>
using namespace std;

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}

void show(const int a[], unsigned elements);
int * copy(const int a[], unsigned els);
int * nonzeroCopy(unsigned & nonzeroEls, const int a[], unsigned els);
void changeSize(int * & ptr, int newEls, int oldEls);

int main() {
	int One[4] = { 2, 1, 0, 3 };
	show(One, 4);
	show(copy(One, 4), 4);

	int* oneArray;
	try {
		oneArray = copy(One, 4);
	}
	catch (bad_alloc & oneArray) {
		cout << oneArray.what();
		die("Main fail #1.");
	}
	sort(oneArray, oneArray + 4);
	show(oneArray, 4);
	delete[] oneArray;

	unsigned abc = 1;
	int* twoArray;
	try {
		twoArray = copy(One, 4);
	}
	catch (bad_alloc & twoArray) {
		cout << twoArray.what();
		die("Main fail #2.");
	}
	show(nonzeroCopy(abc, twoArray, 4), 4);
	delete[] twoArray;

	int* threeArray;
	try {
		threeArray = copy(One, 4);
	}
	catch (bad_alloc & threeArray) {
		cout << threeArray.what();
		die("Main fail #3.");
	}
	changeSize(threeArray, 5, 4);
	show(threeArray, 5);



}

void show(const int a[], unsigned elements) {
	for (unsigned count = 0; count < elements; count++) {
		if (count == 0)
			cout << "{ ";
		if (count >= 0 && count < elements - 1)
			cout << a[count] << ", ";
		else
			cout << a[count];
	}
	cout << " }" << endl;
}

int * copy(const int a[], unsigned els) {
	int * newArray;
	try {
		newArray = new int[els];
	}
	catch (bad_alloc & newArray) {
		cout << newArray.what();
		die("Copy fail.");
	}
	for (unsigned count = 0; count < els; count++) {
		newArray[count] = a[count];
		}
	return newArray;
}

int * nonzeroCopy(unsigned & nonzeroEls, const int a[], unsigned els) {
	nonzeroEls = 0;
	unsigned counter = 0;
	for (unsigned count = 0; count < els; count++) {
		if (a[count] != 0)
			counter++;
	}
	nonzeroEls = counter;
	unsigned count1 = 0;
	int* newArray;
	try {
		newArray = new int[counter];
	}
	catch (bad_alloc & newArray) {
		cout << newArray.what();
		die("Non Zero fail.");
	}
	for (unsigned count = 0; count < els; count++) {
		if (a[count] != 0) {
			newArray[count1] = a[count];
			count1++;
		}
	}
	return newArray;
}

void changeSize(int * & ptr, int newEls, int oldEls) {
	int* nArray;
	try {
		nArray = new int[newEls];
	}
	catch (bad_alloc & nArray) {
		cout << nArray.what();
		die("Change Size fail.");
	}
	if (newEls >= oldEls) {
		nArray = copy(ptr, oldEls);
		for (int count = oldEls + 1; count < newEls; count++) {
			nArray[count] = 0;
		}
	}
	if (newEls < oldEls) {
		nArray = copy(ptr, newEls);
	}

	delete[] ptr;
	ptr = nArray; //need to catch in this function
}
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
#include <iostream>
#include <new>
using namespace std;

class String540 {
public:
	/**/String540(); //default ctor
	/**/String540(const String540 & other); //Copy ctor
	/**/String540(char CH, unsigned els = 1); //char ctor
	/**/String540(char* arg); //string ctor
	/**/~String540(); //D-ctor
	/**/String540 operator=(const String540 & rhs);
	/**/String540 operator+(const String540 & rhs) const;
	/**/friend ostream & operator<<(ostream & out, const String540 & pChar);
	String540 substr(const unsigned arg1, const unsigned arg2);
	const unsigned getUsedChar();
	char *getPtrChar();
private:
	char *ptrChar;
	unsigned usedChar;
};

String540 operator*(String540 word, const unsigned times);
bool die(const char* arg);

int main() {
	String540 a("HELLO WORLD");
	cout << "Ctor 1: " << endl;
	cout << a << endl << endl;

	String540 b("LOLOL");
	cout << "Ctor 2 & 3: " << endl;
	cout << b << endl << endl;

	String540 c(a);
	cout << "Ctor 4: " << endl;
	cout << c << endl << endl;

	String540 d("Blah");
	a = d;
	cout << "Equals overload: " << endl;
	cout << a << endl << endl;

	String540 e(b + d);
	cout << "Addition overload: " << endl;
	cout << e << endl << endl;

	String540 f("Hello World");
	cout << "Substr method: " << endl;
	cout << f.substr(2, 2) << endl << endl;

	cout << "Multiply Overload: " << endl;
	cout << d * 10 << endl << endl;


}

const unsigned String540::getUsedChar() {
	return usedChar;
}

char *String540::getPtrChar() {
	return ptrChar;
}

bool die(const char* arg) {
	cout << "ERROR: " << arg << endl;
	exit(EXIT_FAILURE);
}

String540::String540(char* arg) {
	usedChar = strlen(arg);
	ptrChar = new char[usedChar];
	for (unsigned count = 0; count < usedChar; count++) {
		ptrChar[count] = arg[count];
	}
}

String540::String540() {
	ptrChar = new char[NULL];
	usedChar = 0;
}//constructor

String540::~String540() {
	delete[] ptrChar;
}//deconstructor

String540::String540(const String540 & other) {
	ptrChar = new char[other.usedChar];
	usedChar = other.usedChar;
	for (unsigned count = 0; count < other.usedChar; count++) {
		ptrChar[count] = other.ptrChar[count];
	}
}//copy constructor

String540::String540(char CH, unsigned els) {
	ptrChar = new char[els];
	for (unsigned count = 0; count < els; count++) {
		ptrChar[count] = CH;
	}
	usedChar = els;
}

ostream & operator<<(ostream & out, const String540 & pChar) {
	for (unsigned count = 0; count < pChar.usedChar; count++) {
		out << pChar.ptrChar[count];
	}
	return out;
}//operator<<

String540 String540::operator=(const String540 & rhs) {
	delete[] ptrChar;
	ptrChar = new char[rhs.usedChar];
	usedChar = rhs.usedChar;
	for (unsigned count = 0; count < usedChar; count++) {
		ptrChar[count] = rhs.ptrChar[count];
	}
	return *this;
}//operator=

String540 String540::operator+(const String540 & rhs) const {
	String540 newArray;
	newArray.usedChar = rhs.usedChar + usedChar;
	newArray.ptrChar = new char[newArray.usedChar];
	for (unsigned count = 0, count2 = 0; count < newArray.usedChar; count++) {
		if (count < usedChar)
			newArray.ptrChar[count] = ptrChar[count];
		if (count >= usedChar) {
			newArray.ptrChar[count] = rhs.ptrChar[count2];
			count2++;
		}
	}
	return newArray;
}

String540 operator*(String540 word, const unsigned times) {
	unsigned size = word.getUsedChar() * times;
	String540 newArray(' ', size);

	for (unsigned count = 0; count < size;) {
		for (unsigned count1 = 0; count1 < word.getUsedChar(); count1++) {
			newArray.getPtrChar()[count] = word.getPtrChar()[count1];
			count++;
		}
	}
	return newArray;
}

String540 String540::substr(const unsigned start, const unsigned size) {
	String540 newArray;
	if (start > usedChar)
		die("Argument 1 is too big.");
	if (size > usedChar) {
		newArray.usedChar = usedChar-start;
		newArray.ptrChar = new char[newArray.usedChar];
		for (unsigned count = 0; count < usedChar - start; count++) {
			newArray.ptrChar[count] = ptrChar[count+start];
		}
	}
	if (size <= usedChar) {
		newArray.usedChar = size;
		newArray.ptrChar = new char[newArray.usedChar];
		for (unsigned count = 0; count < size; count++) {
			newArray.ptrChar[count] = ptrChar[count+start];
		}
	}
	return newArray.ptrChar;
}
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
#include <iostream>
using namespace std;

class Color {
public:
	Color(unsigned red = 0, unsigned green = 0, unsigned blue = 0);    //  ctor
	unsigned getRed() const;    //  accessor
	unsigned getGreen() const;    //  accessor
	unsigned getBlue() const;    //  accessor
	Color & setRed(unsigned red);    //  mutator
	Color & setGreen(unsigned green);    //  mutator
	Color & setBlue(unsigned blue);    //  mutator
	const Color & output() const;
private:
	unsigned RED, GREEN, BLUE;
};  //  Color

Color mixture(const Color & color0, const Color & color1, double weight = 0.5);

int main() {
	Color One, Two;
	unsigned R, G, B;
	cout << "Enter 3 numbers [0, 255]. (Red, Green, Blue): " << endl;
	cin >> R >> G >> B;
	One.setRed(R);
	One.setGreen(G);
	One.setBlue(B);
	cout << endl;
	cout << "You entered: " << endl;
	One.output();
	cout << endl << endl;

	cout << "Enter 3 numbers [0, 255]. (Red, Green, Blue): " << endl;
	cin >> R >> G >> B;
	Two.setRed(R);
	Two.setGreen(G);
	Two.setBlue(B);
	cout << endl;
	cout << "You entered: " << endl;
	One.output();
	cout << endl << endl;

	cout << "When mixed together: " << endl;
	mixture(One, Two, .3).output();
	cout << endl << endl;


	

}

Color::Color(unsigned red, unsigned green, unsigned blue) {
	setRed(red);
	setGreen(green);
	setBlue(blue);
}

unsigned Color::getRed() const {
	return RED;
}

unsigned Color::getGreen() const {
	return GREEN;
}

unsigned Color::getBlue() const {
	return BLUE;
}

Color & Color::setRed(unsigned red) {
	RED = red;
	return *this;
}

Color & Color::setGreen(unsigned green) {
	GREEN = green;
	return *this;
}

Color & Color::setBlue(unsigned blue) {
	BLUE = blue;
	return *this;
}

const Color & Color::output() const {
	cout << "<" << RED << ", " << GREEN << ", " << BLUE << ">" << endl;
	return *this;
}

Color mixture(const Color & color0, const Color & color1, double weight) {
	Color mixColor;
	double  avgG, avgB, avgR;
	avgG = color0.getGreen() * weight + color1.getGreen() * (1 - weight);
	mixColor.setGreen(static_cast<int>(0.5 + avgG));
	avgB = color0.getBlue() * weight + color1.getBlue() * (1 - weight);
	mixColor.setBlue(static_cast<int>(0.5 + avgB));
	avgR = color0.getRed() * weight + color1.getRed() * (1 - weight);
	mixColor.setRed(static_cast<int>(0.5 + avgR));
	return mixColor;

}
Topic archived. No new replies allowed.