c++运算符重载习题

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Task8-1
/*
1. 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。

将运算符函数重载为非成员、非友元的普通函数。

编写程序,求两个复数之和*/
#include<iostream>
using namespace std;
class Complex
{
public:
|
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
void display();
double real;
double imag;
};
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)";
*
}
Complex operator +(Complex &c1,Complex &c2)
{
Complex p;
=+;
=+;
return p;
}
int main()

{
Complex c1(3,5),c2(2,5),c3;
();
cout<<"+";
();
cout<<"=";
c3=c1+c2;
();
}
)
Task8-2
/*2. 定义一个复数类Complex,重载运算符“+”、”-“、”*”、”/”,
使之能用于复数的加、减、乘、除。

运算符重载函数作为Complex类的成员函数,编程,分别求两个复数之和差积商。

*/
#include<iostream>
using namespace std;
class Complex
{public:

Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;

};
Complex Complex::operator +(Complex &c2)
{Complex c;
=real+;
=imag+;
return c;
}
Complex Complex::operator -(Complex &c2)
{Complex c;
=;
$
=;
return c;
}
Complex Complex::operator *(Complex &c2)
{Complex c;
=real*;
=imag*;
return c;
}
Complex Complex::operator /(Complex &c2)
"
{Complex c;
=(real*+imag*/*+*;
=(imag**/*+*;
return c;
}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{Complex c1(3,4),c2(5,-10),c3;
|
c3=c1+c2;
cout<<"c1+c2=";
();
c3=c1-c2;
cout<<"c1-c2=";
();
c3=c1*c2;
cout<<"c1*c2=";
();
c3=c1/c2;
[
cout<<"c1/c2=";
();
return 0;
}
Task8-3 /*
3. 有两个矩阵a和b,均为n行m列(m、n的值自己给出),
求两个矩阵之和、差、积、商,重载运算符“+”、”-“、”*”、”/”,
使之能用于矩阵向加减乘除,如c=a+b、c=a*b。

?
*/
#include <iostream>
#define n 2
#define m 3
using namespace std;
class Matrix
在第三题的基础上,重载运算符>>和<<, 使之能用于该矩阵的输入和输出。

*/
#include <iostream>
#define n 2
#define m 3
~
using namespace std;
class Matrix
实现分数类中的运
算符重载,在分数类中可以完成分数的加减乘除
(运算后再化简)、求反、比较(6种关系)的运算。

*/
#include<iostream>
#include<>
using namespace std;
int gcd(int m,int n);
class Fraction
{

private:
int nume; // 分子
int deno; // 分母
public:
Fraction(int nu=0,int de=1); //构造函数,初始化用void Set(int nu=0,int de=1); //置值,改变值时用Fraction operator+(Fraction &c2);
Fraction operator-(Fraction &c2);
Fraction operator*(Fraction &c2);
Fraction operator/(Fraction &c2);
.
bool operator>=(Fraction &c2);
Fraction operator-();
void display();
void Simplify(); //化简(使分子分母没有公因子)
};
Fraction::Fraction(int nu,int de)
{
if(de!=0)
{

nume=nu;
deno=de;
}
else
{
cout<<"格式错误,程序退出\n";
exit(0);
}
}
!
void Fraction:: Set(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void Fraction::display()
-
{
cout<<nume<<'/'<<deno<<endl;
}
//求分子,分母最大公约数
int gcd(int m,int n)
{
int r;
if(m<n)
{
r=m;

m=n;
n=r;
}
while(r=m%n)
{
if(r==0)
break;
m=n;
n=r;
}

return n;
}
//将分数化简
void Fraction::Simplify()
{
int n=gcd(nume,deno);
nume=nume/n;
deno=deno/n;
cout<<nume<<'/'<<deno;
?
}
Fraction Fraction::operator+(Fraction &c2) {
Fraction c;
=deno*;
=nume*+*deno;
}
Fraction Fraction::operator-(Fraction &c2) {

Fraction c;
=deno*;
=nume* return c;
}
Fraction Fraction::operator*(Fraction &c2) {
Fraction c;
=deno*;
=nume*;
return c;
'
}
Fraction Fraction::operator/(Fraction &c2) {
Fraction c;
=deno*;
=nume*;
return c;
}

bool Fraction::operator>=(Fraction &c2) {
nume=nume*;
=*deno;
if(nume>=
return true;
else
return false;
^
}
Fraction Fraction::operator-()
{
Fraction c;
=-nume;
=deno;
}
int main()
{
\
Fraction c1(1,2),c2(5,8),c;
cout<<"c1=";
();
cout<<"c2=";
();
c=c1+c2;
cout<<"c1+c2=";
();
cout<<endl;
c=c1-c2;
cout<<"c1-c2=";
();
cout<<endl;
c=c1*c2;
cout<<"c1*c2=";
();
cout<<endl;
c=c1/c2;
cout<<"c1/c2=";
();
cout<<endl;
c=-c1;
cout<<"-c1=";
();
cout<<endl;
if(c1>=c2) cout<<"c1>=c2"<<endl; system("pause");
}。

相关文档
最新文档