C++作业7文档格式.docx

上传人:b****4 文档编号:16404197 上传时间:2022-11-23 格式:DOCX 页数:18 大小:132.17KB
下载 相关 举报
C++作业7文档格式.docx_第1页
第1页 / 共18页
C++作业7文档格式.docx_第2页
第2页 / 共18页
C++作业7文档格式.docx_第3页
第3页 / 共18页
C++作业7文档格式.docx_第4页
第4页 / 共18页
C++作业7文档格式.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

C++作业7文档格式.docx

《C++作业7文档格式.docx》由会员分享,可在线阅读,更多相关《C++作业7文档格式.docx(18页珍藏版)》请在冰豆网上搜索。

C++作业7文档格式.docx

classDoubleSubscriptedArray

{

friendostream&

operator<

(ostream&

constDoubleSubscriptedArray&

);

friendistream&

operator>

(istream&

DoubleSubscriptedArray&

public:

DoubleSubscriptedArray(int=3,int=5);

DoubleSubscriptedArray(constDoubleSubscriptedArray&

~DoubleSubscriptedArray();

int&

operator()(int=0,int=0);

operator()(int=0,int=0)const;

booloperator==(constDoubleSubscriptedArray&

)const;

booloperator!

=(constDoubleSubscriptedArray&

right)const

{

return!

(*this==right);

}

constDoubleSubscriptedArray&

operator=(constDoubleSubscriptedArray&

);

intgetRows()const

returnrows;

intgetColumns()const

returncolumns;

private:

introws;

intcolumns;

int*ptr;

};

#endif

DoubleSubscriptedArray.cpp

cerr;

cout;

cin;

endl;

stdlib.h>

iomanip>

setw;

#include"

DoubleSubscriptedArray.h"

 

DoubleSubscriptedArray:

DoubleSubscriptedArray(introw,intcolumn)

rows=(row>

0)?

row:

3;

columns=(column>

column:

5;

ptr=newint[rows*columns];

for(inti=0;

i<

rows;

i++)

for(intj=0;

j<

columns;

j++)

ptr[i*columns+j]=0;

}

DoubleSubscriptedArray(constDoubleSubscriptedArray&

arrayToCopy)

rows(arrayToCopy.rows),columns(arrayToCopy.columns)

ptr=newint[rows*columns];

ptr[i*columns+j]=arrayToCopy.ptr[i*columns+j];

~DoubleSubscriptedArray()

delete[]ptr;

boolDoubleSubscriptedArray:

operator==(constDoubleSubscriptedArray&

right)const

if(rows!

=right.rows||columns!

=right.columns)

returnfalse;

{

if(ptr[i*columns+j]!

=right.ptr[i*columns+j])

returnfalse;

}

returntrue;

constDoubleSubscriptedArray&

operator=(constDoubleSubscriptedArray&

right)

if(&

right!

=this)

if(rows!

delete[]ptr;

rows=right.rows;

columns=right.columns;

ptr=newint[rows*columns];

for(inti=0;

for(intj=0;

ptr[i*columns+j]=right.ptr[i*columns+j];

return*this;

int&

operator()(introw,intcolumn)

if(rows<

=row||columns<

=column)

cerr<

"

\nError:

Subscript["

<

row<

]["

column<

]outofrange"

endl;

exit

(1);

returnptr[row*columns+column];

operator()(introw,intcolumn)const

row||columns<

column)

istream&

input,DoubleSubscriptedArray&

a)

a.rows;

a.columns;

input>

a.ptr[i*a.columns+j];

returninput;

ostream&

output,constDoubleSubscriptedArray&

output<

setw(10)<

output<

returnoutput;

test_DoubleSubscripted.cpp

intmain()

introw,column;

cout<

请输入你想要创建的数组的行数与列数:

"

cin>

row>

column;

DoubleSubscriptedArrayarray1(row,column);

这个"

array1.getRows()<

行"

array1.getColumns()<

列的数组中,元素是:

array1;

请给这个数组中的元素赋值..."

赋值完后的数组是:

请随便创建另一个数组,输入行数、列数:

DoubleSubscriptedArrayarray2(row,column);

array2;

if(array1==array2)

cout<

\n这两个数组相等。

if(array1!

=array2)

\n这两个数组不相等。

现在自动创建一个与第一个数组相等的数组:

DoubleSubscriptedArrayarray3(array1);

array3;

\n现在将第二个数组复制给第三个数组:

array3=array2;

if(array3==array2)

if(array3!

\narray1[3][5]is"

array1(3,5);

\nAssigning1000toarray1[3][5]"

array1(3,5)=1000;

\nAssigning1000toarray1[13][15]"

array1(13,15)=1000;

return0;

2、考虑图11.19至图11.21中给出的Complex类。

该类可以对所谓的复数进行操作。

复数的形式如下:

realPart(实部)+imaginaryPart(虚部)*i,其中i的值为√-1。

a)修改Complex类,使其通过重载的>

和<

运算符分别输入和输出复数(应该删除该类的print函数)。

b)重载乘法运算符,使两个复数能够执行代数乘法。

c)重载==和!

=运算符,支持复数之间的比较。

Complex.h

#ifndefCOMPLEX_H

#defineCOMPLEX_H

#include<

classComplex

input,Complex&

output,constComplex&

Complex(double=0,double=0);

Complexoperator+(constComplex&

Complexoperator-(constComplex&

Complexoperator*(constComplex&

booloperator==(constComplex&

=(constComplex&

operand2)

(*this==operand2);

doublereal;

doubleimaginary;

Complex.cpp

Complex.h"

Complex:

Complex(doublerealPart,doubleImaginaryPart)

real=realPart;

imaginary=ImaginaryPart;

ComplexComplex:

operator+(constComplex&

operand2)const

returnComplex(real+operand2.real,imaginary+operand2.imaginary);

operator-(constComplex&

returnComplex(real-operand2.real,imaginary-operand2.imaginary);

operator*(constComplex&

returnComplex(real*operand2.real-imaginary*operand2.imaginary,

real*operand2.imaginary+operand2.real*imaginary);

boolComplex:

operator==(constComplex&

if(real==operand2.real&

&

imaginary==operand2.imaginary)

returntrue;

returnfalse;

complex)

input.ignore();

input>

complex.real;

input.ignore

(2);

complex.imaginary;

output,constComplex&

output<

("

setw

(2)<

complex.real<

"

complex.imaginary<

)"

;

test_Complex.cpp

Complexcomplex1,complex2;

请按(a,b)的格式输入复数1.."

complex1;

getchar();

请按(a,b)的格式输入复数2.."

complex2;

if(complex1==complex2)

这两个复数相等。

if(complex1!

=complex2)

这两个复数不相等。

complex1<

+"

complex2<

="

complex1+complex2<

-"

complex1-complex2<

*"

complex1*complex2<

很遗憾没做出可以连续相加或者连续相减、连续相乘的。

那几个运算符重载的函数如果返回引用就会产生警告。

而且不能得到正确结果。

3、创建类RetionalNumber(分数),使其具备下列能力:

a)创建一个构造函数,它可以防止分数的分母为0.如果分数不是化简形式,它可以进行化简。

而且它还可以避免分母为负数。

b)针对该类,重载加法、减法、乘法和除法运算符。

c)针对该类,重载关系和相等运算符。

RetionalNumber.h

#ifndefRETIONALNUMBER_H

#defineRETIONALNUMBER_H

classRetionalNumber

operator>

RetionalNumber&

operator<

constRetionalNumber&

RetionalNumber(int=1,int=1);

constRetionalNumberoperator+(constRetionalNumber&

constRetionalNumberoperator-(constRetionalNumber&

constRetionalNumberoperator*(constRetionalNumber&

constRetionalNumberoperator/(constRetionalNumber&

booloperator==(constRetionalNumber&

)const;

=(constRetionalNumber&

right)const

booloperator<

(constRetionalNumber&

booloperator>

right)const

returnright<

*this;

(right<

*this);

(*this<

right);

RetionalNumber&

setZuiJian(RetionalNumber&

//利用分子分母同时除以最大公约数化简

intfenzi;

intfenmu;

RetionalNumber.cpp

RetionalNumber.h"

RetionalNumber:

Re

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

当前位置:首页 > 表格模板 > 合同协议

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

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