Study guide

To mods: I am posting a friend's code online so that we can all study the material together on one website. Please do not delete! I appreciate it greatly.

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
248
249
250
251
252
253
254
#include <iostream>
#include <string>
using namespace std;

struct Address {
	unsigned number;	//	e.g. 6401 (disallowing fractional addresses)
	string street;		//	e.g. “Winnetka”
	string suffix;		//	e.g. “Avenue”
	string city;		//	e.g. “Woodland Hills”
	string state;		//	e.g. “CA”
	unsigned zip;		//	e.g. 91371 (no zip+4)
}mine, one[5];

struct Time {
	//	always in [0, 6]:
	//	0 means Sunday, 1 means Monday, ... , 6 means Saturday
	unsigned day;
	//	false means at or after midnight, and before the following noon (AM)
	//	true means at or after noon, and before the following midnight (PM)
	bool pm;
	unsigned hour;		//	in [1, 12], e.g. 12 for 12 o’clock
	unsigned minute;	//	in [0, 59]
}First, Second, Third;	//	struct Time

bool die(const string & msg);
void show(const Address & address);
void show(const Address address[], unsigned elements, unsigned desiredZip);
void show(const Address address[], unsigned addressElements,
	const unsigned desiredZip[], unsigned desiredZipElements);
void show(const Time & time);
bool ok(const Time & time);
int compare(const Time & time0, const Time & time1);
void input(Time & time);

const string days[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

int main() {
	mine.number = 20521;
	mine.street = "Orey";
	mine.suffix = "Pl.";
	mine.city = "Winnetka";
	mine.state = "CA";
	mine.zip = 91306;
	cout << "Basic Show Function: " << endl;
	show(mine);
	cout << endl;

	one[0].number = 11111;
	one[0].street = "Blah";
	one[0].suffix = "St.";
	one[0].city = "Los Angeles";
	one[0].state = "CA";
	one[0].zip = 91300;

	one[1].number = 2222;
	one[1].street = "BLAHBLAH";
	one[1].suffix = "Ave.";
	one[1].city = "Westwood";
	one[1].state = "CA";
	one[1].zip = 91303;

	one[2].number = 33333;
	one[2].street = "Blehbleh";
	one[2].suffix = "Way";
	one[2].city = "Topanga";
	one[2].state = "CA";
	one[2].zip = 91303;

	one[3].number = 44444;
	one[3].street = "Random";
	one[3].suffix = "Blvd.";
	one[3].city = "Buena Park";
	one[3].state = "CA";
	one[3].zip = 91306;

	one[4].number = 55555;
	one[4].street = "Los Santos";
	one[4].suffix = "Way";
	one[4].city = "Irvine";
	one[4].state = "CA";
	one[4].zip = 91302;

	cout << "Second show function: " << endl;
	show(one, 5, 91306);
	cout << endl;

	unsigned zips[2] = { 91303, 91306 };
	cout << "Third show function: " << endl;
	show(one, 5, zips, 2);
	cout << endl;

	First.day = 3;
	First.pm = true;
	First.hour = 7;
	First.minute = 25;

	cout << "Show time function:" << endl;
	show(First);
	cout << endl;

	Second.day = 0;
	Second.pm = true;
	Second.hour = 12;
	Second.minute = 0;

	cout << "Okay function: " << endl;
	cout << ok(Second); //I have it doing an output so I can see that it works.
	cout << endl;

	cout << "Compare function: " << endl;
	cout << compare(First, Second);
	cout << endl;

	cout << "Input function: " << endl;
	input(Third);


}

bool die(const string & msg) {
	cout << "ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
}
void show(const Address & address) {
	cout << address.number << " " << address.street << " " << address.suffix << endl
		<< address.city << " " << address.state << " " << address.zip << endl;

}

void show(const Address address[], unsigned elements, unsigned desiredZip) {
	for (unsigned count = 0; count < elements; count++) {
		if (address[count].zip == 91306) {
			show(address[count]);
			cout << endl << "------------------------------" << endl;
		}
	}
}

void show(const Address address[], unsigned addressElements,
	const unsigned desiredZip[], unsigned desiredZipElements) {

	for (unsigned count = 0; count < addressElements; count++) {
		for (unsigned count1 = 0; count1 < desiredZipElements; count1++) {
			if (address[count].zip == desiredZip[count1]) {
				show(address[count]);
				cout << endl << "------------------------------" << endl;
			}
		}
	}

}

void show(const Time & time) {
	cout << days[time.day] << "  " << time.hour << ":";
	if (time.minute < 10) cout << 0 << time.minute << " ";
	else cout << time.minute << " ";
	if (time.pm == true)
		cout << "PM";
	if (time.pm == false)
		cout << "AM";
	cout << endl;
}

bool ok(const Time & time) {
	bool ret;
	if (time.day >= 0 && time.day <= 6)
		ret = true;
	else return false;
	if (time.hour >= 1 && time.hour <= 12)
		ret = true;
	else return false;
	if (time.minute >= 0 && time.minute <= 59)
		ret = true;
	else return false;
	if (time.pm)
		ret = true;
	else
		ret = true;

	return ret;
}

int compare(const Time & time0, const Time & time1) {
	if (ok(First) == 0 || ok(Second) == 0)
		die("One or both times are invalid.");

	if (time0.day < time1.day)
		return -1;
	if (time0.day > time1.day)
		return 1;
	if (time0.day == time1.day) {
		if (time0.pm == false && time1.pm == true)
			return -1;
		if (time0.pm == true && time1.pm == false)
			return 1;
		if (time0.pm == false && time1.pm == false || time0.pm == true && time1.pm == true) {
			if (time0.hour < time1.hour)
				return -1;
			if (time0.hour > time1.hour)
				return 1;
			if (time0.hour == time1.hour) {
				if (time0.minute < time1.minute)
					return -1;
				if (time0.minute > time1.minute)
					return 1;
				if (time0.minute == time1.minute)
					return 0;
			}
		}
	}
}

void input(Time & time) {
	string day;
	bool check = false;
	char colon;
	string ampm;
	cin >> day >> time.hour >> colon >> time.minute >> ampm;
	if (!cin)
		die("Invalid Input");

	cout << "Day: " << days[time.day] << "  Hour: " << time.hour << "  Colon: " << colon << "  Minute: " << time.minute
		<< "  AMPM: " << ampm << endl;

	for (unsigned count = 0; count < 7; count++) {
		if (day == days[count]) {
			time.day = count;
			check = true;
			break;
		}
		if (check == false && count == 6)
			die("Invalid Day");
	}

	if (time.hour < 0 || time.hour > 12)
		die("Invalid Hour");

	if (colon != ':')
		die("Invalid Char");

	if (time.minute < 0 || time.minute > 59)
		die("Invalid Minute");

	if (ampm != "PM")
		if (ampm != "AM")
			die("Invalid AM-PM");

	if (ampm == "AM")
		time.pm = false;
	if (ampm == "PM")
		time.pm = true;
}

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

class Point {
public:
	//ctor
	Point(double x = 0, double y = 0);

	//access
	double getX() const;
	double getY() const;

	//mutate
	Point & setX(double x);    //  mutator, returning reference to self
	Point & setY(double y);
	const Point & output() const;
	double distance(const Point & other) const;
private:
	double Left;
	double Right;
}; // class Point

double area(const Point & a, const Point & b, const Point & c);

int main() {
	Point One, Two, Three;
	double xVal, yVal;

	cout << "Get functions: " << endl;
	cout << One.getX() << One.getY() << endl << endl;

	cout << "Enter two points: " << endl;
	cin >> xVal >> yVal;
	One.setX(xVal);
	One.setY(yVal);
	cout << endl;


	cout << "Output function: " << endl;
	One.output();
	cout << endl;

	cout << "Enter two points: " << endl;
	cin >> xVal >> yVal;
	cout << endl;
	Two.setX(xVal);
	Two.setY(yVal);
	cout << "Distance function: " << endl;
	cout << One.distance(Two);
	cout << endl;
	cout << endl;

	cout << "Enter two points: " << endl;
	cin >> xVal >> yVal;
	cout << endl;
	Three.setX(xVal);
	Three.setY(yVal);
	cout << "Area function: " << endl;
	cout << area(One, Two, Three) << endl;
	cout << endl;
}

Point::Point(double x, double y) {
	setX(x);
	setY(y);
}

double Point::getX() const {
	return Left;
}

double Point::getY() const {
	return Right;
}


Point & Point::setX(double x) {
	Left = x;
	return *this;
}

Point & Point::setY(double y) {
	Right = y;
	return *this;
}


const Point & Point::output() const {
	cout << "(" << getX() << ", " << getY() << ")" << endl;
	return *this;
}

double Point::distance(const Point & other) const {
	return sqrt(pow(Left - other.getX(), 2) + pow(Right - other.getY(), 2));
}

double area(const Point & a, const Point & b, const Point & c) {
	double S = (a.distance(b) + b.distance(c) + c.distance(a)) / 2;
	return sqrt(S*(S - a.distance(b))*(S - b.distance(c))*(S - c.distance(a)));
}
This isn't a file hosting service.
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
#include <iostream>
#include <string>
using namespace std;

class Transaction {
public:
	Transaction(unsigned doughnuts, unsigned carburetors = 0, unsigned racquets = 0);
	unsigned getDoughnuts() const;		//  return the number of doughnuts purchased in this transaction
	unsigned getCarburetors() const;	//  return the number of carburetors purchased in this transaction
	unsigned getRacquets() const;		//  return the number of racquets purchased in this transcation
	double getSubtotal() const;		//  return the total cost of this purchase, excluding sales tax
	double getTotal() const;			//  return the total cost of this purchase, including sales tax

	Transaction & setDoughnuts(unsigned doughnuts);
	Transaction & setCarburetors(unsigned carburetors);
	Transaction & setRacquets(unsigned racquets);
private:
	unsigned dough, carb, rac;
	double subtotal = 0, total = 0;
};    //  class Transaction

void report(const Transaction transaction[], unsigned elements);


int main() {
	Transaction One(0), Two(0, 0, 0), Three(1, 1, 1), Four(1, 1, 1), Five(2, 2, 2);
	unsigned doughs, carbs, racs;
	cout << "How many Doughnuts, Carburetors, and Racquets? " << endl;
	cin >> doughs >> carbs >> racs;
	One.setDoughnuts(doughs);
	One.setCarburetors(carbs);
	One.setRacquets(racs);
	cout << endl;

	cout << "Get functions: " << endl;
	cout << "Number of Doughnuts: " << One.getDoughnuts() << endl;
	cout << "Number of Carburetors: " << One.getCarburetors() << endl;
	cout << "Number of Racquets: " << One.getRacquets() << endl << endl;

	cout << "Total functions: " << endl;
	cout << "Subtotal is: " << One.getSubtotal() << endl;
	cout << "Total is: " << One.getTotal() << endl << endl;

	Transaction January[5] = { One, Two, Three, Four, Five };
	cout << "Report function: " << endl;
	cout << "Business done this month: " << endl;
	report(January, 5);
	cout << endl << endl;


}

void report(const Transaction transaction[], unsigned elements) {
	unsigned dTotal = 0, cTotal = 0, rTotal = 0;
	double Total = 0, Subtotal = 0;
	for (unsigned count = 0; count < elements; count++) {
		dTotal += transaction[count].getDoughnuts();
		cTotal += transaction[count].getCarburetors();
		rTotal += transaction[count].getRacquets();
	}

	cout << "Doughnuts sold: " << dTotal << endl;
	cout << "Carburetors sold: " << cTotal << endl;
	cout << "Racquets sold: " << rTotal << endl;


	dTotal *= 0.4;
	cTotal *= 200;
	rTotal *= 75;
	Total = dTotal*(1.09) + cTotal*(1.09) + rTotal;
	Subtotal = dTotal + cTotal + rTotal;

	cout << "Subtotal: " << Subtotal << endl;
	cout << "Total: " << Total << endl;



}

Transaction & Transaction::setDoughnuts(unsigned doughnuts) {
	dough = doughnuts;
	return *this;
}

Transaction & Transaction::setCarburetors(unsigned carburetors) {
	carb = carburetors;
	return *this;
}

Transaction & Transaction::setRacquets(unsigned racquets) {
	rac = racquets;
	return *this;
}

Transaction::Transaction(unsigned doughnuts, unsigned carburetors, unsigned racquets) {
	setDoughnuts(doughnuts);
	setCarburetors(carburetors);
	setRacquets(racquets);
}

unsigned Transaction::getDoughnuts() const {
	return dough;
}

unsigned Transaction::getCarburetors() const {
	return carb;
}

unsigned Transaction::getRacquets() const {
	return rac;
}

double Transaction::getSubtotal() const {
	double subtotal = (0.4*dough) + (200 * carb) + (75 * rac);
	return subtotal;
}

double Transaction::getTotal() const {
	double dTotal = (0.4*dough);
	double cTotal = (200 * carb);
	double rTotal = (75 * rac);
	double total = dTotal*(1.09) + cTotal*(1.09) + rTotal;
	return total;
}
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;

}
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;
}
yall could just message eachother
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;
}
Last edited on
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;
}
closed account (48bpfSEw)
Here is an online IDE where you can upload/download your projects, compile and run! allinclusive! ^^

http://www.tutorialspoint.com/compile_cpp_online.php

Or share your code on GitHub.

Any of these options would be better than cluttering up a public forum with what is, effectively, a private thread for you and and your friends.
Last edited on
Another option to share code is Dropbox or OneDrive.
It also exists pastebin.

Reported
Last edited on
I don't think this deserves to be reported. It's just that there are better tools for the job.
What's the difference between what OP's done and spam?
It's done for less cynical reasons. I presume the OP simply wanted a place to share C++ code with collaborators, and wasn't aware of the better alternatives. That doesn't deserve the condemnation that someone spamming this forum for commercial gain would deserve.

Or maybe it's because the sun is shining and I'm in a good mood today :P
Last edited on
Even if they weren't aware of better alternatives, it's still an obvious misuse of a public forum. Would it therefore also be excusable for someone to post a dozen Base64 blocks as long as they state the reason why they're doing it? No, it wouldn't. This is a forum, not a dumping ground, nor a private communication channel.
Topic archived. No new replies allowed.