大学生C++教程第九版 习题(10.8)

大学生C++教程第九版 习题(10.8)_第1张图片

#include 
#include 
#include 

using namespace std;

int main()
{
	int i=1;
	cout << "N" << setw(12) << "10*N" << setw(15) << "100*N" << setw(15) << "1000*N" << endl;

	while (i <= 5)
	{
		cout << i<< setw(12) << 10*i << setw(15) << 100*i<< setw(15) << 1000*i<< endl;
		i++;
	}
	return 0;
}

 

//Complex.cpp

#include "Complex.h"


Complex::Complex(double realPart, double imaginaryPart) :real(realPart),
imaginary(imaginaryPart)
{

}


Complex::~Complex()
{
}

Complex Complex::operator+(const Complex &operand2) const
{
	return Complex(real + operand2.real,imaginary+
		operand2.imaginary);
}

Complex Complex::operator-(const Complex &operand2) const
{
	return Complex(real-operand2.real,imaginary+operand2.imaginary);
}

void Complex::print() const
{
	cout << "(" << real << "," << imaginary << ")";
}

ostream &operator<<(ostream &out, Complex &fx)
{
	if (fx.imaginary == 0)
		cout << fx.real;
	else
	out << fx.real << "+"<
//驱动程序

#include 
#include "Complex.h"
using namespace std;

int main()
{
	Complex x;
	Complex y(4.3,8.3);
	Complex z(3.3,1.1);

	cout << "x:";
	//x.print();
	cout << x << endl;
	cout << "\ny:";
	//y.print();
	cout << y << endl;
	cout << endl;
	cout << "\nz:";
	//z.print();
	cout << z << endl;
	//

	x = y + z;
	cout << "\n\nx=y+z:" << endl;
	x.print();
	cout << "=";
	y.print();
	cout << "+";
	z.print();

	x = y - z;
	cout << "\n\nx=y-z:" << endl;
	x.print();
	cout << "=";
	y.print();
	cout << "-";
	z.print();
	cout << endl;

	cout << "\n\nx*y="<

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(C++)