第十一章 多态性练习

网易云音乐链接 : 点我听歌.

1、定义复数类的加法与减法,使之能够执行下列运算:
complex a(2,5),b(7,8),c(0,0);
c=a+b;
c=4.1+a;
c=b-5.6;

代码

#include <iostream.h>
#include <string.h>

//using namespace std;
class Complex
{
	double real;
	double imag;
public:
	Complex() 
	{real=0.0; imag=0.0;}
	Complex(double r, double i)
	{real = r; imag = i;}
	void show()
	{cout<<real<<" + "<<imag<<"i"<<endl;}
	Complex operator+(const Complex &y)				//做为成员函数-对象加法
	{
		Complex t(0,0);
		t.real = real + y.real;
		t.imag = imag + y.imag;
		return t;
	}
	Complex operator-(const Complex &y1)				//做为成员函数-对象减法
	{
		Complex t(0,0);
		t.real = real - y1.real;
		t.imag = imag - y1.imag;
		return t;
	}
	
	Complex operator+(const double v)
	{
		Complex t(0,0);
		t.real = v + real;
		t.imag = imag;
		return t;
	}
	Complex operator-(const double v1)
	{
		Complex t(0,0);
		t.real = v1 - real;
		t.imag = -imag;
		return t;
	}

	friend Complex operator-(double, Complex &);
	friend Complex operator+(double, Complex &);
};

Complex operator+(double c1, Complex &c2) 
{ 
	Complex c; 
	c.real=c2.real+c1; 
	c.imag=c2.imag; 
	
	return c; 
} 
Complex operator-(double c1, Complex &c2) 
{ 
 Complex c; 
 c.real=c1-c2.real; 
 c.imag=-c2.imag; 

 return c; 
} 


void main()
{
	Complex a(2, 5);Complex b(7, 8);Complex c;
	c = a + b;
	c.show();
	c = 4.1 + a;
	c.show();
	c=b-5.6;
	c.show();
}

运行结果

在这里插入图片描述

◇ 2、设计一个2行3列的矩阵类,重载运算符“+”和“-”,能实现矩阵类对象的加减运算;重载流插入运算符“<<”和流提取运算符“>>”,使之能用于矩阵的输入和输出。

代码

#include <iostream.h>

class matrix
{
	private:
		int a[2][3];	
	public:
		friend matrix operator+(matrix&A, matrix&B);
		friend ostream& operator<<(ostream&, matrix&);
		friend istream& operator>>(istream&, matrix&);
		matrix(){};
};
 
matrix operator+(matrix&A,matrix&B)
{
	matrix c;
	for(int i=0;i<2;i++)
     for(int j=0;j<3;j++)
      c.a[i][j]=A.a[i][j]+B.a[i][j];
	return c;
}
 
istream & operator >> (istream &input,matrix &a)
{
    cout<<"请输入矩阵: "<<endl;
  for(int i=0;i<2;i++)
   for(int j=0;j<3;j++)
    input>>a.a[i][j];
  return input;
}
 
ostream & operator << (ostream &output,matrix &C)
{
  cout<<"矩阵相加的结果为:"<<endl; 
  for(int i=0;i<2;i++)
  {
   for(int j=0;j<3;j++)
    output<<C.a[i][j]<<" ";
   cout<<endl;
	}
  return output;
}
 
void main()
{
	matrix A,B,C;
	cin>>A;
	cin>>B;
	C=A+B;
	cout<<C;
}

运行结果

在这里插入图片描述
◇ 3、定义一个人民币的类,其中成员数据包括元、角、分,成员函数包括构造及输出函数。要求增加适当的成员函数,重载“+”“-”“+=”“++”及输入输出流,实现人民币的直接运算。注意分、角、元的进位。

代码

#include <iostream.h>
#include <string.h>
#include <iomanip.h>

class RMB
{
	int yuan, jiao ,fen;
public:
	RMB(int y=0, int j=0, int f=0)
	{
		yuan = y; jiao = j; fen = f;
	}
	void show()
	{
		cout<<yuan<<"."<<jiao<<fen<<endl;
	}
	RMB operator+(RMB &r);								//成员函数重载加运算符
	friend RMB operator-(RMB &r1, RMB &r2);				//用友元函数形式重载减
	RMB& operator+=(RMB &r);							//只能用成员函数形式重载+=
	RMB& operator++();									//成员函数形式重载++
	friend istream &operator>>(istream &input, RMB &);	
	friend ostream &operator<<(ostream &output, RMB &); //只能用友元重载输入输出流
};
RMB RMB::operator+(RMB &r)
{
	RMB rs;
	rs.fen=fen+r.fen;
	while(rs.fen>=10)
	{
		rs.jiao++;
		rs.fen-=10;
	}
	rs.jiao=jiao+r.jiao;
	while(rs.jiao>=10)
	{
		rs.yuan++;
		rs.jiao-=10;
	}
	rs.yuan=yuan+r.yuan;
	
	return rs;
}

RMB operator-(RMB &r1, RMB &r2)
{
	RMB rs;
	if(r1.yuan<r2.yuan)
	{
		cout<<"钱不够,不能付账!
";
		return rs;
	}
	if(r1.yuan==r2.yuan&&r1.jiao<r2.jiao)
	{
		cout<<"钱不够,不能付账!
";
		return rs;
	}
	if(r1.yuan==r2.yuan&&r1.jiao==r2.jiao&&r1.fen<r2.fen)
	{
		cout<<"钱不够,不能付账!
";
		return rs;
	}

	rs.yuan=r1.yuan-r2.yuan;
	rs.jiao=r1.jiao-r2.jiao;
	while(rs.jiao<0)
	{
		rs.yuan--;
		rs.jiao+=10;
	}
	rs.fen=r1.fen-r2.fen;
	while(rs.fen<0)
	{
		rs.jiao--;
		if(rs.jiao<0)
		{
			rs.yuan--;
			rs.jiao+=10;
		}
		rs.fen+=10;
	}
	return rs;
}

RMB& RMB::operator+=(RMB &r)				//返回对象本身的引用
{
	fen=fen+r.fen;
	while(fen>=10)
	{
		jiao++;
		fen-=10;
	}
	jiao=jiao+r.jiao;
	while(jiao>=10)
	{
		yuan++;
		jiao-=10;
	}
	yuan=yuan+r.yuan;
	return *this;
}
RMB& RMB::operator++(void)				//对象本身加
{
	fen=fen+1;
	while(fen>=10)
	{
		jiao++;
		fen-=10;
	}
	while(jiao>=10)
	{
		yuan++;
		jiao-=10;
	}
	return *this;
}
istream &operator>>(istream &input, RMB &r)
{
	cout<<"请输入元、角、分(整数之间用空格或回车符间隔):";
	input>>r.yuan>>r.jiao>>r.fen;

	return input;
}	
ostream &operator<<(ostream &output, RMB &r) //只能用友元重载输入输出流
{
	while(r.fen>=10)
	{
		r.jiao++;
		r.fen-=10;
	}
	while(r.jiao>=10)
	{
		r.yuan++;
		r.jiao-=10;
	}
	output<<"¥"<<setw(3)<<r.yuan<<"."<<r.jiao<<r.fen;//输出到输出流的引用

	return output;
}
void main()
{
	RMB r1(14, 5, 7),r2,r3;
	cout<<"请输入一个人民币数值:
";
	cin>>r2;
	r3=r1+r2;

	cout<<r1<<" + "<<r2<<" = "<<r3<<endl;
	cout<<r3<<" + "<<r1<<" = ";
	r3+=r1;
	cout<<r3<<endl;
	cout<<r3<<" - "<<r1<<" = ";
	r3=r3-r1;
	cout<<r3<<endl;
	cout<<r3<<" + "<<"¥ 0.01=";
	++r3;
	cout<<r3<<endl;
}

运行结果

在这里插入图片描述
◇ 4、定义一个时间的类,其中成员数据包括小时、分、秒,成员函数为构造函数。要求增加适当的成员函数,重载“+”“-”“+=”及输入输出流,实现时间类对象的直接输入输出及两个时间的运算。

代码

#include <iostream.h>
#include <iomanip.h>
#include <time.h>

class TIME
{
	int hour, minute, second;
public:
	TIME(int h=0, int m=0, int s=0)
	{
		hour=h; minute=m; second=s;
	}
	TIME operator+(TIME &t);			//成员函数形式重载
	friend double operator-(TIME &t1, TIME &t2);
	TIME& operator+=(TIME &t);			//只能用成员函数
	//TIME& operator++();
	friend istream &operator>>(istream &input, TIME &);
	friend ostream &operator<<(ostream &output, TIME &);
};

TIME TIME::operator+(TIME &t)			//成员函数形式重载
{
	TIME ot;
	ot.second=second+t.second;
	while(ot.second>=60)
	{
		ot.minute++;
		ot.second-=60;
	}
	ot.minute+=minute+t.minute;
	while(ot.minute>=60)
	{
		ot.hour++;
		ot.minute-=60;
	}
	ot.hour=hour+t.hour;
	if(ot.hour>=24)
		ot.hour-=24;

	return ot;
}

double operator-(TIME &t1, TIME &t2)
{
	double minute;
	if((t1.hour<t2.hour)||(t1.hour==t2.hour&&t1.minute<t2.minute)||(t1.hour==t2.hour&&t1.minute==t2.minute
		&&t1.second<t2.second))
	{
		cout<<"不能相减!
";
		return 0;
	}
	minute=(t1.hour-t2.hour)*60;
	minute=t1.minute-t2.minute;
	if(t1.second-t2.second)
		minute--;

	return minute;
}

TIME& TIME::operator+=(TIME &t)			//只能用成员函数
{
	second=second+t.second;
	if(second>60)
	{
		minute++;
		second-=60;
	}
	minute=minute+t.minute;
	while(minute>=60)
	{
		hour++;
		minute-=60;
	}
	hour=hour+t.hour;
	if(hour>=24)
		hour=hour-24;
	return *this;

}

//TIME& operator++();

istream &operator>>(istream &input, TIME &t)
{
	cout<<"请输入时间(按tip输入小时、分、秒):
";
	cout<<"请输小时(大于0,小于等于23):";
	input>>t.hour;
	while(t.hour>23||t.hour<0)
	{
		cout<<"输入的小时没按要求输入!请重新时输入!";
		input>>t.hour;
	}
	cout<<"请输分钟(大于等于0,小于等于59):";
	input>>t.minute;
	while(t.minute>59||t.minute<0)
	{
		cout<<"输入的分钟没按要求输入!请重新时输入!";
		input>>t.minute;
	}
	cout<<"请输秒数(大于等于0,小于等于59):";
	input>>t.minute;
	while(t.second>59||t.second<0)
	{
		cout<<"输入的秒数没按要求输入!请重新时输入!";
		input>>t.second;
	}

	return input;
}

ostream &operator<<(ostream &output, TIME &t)
{
	output<<setw(2)<<t.hour<<":"<<t.minute<<":"<<t.second;

	return output;
}

void main()
{
	struct tm *CurrentTime;	
	time_t time_date(0);  //两个变量获取当前时间
	CurrentTime=localtime(&time_date);//设置当前时间
	TIME currenttime(CurrentTime->tm_hour,CurrentTime->tm_min,CurrentTime->tm_sec);
	//将当前时间赋值给时间类对象
	
	cout<<"
		欢迎使用简易时间计算器!
";
	cout<<"
	当前时间是:"<<currenttime<<endl;
	int Choice;	//输入选择用
	TIME t1, t2, t3;
	double m;

	do
	{
		cout<<"

	 1.时间加法
";
		cout<<"

	 2.时间减法
";
		cout<<"

	 3.时间自身加法(+=)
";
		cout<<"

	 0.退出时间计算器
";
		cout<<"
	 请选择你需要的操作:";
		cin>>Choice;
		switch(Choice)
		{
			case 1:
			{
				cout<<"		 时间加法,请按提示输入两个时间:

";
				cin>>t1;
				cout<<endl;
				cin>>t2;
				t3=t1+t2;
				cout<<endl<<t1<<" + "<<t2<<" = "<<t3<<endl;
				break;
			}
			case 2:
			{
				cout<<"		 时间减法,请按提示输入两个时间:

";
				cin>>t1;
				cout<<endl;
				cin>>t2;
				m=t1-t2;
				cout<<endl<<t1<<" - "<<t2<<" = "<<m<<"分钟"<<endl;
				break;
			}
			case 3:
			{
				cout<<"		 时间自身加法,请按提示输入两个时间:

";
				cin>>t1;
				cout<<endl;
				cin>>t2;
				t3=t1+t2;
				cout<<endl<<t1<<" +=  "<<t2;
				t1+=t2;
				cout<<t1<<endl;
				break;
			}
			case 0:			
			{
				break;
			}
			default://未选择0~3数字,重新选择
				cout<<"输入错误,请重新输入你的
";
				break;
		}
	}while(Choice != 0);
	cout<<"

	 欢迎再次使用简易时间计算器,谢谢!"<<endl;
}

运行结果

在这里插入图片描述

◇ 5、用弦截法求以下方程的根。
(1) f1(x)=xxx+xx-3x+1,初值为x1=0.5,x2=1.5。
(2) f2(x)=x
x-2x-8,初值为x1=-3,x2=3。
(3) f3(x)=xxx+2x*x+2x+1,初值为x1=-2,x2=3。

(1) f1(x)=xxx+xx-3x+1,初值为x1=0.5,x2=1.5*

代码1

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double f(double);                                             //函数声明
double xpoint(double,double);                                 //函数声明
double root(double,double);                                   //函数声明

int main( )
{
    double x1,x2,f1,f2,x;
    do
    { cout<< "input x1,x2:";
    cin >> x1 >> x2;
    f1=f(x1);
    f2=f(x2);
    } while(f1*f2 >= 0);
    x = root(x1,x2);
    cout << setiosflags(ios::fixed) << setprecision(7);       //指定输出7位小数
    cout << "A root of equation is " << x << endl;
    return 0;
}


double f(double x)                                       //定义f函数
{ double y;
  y = x * x * x + x * x-3*x+1;
  return y;
}

double xpoint(double x1,double x2)                       //定义spoint 函数,求出弦与 x 轴的交点
{ double y;
y = ( x1 * f(x2) - x2 * f(x1))/(f(x2) - f(x1));          //在 xpoint 函数中调用 f 函数
return y;
}

double root(double x1,double x2)                         //定义 root 函数,求近似根
{
    double x,y,y1;
    y1 = f(x1);
    do
    { x = xpoint(x1,x2);                                 //在 root 函数中调用 xpoint 函数
    y = f(x);                                            //在 root 函数中调用 f 函数
    if(y*y1 > 0)
    { y1 = y;
    x1 = x;
    }
    else
        x2 = x;
    }while(fabs(y)>= 0.00001);
    return x;
}

运行结果1

在这里插入图片描述
(2) f2(x)=x*x-2x-8,初值为x1=-3,x2=3。

代码2

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double f(double);                                             //函数声明
double xpoint(double,double);                                 //函数声明
double root(double,double);                                   //函数声明

int main( )
{
    double x1,x2,f1,f2,x;
    do
    { cout<< "input x1,x2:";
    cin >> x1 >> x2;
    f1=f(x1);
    f2=f(x2);
    } while(f1*f2 >= 0);
    x = root(x1,x2);
    cout << setiosflags(ios::fixed) << setprecision(7);       //指定输出7位小数
    cout << "A root of equation is " << x << endl;
    return 0;
}


double f(double x)                                       //定义f函数
{ double y;
  y = x*x-2*x-8;
  return y;
}

double xpoint(double x1,double x2)                       //定义spoint 函数,求出弦与 x 轴的交点
{ double y;
y = ( x1 * f(x2) - x2 * f(x1))/(f(x2) - f(x1));          //在 xpoint 函数中调用 f 函数
return y;
}

double root(double x1,double x2)                         //定义 root 函数,求近似根
{
    double x,y,y1;
    y1 = f(x1);
    do
    { x = xpoint(x1,x2);                                 //在 root 函数中调用 xpoint 函数
    y = f(x);                                            //在 root 函数中调用 f 函数
    if(y*y1 > 0)
    { y1 = y;
    x1 = x;
    }
    else
        x2 = x;
    }while(fabs(y)>= 0.00001);
    return x;
}

运行结果2

在这里插入图片描述

(3) f3(x)=xxx+2x*x+2x+1,初值为x1=-2,x2=3。

代码3

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double f(double);                                             //函数声明
double xpoint(double,double);                                 //函数声明
double root(double,double);                                   //函数声明

int main( )
{
    double x1,x2,f1,f2,x;
    do
    { cout<< "input x1,x2:";
    cin >> x1 >> x2;
    f1=f(x1);
    f2=f(x2);
    } while(f1*f2 >= 0);
    x = root(x1,x2);
    cout << setiosflags(ios::fixed) << setprecision(7);       //指定输出7位小数
    cout << "A root of equation is " << x << endl;
    return 0;
}


double f(double x)                                       //定义f函数
{ double y;
  y = x*x*x+2*x*x+2*x+1;
  return y;
}

double xpoint(double x1,double x2)                       //定义spoint 函数,求出弦与 x 轴的交点
{ double y;
y = ( x1 * f(x2) - x2 * f(x1))/(f(x2) - f(x1));          //在 xpoint 函数中调用 f 函数
return y;
}

double root(double x1,double x2)                         //定义 root 函数,求近似根
{
    double x,y,y1;
    y1 = f(x1);
    do
    { x = xpoint(x1,x2);                                 //在 root 函数中调用 xpoint 函数
    y = f(x);                                            //在 root 函数中调用 f 函数
    if(y*y1 > 0)
    { y1 = y;
    x1 = x;
    }
    else
        x2 = x;
    }while(fabs(y)>= 0.00001);
    return x;
}

运行结果3

在这里插入图片描述

千里之行,始于足下!
原文地址:https://www.cnblogs.com/MINAIot/p/13040983.html