重载运算符Word文件下载.docx

上传人:b****5 文档编号:21428379 上传时间:2023-01-30 格式:DOCX 页数:25 大小:20.05KB
下载 相关 举报
重载运算符Word文件下载.docx_第1页
第1页 / 共25页
重载运算符Word文件下载.docx_第2页
第2页 / 共25页
重载运算符Word文件下载.docx_第3页
第3页 / 共25页
重载运算符Word文件下载.docx_第4页
第4页 / 共25页
重载运算符Word文件下载.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

重载运算符Word文件下载.docx

《重载运算符Word文件下载.docx》由会员分享,可在线阅读,更多相关《重载运算符Word文件下载.docx(25页珍藏版)》请在冰豆网上搜索。

重载运算符Word文件下载.docx

}

(consttring&

op)

intk;

k=strcmp(str,op.str);

returnk;

};

intmain()

chara1[100],a2[100];

intn;

while(cin>

>

a1>

a2)

tringA1(a1);

tringA2(a2);

n=A1>

A2;

cout<

<

n<

endl;

return0;

}

1002:

ProblemB

定义一个字符串类,该类包括一个字符型指针数据成员,构造函数、析构函数、显示字符串函数,以及重载=运算符函数(用于字符串赋值)。

在主函数中对字符串对象的赋值运算进行测试。

输入数据有多行,每行包括一个字符串。

输出有多行,对应每个输入数据要求输出两行,第一行是该输入的字符串对象,第二行是定义另一个对象进行赋值运算,将第一个对象赋值给第二个对象。

Hello!

530ithinkyou!

classA

private:

char*arr;

public:

A(){}

A(char*s)

arr=newchar[strlen(s)+1];

strcpy(arr,s);

~A()

{}

A&

operator=(constA&

s)

arr=newchar[strlen(s.arr)+1];

strcpy(arr,s.arr);

return*this;

voidprint()

arr<

chara[100];

while(cin.getline(a,100,'

\n'

))

Aa1(a);

Aa2;

a1.print();

a2=a1;

a2.print();

1003:

ProblemC

设计一个计数器Counter,用类成员重载自增运算符实现计数器的自增,用友元重载实现计数器的自减。

输入数据有多组,每组包含两个整数。

第一个数m为计数器的基数,第二个数n(n>

0为计数器增加,n<

0计数器减少)的数。

输出计数器自增或自减的结果。

每组输出占一行。

110

5-4

11

classcounter

counter(intx){n=x;

counteroperator++(){++n;

return*this;

counteroperator++(int){countert=*this;

n++;

returnt;

friendcounteroperator--(counter&

p){--p.n;

returnp;

p,int){p.n--;

voidprint()

{cout<

intm,n,i;

m>

n)

counterc(m);

if(n>

0)

for(i=0;

i<

n;

i++;

c++;

elsefor(i=0;

i>

i--)

c--;

c.print();

运算符重载——重载+和-对复数类对象进行加减运算

ProblemDescription

定义一个复数类,该类包含两个double型的数据成员代表复数的实部和虚部,包含构造函数(默认值为0,0),和显示函数,现重载运算符+和-,使其能对复数类对象进行加和减运算。

在主函数中进行测试

输入数据有多行,每行包括4个数,前两个代表参与运算的第一个复数对象的实部和虚部,后两个代表第二个复数对象的实部和虚部。

输出多行,每行包括了加和减运算后的结果。

10020-1020

10232034

2.563.51.3

(90,40)(110,0)

(30,57)(-10,-11)

(6,7.3)(-1,4.7)

iostream.h>

classComplex

Complex(doubler=0.0,doublei=0.0);

friendComplexoperator+(Complex&

a,Complex&

b);

friendComplexoperator-(Complex&

voidprint1()

"

("

real<

"

imag<

)"

"

;

voidprint2()

doublereal;

doubleimag;

Complex:

:

Complex(doubler,doublei)

real=r;

imag=i;

Complexoperator+(Complex&

b)

Complextemp;

temp.real=a.real+b.real;

temp.imag=a.imag+b.imag;

returntemp;

Complexoperator-(Complex&

temp.real=a.real-b.real;

temp.imag=a.imag-b.imag;

doublea1,b1;

doublea2,b2;

b1>

a2>

b2)

Complexop1(a1,b1);

Complexop2(a2,b2);

Complexop3,op4;

op3=op1+op2;

op3.print1();

op4=op1-op2;

op4.print2();

string>

classPoint

doublea,b,c,d;

Point(doublei=0,doublej=0,doublek=0,doublel=0)

a=i;

b=j;

c=k;

d=l;

voidsd()

a+c<

b+d<

voidsd1()

a-c<

b-d<

a>

b>

c>

d)

PointA(a,b,c,d);

A.sd();

A.sd1();

classPro

{

doublex1,y1;

Pro()

x1=0,y1=0;

Pro(doublea1,doublea2)

x1=a1;

y1=a2;

x1<

y1<

friendProoperator+(Pro&

m,Pro&

n);

friendProoperator-(Pro&

Prooperator+(Pro&

Prol;

l.x1=m.x1+n.x1;

l.y1=m.y1+n.y1;

returnl;

Prooperator-(Pro&

Prok;

k.x1=m.x1-n.x1;

k.y1=m.y1-n.y1;

Protoal1,toal2;

doublea1,b1,a2,b2;

Proob1(a1,b1);

Proob2(a2,b2);

toal1=ob1+ob2;

toal2=ob1-ob2;

toal1.print();

cout<

toal2.print();

设计一个复数类,实现复数的加减乘除计算功能:

首先输入整数t,表示有t组数据,每组数据奇数行为m,n分别为一个复数的实部和虚部,偶数行为‘+’、‘-’、‘*’、‘/’、‘=’任意一个字符,根据偶数行的输入决定其前后两行的运算。

运算一律从前往后算。

若偶数行输入为‘=’,则结束运算,并输出运算结果。

输出最后计算的结果,以m+ni活m-ni,其中m,n分别为复数的实部和虚部(如果m为0,则不用输出)。

若除0,则停止计算,并输出”dividebyzero”的信息。

3

12

+

45

*

22

=

/

2.34.5

00

-4+24i

dividebyzero

复数

classcomplex

complex(doubler=0,doublei=0)

voidprint()const

if(real!

=0)

{

cout<

real;

if(imag>

cout<

+"

if(imag!

i"

}

friendcomplexoperator+(constcomplex&

a,constcomplex&

friendcomplexoperator-(constcomplex&

friendcomplexoperator*(constcomplex&

friendcomplexoperator/(constcomplex&

complexoperator+(constcomplex&

complext;

t.real=a.real+b.real;

t.imag=a.imag+b.imag;

returnt;

complexoperator-(constcomplex&

t.real=a.real-b.real;

t.imag=a.imag-b.imag;

complexoperator*(constcomplex&

t.real=a.real*b.real-a.imag*b.imag;

t.imag=a.real*b.imag+a.imag*b.real;

complexoperator/(constcomplex&

doublet1;

t1=1/(b.real*b.real+b.imag*b.imag);

t.real=(a.real*b.real+a.imag*b.imag)*t1;

t.imag=(b.real*a.imag-a.real*b.imag)*t1;

doublea,b,x,y;

charc;

intt;

cin>

t;

while(t--)

intk=0;

cin>

x>

y;

complexd(x,y);

while(cin>

c,c!

='

cin>

b;

complexe(a,b);

if(c=='

+'

d=d+e;

else

if(c=='

-'

d=d-e;

else

if(c=='

/'

{

if(a==0&

&

b==0)

{

k=1;

}

d=d/e;

}

else

if(c=='

*'

d=d*e;

if(k==0)

d.print();

else

if(k==1)

dividebyzero"

设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。

输入数据有多组,每组占二行,每行包括3个整数,表示三角形的三条边(保证能构成三角形)。

对于每组数据,输出占一行。

先是第1个三角形的面积,然后是第2个三角形的面积,接下来是两个面积之和。

中间用空格隔开。

345

456SampleOutput

6.009.9215.92

cmath>

iomanip>

classTriangle

doublex,y,z;

doublep,m;

staticdoubles;

Triangle(doublea,doubleb,doublec)

x=a;

y=b;

z=c;

p=(y+x+z)/2;

m=sqrt(p*(p-x)*(p-y)*(p-z));

s=s+m;

setiosflags(ios:

fixed);

setprecision

(2)<

m<

staticvoidshow()

s<

s=0;

doubleTriangle:

s=0;

doublea,b,c,a1,b1,c1;

c)

{

c1;

Triangles1(a,b,c);

Triangles2(a1,b1,c1);

s1.print();

s2.print();

Triangle:

show();

1162:

求矩阵之和

设计一个矩阵类Data,包括矩阵的行、列、矩阵数据等私有数据成员。

要求实现两个矩阵的加。

输入数据有多组,每组输入数据有三行,第一行两个整数,分别代表矩阵的行数m和列数n;

第二行有m*n个整数,是第一个矩阵的数据;

第三行也有m*n个整数,是第二个矩阵的数据。

对于每组输入数据,输出两个矩阵的和,具体格式见样例。

33

111222333

111111111

SampleOutput

222

333

444

classDate

intm,n,s[100];

Date(inta,intb,intc[])

m=a;

n=b;

inti;

for(i=1;

=m*n;

i++)

s[i]=c[i];

Date(){}

friendDateoperator+(Date&

a1,Date&

inti,s[100];

=a1.m*a1.n;

s[i]=a1.s[i]+a2.s[i];

returnDate(a1.m,a2.n,s);

s[i]<

if(i%n==0)

intm,n,i,a[100],b[100];

a[i];

b[i];

Datea1(m,n,a),a2(m,n,b),d;

d=a1+a2;

d.print();

Description

设计一个字符串类MyString,通过运算符重载实现字符串的以下运算。

classMyString

intlength;

//字符串的长度

char*ptr;

//指向字符串的指针

MyString(constchar*s="

//构造函数

~MyString();

//析构函数

 

MyString&

operator=(constMyString&

ms);

//重载赋值运算符=

booloperator<

(constMyString&

ms)const;

//字符串的小于比较<

booloperator>

//字符串的大于于比较>

friendostream&

operator<

(ostream&

os,constMyString&

//重载输出运算符

friendistream&

operator>

(istream&

is,MyString&

//重载输入运算符

Input

输入数据有多组,每组数据占两行,每行包含一串文本(长度不超过100)。

Output

对于每组数据,输出比较大的那个字符串,占一行。

SampleInput

abc

cba

Cba

classstu{

char*ch;

char*sh;

stu(charch1[],charsh1[])

{ch=newchar[strlen(ch1)+1];

sh=newchar[strlen(sh1)+1];

strcpy(ch,ch1);

strcpy(sh,sh1);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1