版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
高级程序设计实践(C++)课程设计报告题目复数计算器目录需求分析第1页第二章概要设计第2页第三章详细设计第4页第四章调试分析第21页第五章测试结果第25页第六章课程设计总结第36页第七章参考文献第37页第八章附录第37页C++程序设计之复数计算器第一章需求分析第52页第一章需求分析1.1程序设计的任务1.1.1编程目的1.本次程序设计的目的运用面向对象程序设计知识,利用C++语言设计和实现一个复数计算器,要求具备如下主要功能:(1)建立实数类、复数类(2)实现实数、复数信息的初始化(3)实现实数的加、减、乘、除、自增、自减、求平方、二次方根等操作(4)实现复数的加、减、乘、除、取模、求平方、求共轭复数、求单个复数的向量角、求两个复数的夹角等运算(5)实现实数、复数信息的输出1.1.2编程要求在实现过程中,需利用面向对象程序设计理论的基础知识,充分体现出C++语言关于类、继承、封装与多态等核心概念,每一个类应包含数据成员和成员函数。1.1.3数据范围数据输入的形式为float型,输出形式亦是float型,数据(实数、复数的实部、虚部)范围是-2^128~+2^128,也即-3.40E+38~+3.40E+38。1.2本程序能实现的功能1.2.1实数类(Complex)所实现的功能:实数的加法运算、减法运算、乘法运算、除法运算、前置自增运算、后置自增运算、前置自减运算、后置自减运算、求平方运算、求平方根运算;1.2.2复数类(Complex1)所实现的功能:复数的加法运算、减法运算、乘法运算、除法运算、模长运算、求平方运算、共轭复数运算、求单个复数的向量角运算、求两个复数的夹角运算。1.2.3主函数所能实现的功能1.提供给操作者操作数据的类型(实数还是复数)的标识;2.操作者选择数据类型后,提供运算类型操作的标识;3.运用指针调用两个类的各成员函数分别完成各项计;C++程序设计之复数计算器第二章概要设计第二章概要设计2.1构成部分2.1.1系统由三部分构成:实数类(Complex)、复数类(Complex1)、主函数main。2.1.2.构成部分关系复数类(Complex1)由实数类(Complex)派生而来,其中两者成员函数中的enter(输入数据)函数是虚函数,用以实现多态性。主函数main通过指针调用实数类(Complex)、复数类(Complex1)分别完成实数运算、复数运算。2.1.3.类层次实数类是父类,复数类是子类。2.2主程序流程以及程序模块之间的调用关系如图2.2—1所示: 输入flag判断flag判断wflag=1flag=2实数运算复数运算判断v输入flag判断flag判断wflag=1flag=2实数运算复数运算判断vw=3w=4w=5w=6……w=12v=3v=4v=5v=6……v=11乘法运算减法运算加法运算求复数间夹角除法运算乘法运算减法运算加法运算求平方运算除法运算乘法运算减法运算加法运算求复数间夹角除法运算乘法运算减法运算加法运算求平方运算除法运算图2.2--1主程序流程以及程序模块之间的调用关系图注:图2.2--1中没有画出flag=0时退出系统;flag不等于0,1,2时出现错误提示语句的选择结构;w,v在选择范围之外时出现错误提示语句的选择结构。特殊处理:除法运算分母为0;求二次方根数为负数;求单个复数夹角时判断象限的过程。C++程序设计之复数计算器第三章详细设计第三章详细设计3.1类层次中各个类的描述3.1.1.实数类(Complex)私有部分仅数据:float型数据real;公共部分包括12个成员函数,分别是:类的构造函数:Complex()实数加法运算函数:Complexoperator+(Complex&c1)实数减法运算函数:Complexoperator-(Complex&c1)实数乘法运算函数:Complexoperator*(Complex&c1)实数除法运算函数:Complexoperator/(Complex&c1)实数前置自增函数:Complexoperator++()实数后置自增函数:Complexoperator++(int)实数前置自减函数:Complexoperator--()实数后置自减函数:Complexoperator--(int)实数求平方根函数:floatRootOfTwo()实数求平方函数:floatSquare()实数输入数据函数:virtualvoidenter()3.1.2.复数类(Complex1)私有部分仅数据成员float型数据real和float型数据imag,二者分别代表复数的实部和虚部;公共部分包含11个成员函数,分别是:类的构造函数:Complex1()复数加法运算函数:Complex1operator+(Complex1&c1)复数减法运算函数:Complex1operator-(Complex1&c1)复数乘法运算函数:Complex1operator*(Complex1&c1)复数除法运算函数:Complex1operator/(Complex1&c1)复数求模长函数:floatLength()复数求平方函数:Complex1Square1()求共轭复数函数:Complex1Conjugate()复数求角度函数:floatAngle1()复数间求角度函数:floatAngle2(Complex1&c1)复数输入数据函数:voidenter()3.2类中各成员函数的定义3.2.1实数类(Complex)1类的构造函数函数体部分:Complex(){};2实数加法运算函数函数体部分Complexoperator+(Complex&c1){ Complexc; cout<<"theresultis:"<<endl; c.real=real+c1.real; cout<<real<<"+"<<c1.real<<"="<<c.real<<endl; returnc; }3实数减法运算函数函数体部分Complexoperator-(Complex&c1){ Complexc; cout<<"theresultis:"<<endl; c.real=real-c1.real; cout<<real<<"-"<<c1.real<<"="<<c.real<<endl; returnc; }4实数乘法运算函数函数体部分Complexoperator*(Complex&c1){ Complexc; c.real=real*c1.real; cout<<"theresultis:"<<endl; cout<<real<<"*"<<c1.real<<"="<<c.real<<endl; returnc; }5实数除法运算函数函数体部分Complexoperator/(Complex&c1){ Complexc; cout<<"theresultis:"<<endl; c.real=real/c1.real; cout<<real<<"/"<<c1.real<<"="<<c.real<<endl; returnc; }6实数前置自增函数函数体部分Complexoperator++()
{ cout<<"theresultis:"<<endl; cout<<"++"<<real; ++real; cout<<"="<<real<<endl; return*this; }7实数后置自增函数函数体部分Complexoperator++(int){ Complextemp(*this); cout<<"theresultis:"<<endl; cout<<real<<"++"; real++; cout<<"="<<real-1<<endl; cout<<real<<endl; returntemp; }8实数前置自减函数函数体部分Complexoperator--(){ cout<<"theresultis:"<<endl; cout<<"--"<<real; --real; cout<<"="<<real<<endl; return*this; }9实数后置自减函数函数体部分Complexoperator--(int){ Complextemp(*this); cout<<"theresultis:"<<endl; cout<<real<<"--"; real--; cout<<"="<<real+1<<endl; cout<<real<<endl; returntemp; }10实数求平方根函数函数体部分floatRootOfTwo(){ Complexc; if(real<0) { cout<<"thenumberiswrong!!!"<<endl; } else { cout<<"theresultis:"<<endl; c.real=sqrt(real); cout<<"sqrt"<<real<<"="<<c.real<<endl; } returnc.real; }11实数求平方函数函数体部分floatSquare(){ Complexc; cout<<"theresultis:"<<endl; c.real=real*real; cout<<"square"<<real<<"="<<c.real<<endl; returnc.real; }12实数输入数据函数(虚函数)函数体部分virtualvoidenter(){ cin>>real; }3.2.2复数类(Complex1)1.类的构造函数函数体部分Complex1(){};2.复数加法运算函数函数体部分Complex1operator+(Complex1&c1){ Complex1c; cout<<"theresultis:"<<endl; c.real=real+c1.real; c.imag=imag+c1.imag; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"+" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; }3复数减法运算函数函数体部分Complex1operator-(Complex1&c1){ Complex1c; cout<<"theresultis:"<<endl; c.real=real-c1.real; c.imag=imag-c1.imag; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"-" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; }4复数乘法运算函数函数体部分Complex1operator*(Complex1&c1){ Complex1c; cout<<"theresultis:"<<endl; c.real=real*c1.real-imag*c1.imag; c.imag=real*c1.imag+imag*c1.real; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"*" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; }5.复数除法运算函数函数体部分Complex1operator/(Complex1&c1){ Complex1c; cout<<"theresultis:"<<endl; c.real=(real*c1.real+imag*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag); c.imag=(imag*c1.real-real*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag); cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"/" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; }6.复数求模长函数函数体部分floatLength(){ floatx; cout<<"theresultis:"<<endl; x=sqrt(real*real+imag*imag); cout<<"|"<<real<<"+"<<imag<<"j"<<"|"<<"="<<x<<endl; returnx; }7.复数求平方函数函数体部分Complex1Square1(){ Complex1c; cout<<"theresultis:"<<endl; c.real=real*real-imag*imag; c.imag=real*imag+imag*real; cout<<"thesquareof"<<"("<<real<<"+"<<imag<<"j"<<")" <<"is"<<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; }8.求共轭复数函数函数体部分Complex1Conjugate(){ Complex1c; cout<<"theresultis:"<<endl; c.real=real; c.imag=imag*(-1); if(c.imag>=0) { cout<<"conjugatecomplexnumberof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; } if(c.imag<0) { cout<<"conjugatecomplexnumberof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<"("<<c.real<<c.imag<<"j"<<")"<<endl; } returnc; }8.复数求角度函数函数体部分floatAngle1(){ floaty; cout<<"theresultis:"<<endl; y=atan((imag)/(real))/i*180; if(real==0&&imag!=0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<90<<endl; } if(real>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y<<endl; } if(real<0&&imag>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y+180<<endl; } if(real<0&&imag<0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y-180<<endl; } returny; }9.复数间求角度函数函数体部分floatAngle2(Complex1&c1){ floatz,r,s,t; cout<<"theresultis:"<<endl; r=(real*c1.real+imag*c1.imag); s=sqrt(real*real+imag*imag); t=sqrt(c1.real*c1.real+c1.imag*c1.imag); z=acos(r/(s*t))/i*180; cout<<"theanglebetween"<<"("<<real<<"+"<<imag<<"j"<<")"<<"and" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"is" <<z<<endl; returnz; }10.复数输入数据函数(虚函数)函数体部分voidenter(){ cin>>real; cin>>imag; cout<<endl; }3.3主程序和重要模块的算法3.3.1主程序和重要模块具体代码如下:intmain()//主函数{ intflag,w,v; Complexgrad1,grad2; Complex*pt1,*pt2; Complex1*pm1,*pm2; Complex1revd1,revd2; cout<<"姓名:曾立弘"<<endl; cout<<"专业:电气信息工程"<<endl; cout<<"班级:1401班"<<endl; cout<<"学号:0903140114"<<endl; while(flag!=0)//while循环,flag=0时终止循环 { pt1=&grad1; pt2=&grad2; cout<<"Pleasechoosethetypeofthenumbers:"<<endl; cout<<"'0'exitsystem"<<endl; cout<<"'1'realnumber"<<endl; cout<<"'2'pluralnumber"<<endl; cin>>flag;if(flag==1)//条件语句:如果flag=1,则进行实数运算 { cout<<"pleasechoosetheoperationofthenumbers:"<<endl; cout<<"'3''+'"<<endl; cout<<"'4''-'"<<endl; cout<<"'5''*'"<<endl; cout<<"'6''/'"<<endl; cout<<"'7''++'"<<endl; cout<<"'8''--'"<<endl; cout<<"'9''++(int)'"<<endl; cout<<"'10''--(int)'"<<endl; cout<<"'11''rootoftwo'"<<endl; cout<<"'12''square'"<<endl; cin>>w; if(w==3) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator+(grad2); } if(w==4) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator-(grad2); } if(w==5) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator*(grad2); } if(w==6) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator/(grad2); } if(w==7) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->operator++(); } if(w==8) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->operator--(); } if(w==9) { cout<<"pleaseenterthenumber:"; pt1->enter(); grad1++; } if(w==10) { cout<<"pleaseenterthenumber:"; pt1->enter(); grad1--; } if(w==11) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->RootOfTwo(); } if(w==12) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->Square(); } if(w!=3&&w!=4&&w!=5&&w!=6&&w!=7&&w!=8&&w!=9&&w!=10&&w!=11&&w!=12) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 } pt1=&revd1; pt2=&revd2; pm1=&revd1; pm2=&revd2; if(flag==2)//条件语句:如果flag=1,则进行实数运算 { cout<<"pleasechoosetheoperationofthenumbers:"<<endl; cout<<"'3''+'"<<endl; cout<<"'4''-'"<<endl; cout<<"'5''*'"<<endl; cout<<"'6''/'"<<endl; cout<<"'7''Length'"<<endl; cout<<"'8''Square1'"<<endl; cout<<"'9''Conjugate'"<<endl; cout<<"'10''Angle1'"<<endl; cout<<"'11''Angle2'"<<endl; cin>>v; if(v==3) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator+(revd2); } if(v==4) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator-(revd2); } if(v==5) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator*(revd2); } if(v==6) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator/(revd2); } if(v==7) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Conjugate(); } if(v==8) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Square1(); } if(v==9) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Conjugate(); } if(v==10) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Angle1(); } if(v==11) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->Angle2(revd2); } if(v!=3&&v!=4&&v!=5&&v!=6&&v!=7&&v!=8&&v!=9&&v!=10&&v!=11) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 } if(flag!=1&&flag!=2&&flag!=0) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 if(flag!=0) { cout<<"\n"; cout<<"Pleasecarryoutthenextoperation:"<<endl;//继续运算提示 cout<<"\n"; } } cout<<"Welcomenexttime!"<<endl; return0;}3.4函数调用关系图3.4.1函数调用关系图如图3.4—1所示flag!=0其他其他输入w,w=?exiterror输入v,v=?3456789101112输出“pleasechoosecorrectoption!”“输入flagfloatLength()Complex1Conjugate()floatAngle1()Complexoperator++()Complex1operator+(Complex1&c1)Complex1operator-(Complex1&c1)Complex1operator*(Complex1&c1)Complex1operator/(Complex1&c1)floatAngle2(Complex1&c1)floatSquare()floatRootOfTwo()Complexoperator--(int)Complexoperator/(Complex&c1)Complexoperator*(Complex&c1)Complexoperator-(Complex&c1)Complexoperator+(Complex&c1)其他11109876543flag=2复数运算floatLength()Complex1Conjugate()floatAngle1()Complexoperator++()Complex1operator+(Complex1&c1)Complex1operator-(Complex1&c1)Complex1operator*(Complex1&c1)Complex1operator/(Complex1&c1)floatAngle2(Complex1&c1)floatSquare()floatRootOfTwo()Complexoperator--(int)Complexoperator/(Complex&c1)Complexoperator*(Complex&c1)Complexoperator-(Complex&c1)Complexoperator+(Complex&c1)其他11109876543flag=2复数运算flag=1实数运算flag=1实数运算Complexoperator++(int)Complexoperator++(int)3456789101112其他Complexoperator--()Complex1Conjugate()C++程序设计之复数计算器第四章调试分析第四章调试分析4.1调试过程遇到的问题及解决方法的得出过程4.1.1问题及分析过程1.对输出结果的界面没有太注意,导致输出结果不是很美观;2.对字母的拼写错误,导致前面定义的变量到后面编译器不认识;对固有写法的拼写错误,如将运算符重载函数写为operetor,导致运算符重载不成功。3.对一些模块的编写代码缺乏系统分析,主要体现在以下几个方面:(1)实数除法重载函数中没有考虑分母为0的情况,增加了if条件语句讨论;(2)实数求二次方根函数中没有考虑被开方数为负数的情况增加了if条件语句讨论;(3)求单个复数的向量角时没有将弧度制转化为角度制,定义了全局常变量i=3.1415927,通过弧度值除以i再乘以180将其转化为角度值;求单个复数的向量角时没有考虑象限的讨论,最后加了if选择结构讨论;求单个复数的向量角时没有分母(即实部real)为0的情况,增加了if条件语句讨论;具体讨论程序如下(实部为real,虚部为imag):floatAngle1()//求单个复数的夹角度数 { floaty; cout<<"theresultis:"<<endl; y=atan((imag)/(real))/i*180; if(real==0&&imag!=0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<90<<endl; } if(real>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y<<endl; } if(real<0&&imag>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y+180<<endl; } if(real<0&&imag<0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y-180<<endl; } returny; }(4)在调用虚函数时,修改之前是基类指针指向子类指针的这一步骤写在基类、子类的定义部分,导致下面先调用基类虚函数,后调用子类虚函数时会出现调用的时同一部分,即基类的数据。修改后时将基类指针指向子类指针的这一步骤写在调用基类虚函数和调用子类虚函数之间,从而实现了虚函数和多态性。(5)为使程序可以连续运算而不是每算一次就得重新运行使用了while循环,初始时将对象的地址赋给指针这一步骤写在while循环之外,发现经过多次循环后指针所指的对象已经不是原来的了,为使指针总是指向要求的对象,就必须在每一次循环开始时将其重新定位,即指向对象的开头。综上述原因,最后将将对象的地址赋给指针这一步骤写在while循环之内,从而问题得以解决。4.2算法的时间复杂性和可能的改进设想4.2.1算法的时间复杂性1.在主函数中的选择分支结构(本程序中未用switch结构,而是if结构)初始时为下面格式if(){ }else{if() { } else { if() { } else { if() { } else { } } } }以此来选择输入的指令并输出可能的错误形式,但考虑到上面结构可能会对运行速率造成影响,故改为以下形式:C++程序设计之复数计算器第五章测试结果if(){ }if(){ }if(){ }if(!=&&!=!=&&!=!=&&!=!=&&!=!=&&!=){ }来选择结构并输出可能的错误形式。2.指针和虚函数的运用。4.2.2可能的改进思想1.本程序在程序要求中明确说明要建立两个类,实数类和复数类。但个人认为可以直接建立一个复数类,并将两者所要求的函数全部归在这一个类中,因为实数可以用虚部为0的复数代替,唯一要加的就是为实数运算服务的函数使其运用默认参数的知识,令虚部的默认参数为了0即可。2.本程序在程序要求中明确说明要建立两个类,实数类和复数类。但个人认为可以仅仅建立一个复数类,不需要实数类,因为实数的运算可以直接定义函数来进行,而不是建立实数类再用类的成员函数来实现其母的。第五章测试结果5.1错误处理5.1.1初始界面5.1.2错误处理一5.1.3错误处理二5.1.4错误处理三5.1.5退出系统5.2测试数据(实数以3、7测试,若单个变量则取3;复数以-3-7j和5+5j测试,若单个变量取-3-7j)5.2.1实数部分1.加法运算2.减法运算3.乘法运算4.除法运算5前置自增运算6.前置自减运算7后置自增运算8后置自减运算9求平方根运算10.求平方运算5.2.2复数部分1.加法运算2.减法运算3.乘法运算4.除法运算5.求模长运算6.求平方运算7.求共轭复数运算8.求单个复数向量角运算9.求复数间夹角运算C++程序设计之复数计算器第六章课程设计总结第六章课程设计总结6.1三周程序编写及调试,在做“C++课程设计之复数计算器”这个项目中,发现的问题有以下几点:1.基本符号问题其一、本程序是在MicrosoftVisualC++6.0上面完成的,这可以说是一款比较智能的编译系统了,但即使如此,仍有括弧忘写、误删、只有一个括弧的问题,若在其他编译系统中书写,可能错误会更多;其二、书写混乱,在较长的代码段不是很整齐,对后面的书写带来极大的不便。2.知识掌握深度问题其一、对指针虽初步掌握,但不熟,有一些原则性的失误,如:在循环中,指针从开头移到结尾后,第二次循环开始时需将其重新固定到开头,否则会随机赋值进行运算;其二、对虚函数和多态性掌握不彻底,对将子类的地址赋给基类指针这一步骤的代码的位置在此前不是很清楚;其三、未弄清if语句和switch语句的优缺点;其四、对if语句的各式结构的优缺点未对比分析过;其五、对运算符重载函数作为类的成员函数和类的友元函数未细细比较。3.学习编程语言的习惯问题其一、注重书本而不注重上机,导致实践能力与知识掌握不是齐头并进。6.2对数据结构这门课的思考1.即使是一个小小的算法,或者实现一个并不是太复杂的功能,但对编程能力的要求非常严格。从一个初学者的态度来讲,要严格、严谨、务实、创新,否则,即使能够实现所有要求,但编译时间过长,影响用户的使用。2.数据结构这门知识可以说是由无数个小的板块构成的,在知识储存量相同的情况下,判断程序员优秀与否的差别在与这些模块的组合方式,组合方式很多,实现同一步骤的方式也很多。所以,学好这门课的关键是不仅是要会,更重要的是熟练。C++程序设计之复数计算器第七章参考文献第八章附录第七章参考文献【1】《C++程序设计》(第2版)谭浩强编著清华大学出版社第八章附录8.1带注释的源程序#include<iostream>#include<cmath>usingnamespacestd;constfloati=3.1415927;classComplex//实数类{ public: Complex(){};//构造函数 Complexoperator+(Complex&c1)//加法重载函数 { Complexc; cout<<"theresultis:"<<endl; c.real=real+c1.real; cout<<real<<"+"<<c1.real<<"="<<c.real<<endl; returnc; } Complexoperator-(Complex&c1)//减法重载函数 { Complexc; cout<<"theresultis:"<<endl; c.real=real-c1.real; cout<<real<<"-"<<c1.real<<"="<<c.real<<endl; returnc; } Complexoperator*(Complex&c1)//乘法重载函数 { Complexc; c.real=real*c1.real; cout<<"theresultis:"<<endl; cout<<real<<"*"<<c1.real<<"="<<c.real<<endl; returnc; } Complexoperator/(Complex&c1)//除法重载函数 { Complexc; cout<<"theresultis:"<<endl; c.real=real/c1.real; cout<<real<<"/"<<c1.real<<"="<<c.real<<endl; returnc; } Complexoperator++()//前置自增重载函数 { cout<<"theresultis:"<<endl; cout<<"++"<<real; ++real; cout<<"="<<real<<endl; return*this; } Complexoperator++(int)//后置自增重载函数 { Complextemp(*this); cout<<"theresultis:"<<endl; cout<<real<<"++"; real++; cout<<"="<<real-1<<endl; cout<<"afterthat:"<<real<<endl; returntemp; }C++程序设计之复数计算器第八章附录 Complexoperator--()//前置自减重载函数 { cout<<"theresultis:"<<endl; cout<<"--"<<real; --real; cout<<"="<<real<<endl; return*this; } Complexoperator--(int)//后置自减重载函数 { Complextemp(*this); cout<<"theresultis:"<<endl; cout<<real<<"--"; real--; cout<<"="<<real+1<<endl; cout<<"afterthat:"<<real<<endl; returntemp; } floatRootOfTwo()//求平方根函数 { Complexc; if(real<0) { cout<<"thenumberiswrong!!!"<<endl; } else { cout<<"theresultis:"<<endl; c.real=sqrt(real); cout<<"sqrt"<<real<<"="<<c.real<<endl; } returnc.real; } floatSquare()//求平方函数 { Complexc; cout<<"theresultis:"<<endl; c.real=real*real; cout<<"square"<<real<<"="<<c.real<<endl; returnc.real; } virtualvoidenter()//输入数据函数(虚函数) { cin>>real; } private: floatreal;//私有数据};classComplex1:publicComplex//类的共有继承生成复数类{public: Complex1(){};//构造函数 Complex1operator+(Complex1&c1)//加法重载函数 { Complex1c; cout<<"theresultis:"<<endl; c.real=real+c1.real; c.imag=imag+c1.imag; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"+" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; } Complex1operator-(Complex1&c1)//减法重载函数 { Complex1c; cout<<"theresultis:"<<endl; c.real=real-c1.real; c.imag=imag-c1.imag; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"-" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; } Complex1operator*(Complex1&c1)//乘法重载函数 { Complex1c; cout<<"theresultis:"<<endl; c.real=real*c1.real-imag*c1.imag; c.imag=real*c1.imag+imag*c1.real; cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"*" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; } Complex1operator/(Complex1&c1)//除法重载函数 { Complex1c; cout<<"theresultis:"<<endl; c.real=(real*c1.real+imag*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag); c.imag=(imag*c1.real-real*c1.imag)/(c1.real*c1.real+c1.imag*c1.imag); cout<<"("<<real<<"+"<<imag<<"j"<<")"<<"/" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"=" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; } floatLength()//求模长函数 { floatx; cout<<"theresultis:"<<endl; x=sqrt(real*real+imag*imag); cout<<"|"<<real<<"+"<<imag<<"j"<<"|"<<"="<<x<<endl; returnx; } Complex1Square1()//求平方函数 { Complex1c; cout<<"theresultis:"<<endl; c.real=real*real-imag*imag; c.imag=real*imag+imag*real; cout<<"thesquareof"<<"("<<real<<"+"<<imag<<"j"<<")" <<"is"<<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; returnc; } Complex1Conjugate()//求共轭复数 { Complex1c; cout<<"theresultis:"<<endl; c.real=real; c.imag=imag*(-1); if(c.imag>=0) { cout<<"conjugatecomplexnumberof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl; } if(c.imag<0) { cout<<"conjugatecomplexnumberof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<"("<<c.real<<c.imag<<"j"<<")"<<endl; } returnc; } floatAngle1()//求单个复数的夹角度数 { floaty; cout<<"theresultis:"<<endl; y=atan((imag)/(real))/i*180; if(real==0&&imag!=0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<90<<endl; } if(real>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y<<endl; } if(real<0&&imag>0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y+180<<endl; } if(real<0&&imag<0) { cout<<"theangleof" <<"("<<real<<"+"<<imag<<"j"<<")"<<"is" <<y-180<<endl; } returny; } floatAngle2(Complex1&c1)//求复数间的夹角度数函数 { floatz,r,s,t; cout<<"theresultis:"<<endl; r=(real*c1.real+imag*c1.imag); s=sqrt(real*real+imag*imag); t=sqrt(c1.real*c1.real+c1.imag*c1.imag); z=acos(r/(s*t))/i*180; cout<<"theanglebetween"<<"("<<real<<"+"<<imag<<"j"<<")"<<"and" <<"("<<c1.real<<"+"<<c1.imag<<"j"<<")"<<"is" <<z<<endl; returnz; } voidenter()//输入数据函数 { cin>>real; cin>>imag; cout<<endl; }private: floatreal;//私有变量 floatimag;//私有变量};intmain()//主函数{ intflag,w,v; Complexgrad1,grad2; Complex*pt1,*pt2; Complex1*pm1,*pm2; Complex1revd1,revd2; cout<<"姓名:曾立弘"<<endl; cout<<"专业:电子信息工程"<<endl; cout<<"班级:1401班"<<endl; cout<<"学号:0903140114"<<endl; while(flag!=0)//while循环,flag=0时终止循环 { pt1=&grad1; pt2=&grad2; cout<<"Pleasechoosethetypeofthenumbers:"<<endl; cout<<"'0'exitsystem"<<endl; cout<<"'1'realnumber"<<endl; cout<<"'2'pluralnumber"<<endl; cin>>flag;if(flag==1)//条件语句:如果flag=1,则进行实数运算 { cout<<"pleasechoosetheoperationofthenumbers:"<<endl; cout<<"'3''+'"<<endl; cout<<"'4''-'"<<endl; cout<<"'5''*'"<<endl; cout<<"'6''/'"<<endl; cout<<"'7''++'"<<endl; cout<<"'8''--'"<<endl; cout<<"'9''++(int)'"<<endl; cout<<"'10''--(int)'"<<endl; cout<<"'11''rootoftwo'"<<endl; cout<<"'12''square'"<<endl; cin>>w; if(w==3) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator+(grad2); } if(w==4) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator-(grad2); } if(w==5) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator*(grad2); } if(w==6) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pt1->operator/(grad2); } if(w==7) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->operator++(); } if(w==8) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->operator--(); } if(w==9) { cout<<"pleaseenterthenumber:"; pt1->enter(); grad1++; } if(w==10) { cout<<"pleaseenterthenumber:"; pt1->enter(); grad1--; } if(w==11) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->RootOfTwo(); } if(w==12) { cout<<"pleaseenterthenumber:"; pt1->enter(); pt1->Square(); } if(w!=3&&w!=4&&w!=5&&w!=6&&w!=7&&w!=8&&w!=9&&w!=10&&w!=11&&w!=12) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 } pt1=&revd1; pt2=&revd2; pm1=&revd1; pm2=&revd2; if(flag==2)//条件语句:如果flag=2,则进行复数运算 { cout<<"pleasechoosetheoperationofthenumbers:"<<endl; cout<<"'3''+'"<<endl; cout<<"'4''-'"<<endl; cout<<"'5''*'"<<endl; cout<<"'6''/'"<<endl; cout<<"'7''Length'"<<endl; cout<<"'8''Square1'"<<endl; cout<<"'9''Conjugate'"<<endl; cout<<"'10''Angle1'"<<endl; cout<<"'11''Angle2'"<<endl; cin>>v; if(v==3) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator+(revd2); } if(v==4) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator-(revd2); } if(v==5) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator*(revd2); } if(v==6) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->operator/(revd2); } if(v==7) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Length(); } if(v==8) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Square1(); } if(v==9) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Conjugate(); } if(v==10) { cout<<"pleaseenterthenumber:"; pt1->enter(); cout<<endl; pm1->Angle1(); } if(v==11) { cout<<"pleaseenterfirstnumber:"; pt1->enter(); cout<<endl; cout<<"pleaseentersecondnumber:"; pt2->enter(); cout<<endl; pm1->Angle2(revd2); } if(v!=3&&v!=4&&v!=5&&v!=6&&v!=7&&v!=8&&v!=9&&v!=10&&v!=11) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 } if(flag!=1&&flag!=2&&flag!=0) { cout<<"pleasechoosecorrectoption!"<<endl; }//出错提示 if(flag!=0) { cout<<"\n"; cout<<"Pleasecarryoutthenextoperation:"<<endl;//继续运算提示 cout<<"\n"; } } cout<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 知觉的选择性微电影分库周欣然
- 住院病案首页管理制度
- 伽马刀术后病人护理
- 学校教师师德培训
- 数学学案:课堂导学集合之间的关系
- 医疗图文排版
- 《高分子材料》课件
- 介绍高适课件
- 《奇特的植物》课件
- 《光敏电阻及其应用》课件
- 绘本三只小猪完整版课件
- 2023-《商标注册用商品和服务国际分类》第十版
- 眼化学烧伤课件
- 骨质疏松知识培训课件
- 四年级上册数学期中试卷讲评教案
- 能源化学能量转换与储存
- 大象版2022-2023三年级科学上册4.3《空气的流动》课件
- 计算机基础全套完整版ppt教学教程最新最全
- 304不锈钢管材质证明书
- 中国书画书法等级考试二级样题(附学生答卷)(真题)
- 《汽车电工电子技术》全套教案(完整版)
评论
0/150
提交评论