欢迎来到天天文库
浏览记录
ID:35539544
大小:108.54 KB
页数:4页
时间:2019-03-25
《c课程设计复数计算器》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验一复数ADT的描述及实现1.实验要求用抽象数据类型描述complex类,要求实现复数的构造(三个构造函数:缺省构造函数、带有参数的构造函数和拷贝构造函数),设计getreaKgetimag甫数来实现复数的取值,setreal>setimag函数设定复数的实部和虚部,用操作符重载实现复数的加法、减法和输出。2.程序功能说明在用户界血中用户可以选择将要进行的操作,该程序可以实现复数的输入、输115、加法、减法。3.概要设计(1)抽象数据类型ADTcomplex!数据对象:数据关系:基本操作:D={cl,c2,c3,c4
2、cl,c2,c3,c4均为double型}R={
3、,,)创建复数setreal(),setimag();取复数的值getreal(),getimag();复数的加法;复数的减法;}ADTcomplex;(2)主函数voidmain(){complexcl,c2,c3,c4;inti=l;while(i>=0&&iv5){coutw"******cout«ncout«Hcout«Hcout«Hcout«H1.设置复数cln«endl;2.设置复数c2H«endl;3.计算两个复数之和H«endl;4.计算两个复数Z差n«endl;0.退出程序H«endl;cout«ft7,11rTwrTwrTwrTwrV
4、wnwrTwrTwrTwrTwrTwMWrTwrTwrTwrTwrTwrTwrTweTwrTweTwrTweTwrTw■;•■;.eTwrTwrTwMWrTwrTwrTwrTwrTwrYw«endl;cout«H请用户输入操作序号:”;cin»i;switch(i){case1:c1.setimagO,cl.setreal();break;case2:c2.setimagO,c2.setrecil();break;case3:c3=cl+c2;cout«"("«c3.getreal()«,*,H<5、c2;cout«n(n«c4.getreal()«,H«c4.getimag()«n)n«endl;break;caseO:exit(l);break;4.详要设计类定义:#ifndefCOMPLEX_H#defineCOMPLEX.Hclasscomplex{private:doublerealjmag;public:complex(){real=0.0;imag=0.0;}//缺省构造函数complex(doubler,doublei){real=r;imag=i;)//带参数的构造函数complex(complex&p);〃拷贝构造函数voidsetreal();voids6、etimagO;doublegetreal(){returnreal;)doublegetimag(){returnimag;)complexoperator+(constcomplex&c2)const;//操作符”的重载complexoperator-(constcomplex&c2)const;//操作符的重载};#endif类实现:#includeHcomplex.hH#includeusingnamespacestd;complex::complex(complex&p){real=p.real;imag=p.imag;Jcomplexcomplex:7、:operator+(constcomplex&c2)const{returncomplex(real+c2.reaUimag+c2.imag);}complexcomplex::operator-(constcomplex&c2)const{returncomplex(real-c2.realjmag-c2.imag);}voidcomplex::setreal(){cout«n输入实部:”;cin»real;)voidcomplex::setimag(){cout«"输入虚部:”;cin»imag;}主函数:#includencomplex.hH#include8、m>usingnamespacestd;voidmain(){complexcl,c2,c3,c4;inti=l;while(i>=0&&i<5){“^11•丄■•丄“•丄■•丄“•丄“•丄■«£«•丄■•£*•丄■•丄■•丄■•丄■fIf^7^彳.^7^#r^^7^%■^7^%■^7^彳.^7^^7^cout«ncout«Hcoul«ncout«Hcout«H1.设置复数cr*«endl;2.设置复数c2H«endl;3.计
5、c2;cout«n(n«c4.getreal()«,H«c4.getimag()«n)n«endl;break;caseO:exit(l);break;4.详要设计类定义:#ifndefCOMPLEX_H#defineCOMPLEX.Hclasscomplex{private:doublerealjmag;public:complex(){real=0.0;imag=0.0;}//缺省构造函数complex(doubler,doublei){real=r;imag=i;)//带参数的构造函数complex(complex&p);〃拷贝构造函数voidsetreal();voids
6、etimagO;doublegetreal(){returnreal;)doublegetimag(){returnimag;)complexoperator+(constcomplex&c2)const;//操作符”的重载complexoperator-(constcomplex&c2)const;//操作符的重载};#endif类实现:#includeHcomplex.hH#includeusingnamespacestd;complex::complex(complex&p){real=p.real;imag=p.imag;Jcomplexcomplex:
7、:operator+(constcomplex&c2)const{returncomplex(real+c2.reaUimag+c2.imag);}complexcomplex::operator-(constcomplex&c2)const{returncomplex(real-c2.realjmag-c2.imag);}voidcomplex::setreal(){cout«n输入实部:”;cin»real;)voidcomplex::setimag(){cout«"输入虚部:”;cin»imag;}主函数:#includencomplex.hH#include8、m>usingnamespacestd;voidmain(){complexcl,c2,c3,c4;inti=l;while(i>=0&&i<5){“^11•丄■•丄“•丄■•丄“•丄“•丄■«£«•丄■•£*•丄■•丄■•丄■•丄■fIf^7^彳.^7^#r^^7^%■^7^%■^7^彳.^7^^7^cout«ncout«Hcoul«ncout«Hcout«H1.设置复数cr*«endl;2.设置复数c2H«endl;3.计
8、m>usingnamespacestd;voidmain(){complexcl,c2,c3,c4;inti=l;while(i>=0&&i<5){“^11•丄■•丄“•丄■•丄“•丄“•丄■«£«•丄■•£*•丄■•丄■•丄■•丄■fIf^7^彳.^7^#r^^7^%■^7^%■^7^彳.^7^^7^cout«ncout«Hcoul«ncout«Hcout«H1.设置复数cr*«endl;2.设置复数c2H«endl;3.计
此文档下载收益归作者所有