欢迎来到天天文库
浏览记录
ID:41692380
大小:76.99 KB
页数:11页
时间:2019-08-30
《复数运算实验报告》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、复数加减乘除的实验报告一、实验目的:了解类和对象的概念,利用类来解决问题,同时熟悉运算符重载的定义,并学会使用运算符重载。二、实验内容及流程1、定义一个复数类Compex,公有成员为7个成员函数,构①造函数Compex(double二0.0,double二0.0)实现了对象的初始化,Compexopcrator+(constCompex&)const;Compexoperator-(constCompex&)const;Compexoperator*(constCompex&)const;Compexoperator/(cons
2、tCompex&)const;Compex&operator二(constCompex&);符号+-*/不用定义函数,就能够实现整形、浮点型、等一些预定义类型的加减乘除的运算,是因为在文件内部已将定义好这些函数对这些符号进行重载,对象是类的实例化,是新的数据类型,所以要定义函数对利用这些符号进行对象的加减乘除。voidprint()const;是现实复数的函数,包括复数的实部和虚部是否为零,正负等情况②私有成员为两个数据包括实部real和虚部imaginaryo2、程序的设计流程图三、程序源代码#include"stdafx・h
3、〃#includeusingnamespacestd;classCompex{public:Compex(double二0.0,double二0.0);Compexoperator+(constCompex&)const;Compexoperator-(constCompex&)const;Compexoperator*(constCompex&)const;Compexoperator/(constCompex&)const;Compex&operator二(constCompex&);voidprint(
4、)const;private:doublereal;doubleimaginary;};Compex::Compex(doubler,doublei){real=r;imaginary=i;}//析构函数voidCompex::print()const{i.f(real<0){if(imaginary<0)cout〈〈〃C/<5、ginary<6、Compex&operand2)const{Compexsum;sum.real=real+operand2.real;sum.imaginary=imaginary+operand2・imaginary;returnsum;}//+运算符的重载CompexCompex::operator-(constCompex&operand2)const{Compexdiff;diff.real=real-operand2・real;diff.imaginary二imaginai'y-operand2.imaginary;returndi7、ff:}//见运算符的重载CompexCompex::operator*(constCompex&opersnd2)const{Compexdiff;diff.real=rea1*operand?.real-imaginary*operand2.imaginary;diff.imaginaTy=real*opeTand2・imaginary+ope]:、and2.real^imaginary;returndiff:}〃乘运算符的重载CompexCompex::operator/(constCompex&operand2)cons8、t{Compexdiff:diff.real=(real*operand2・real+imaginary*operand2.imaginaj、y)/(operand2.real*operand2.real+operand2.imdginary*operand2.
5、ginary<6、Compex&operand2)const{Compexsum;sum.real=real+operand2.real;sum.imaginary=imaginary+operand2・imaginary;returnsum;}//+运算符的重载CompexCompex::operator-(constCompex&operand2)const{Compexdiff;diff.real=real-operand2・real;diff.imaginary二imaginai'y-operand2.imaginary;returndi7、ff:}//见运算符的重载CompexCompex::operator*(constCompex&opersnd2)const{Compexdiff;diff.real=rea1*operand?.real-imaginary*operand2.imaginary;diff.imaginaTy=real*opeTand2・imaginary+ope]:、and2.real^imaginary;returndiff:}〃乘运算符的重载CompexCompex::operator/(constCompex&operand2)cons8、t{Compexdiff:diff.real=(real*operand2・real+imaginary*operand2.imaginaj、y)/(operand2.real*operand2.real+operand2.imdginary*operand2.
6、Compex&operand2)const{Compexsum;sum.real=real+operand2.real;sum.imaginary=imaginary+operand2・imaginary;returnsum;}//+运算符的重载CompexCompex::operator-(constCompex&operand2)const{Compexdiff;diff.real=real-operand2・real;diff.imaginary二imaginai'y-operand2.imaginary;returndi
7、ff:}//见运算符的重载CompexCompex::operator*(constCompex&opersnd2)const{Compexdiff;diff.real=rea1*operand?.real-imaginary*operand2.imaginary;diff.imaginaTy=real*opeTand2・imaginary+ope]:、and2.real^imaginary;returndiff:}〃乘运算符的重载CompexCompex::operator/(constCompex&operand2)cons
8、t{Compexdiff:diff.real=(real*operand2・real+imaginary*operand2.imaginaj、y)/(operand2.real*operand2.real+operand2.imdginary*operand2.
此文档下载收益归作者所有