p6p
4/5/2016 - 3:31 PM

a15

// ch8-1 a15
#include <iostream>

using namespace std;

class complex {
public:
	complex() {
		real = 0;
		imaginary = 0;
	}
	
	complex(double _real) {
		real = _real;
		imaginary = 0;
	}
	
	complex(double _real, double _imaginary) {
		real = _real;
		imaginary = _imaginary;
	}
	
	bool operator==(const complex& c) {
		return real == c.real && imaginary == c.imaginary;
	}
	
//	complex operator+(const complex& c) {
//		complex tmp(real + c.real, imaginary + c.imaginary);
//		return tmp;
//	}
//	
//	complex operator-(const complex& c) {
//		complex tmp(real + c.real, imaginary + c.imaginary);
//		return tmp;
//	}
//	
//	complex operator*(const complex& c) {
//		complex tmp(real * c.real - imaginary * c.imaginary, c.real * imaginary + real * c.imaginary);
//		return tmp;
//	}
//	
//	ostream& operator<<(ostream& out) {
//		out << real << " " << imaginary << endl;
//		return out;
//	}
//	
//	istream& operator>>(istream& in) {
//		in >> real >> imaginary;
//		return in;
//	}
	
	double real;
	double imaginary;
};


int main()
{
	 // test constructors
	 complex x, y(3), z(-3.2, 2.1);
	 cout <<"x = " << x << " y = " << y << " z = " << z << endl << endl;
	 x = complex(3, -4);
	 cout << "testing members and support functions as well as”
	 << " output operator:\n"
	 << "complex number x = " << x << endl
	 << "real part: " << x.real() << endl
	 << "real part from friend real(x): " << real(x) << endl
	 << "imaginary part: " << x.imag() << endl
	 << "imaginary part from friend imag(x) : " << imag(x) << endl
	 << "norm: " << norm(x) << endl << endl;
	 cout << "test complex arithmetic and output routines: \n\n";
	 y = complex (1, -1);
	 cout <<"x = " << x << " y = " << y << " z = " << z << endl << endl;
	 z = x + y;
	 cout <<"z = x + y = " << z << endl;
	 z = x * y;
	 cout <<"z = x * y = " << z << endl;
	 z = x - y;
	 cout <<"z = x - y = " << z << endl;
	 z = x / y;
	 cout <<"z = x / y = " << z << endl << endl;
	 //test of automatic conversion double -> complex by the constructor.
	 double d(2.0);
	 cout << "d: " << d << " x: " << x <<endl;
	 cout << "x+d: " ;
	 z = x + d;
	 cout << z << endl;
	 z = x - d;
	 cout << "x-d: " ;
	 cout << z << endl;
	 z = x * d;
	 cout << "x*d: " ;
	 cout << z << endl;
	 z = x / d;
	 cout << "x/d: " ;
	 cout << z << endl;
	 z = d + x;
	 cout << "d+x: " ;
	 cout << z << endl;
	 z = d - x;
	 cout << "d-x: " ;
	 cout << z << endl;
	 z = d * x;
	 cout << "d*x: " ;;
	 cout << z << endl;
	 z = d / x;
	 cout << "d/x: " ;;
	 cout << z << endl;
	 //test whether double/complex and complex/complex give same result:
	 complex two(2,0);
	 cout << "two/x: ";
	 cout << two/x << endl;
	 cout << "\nGetting data from standard input: \n";
	 cin >> x >> y;
	 cout <<"data read is: x = " << x << " y = " << y << endl << endl;
	 return 0;
}
// ch 8-2 a16
#include <iostream>

using namespace std;

class PrimeNumber {
public:
	PrimeNumber() {
		pn = 1;
	}
	
	PrimeNumber(int _pn) {
		pn = _pn;
	}
	
	int get() {
		return pn;
	}
	
	PrimeNumber& operator++() {
		for (int i=pn+1; i<999999; ++i) {
			int flag = true;
			for (int j=2; j<i; ++j) {
				if (i%j==0) {
					flag = false;
					break;
				}
			}
			if (flag) {
				pn = i;
				return *this;
			}
		}
		throw "error";
	}
	
	PrimeNumber& operator--() {
		for (int i=pn-1; i>1; --i) {
			bool flag = true;
			for (int j=2; j<i; ++j) {
				if (i%j==0) {
					flag = false;
					break;
				}
			}
			if (flag) {
				pn = i;
				return *this;
			}
		}
		throw "error";
	}

	PrimeNumber operator++(int) {
		PrimeNumber tmp = *this;
		++*this;
		++tmp;
		return tmp;
	}
	
	PrimeNumber operator--(int) {
		PrimeNumber tmp = *this;
		--tmp;
		--*this;
		return tmp;
	}
private:
	int pn;
};

int main()
{
	PrimeNumber p1, p2(13);
	cout << p1.get() << endl;
	cout << p2.get() << endl;
	PrimeNumber p3 = p1++;
	cout << p3.get() << endl;
	PrimeNumber p4 = p2++;
	cout << p4.get() << endl;
	PrimeNumber p5 = p2--;
	cout << p5.get() << endl;
	 return 0;
}