资源描述
高级程序设计实践(C++)
课程设计报告
题 目 复数计算器
目录
第一章 需求分析.................................第1页
第二章 概要设计.................................第2页
第三章 详细设计.................................第4页
第四章 调试分析................................第21页
第五章 测试结果................................第25页
第六章 课程设计总结............................第36页
第七章 参考文献................................第37页
第八章 附录....................................第37页
C++程序设计之复数计算器 第一章 需求分析
第一章 需求分析
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.运用指针调用两个类的各成员函数分别完成各项计;
第52页
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
判断w
flag=1
flag=2
实数运算
复数运算
判断v
w=3 w=4 w=5 w=6 …… w=12 v=3 v=4 v=5 v=6 …… v=11
乘法运算
减法运算
加法运算
求复数间夹角
除法运算
乘法运算
减法运算
加法运算
求平方运算
除法运算
图2.2--1主程序流程以及程序模块之间的调用关系图
注:图2.2--1中没有画出
1. flag=0时退出系统;
2. flag不等于0,1,2时出现错误提示语句的选择结构;
3. w,v在选择范围之外时出现错误提示语句的选择结构。
4. 特殊处理:
除法运算分母为0;求二次方根数为负数;求单个复数夹角时判断象限的过程。
C++程序设计之复数计算器 第三章 详细设计
第三章 详细设计
3.1类层次中各个类的描述
3.1.1.实数类(Complex)
私有部分仅数据:float型数据 real;
公共部分包括12个成员函数,分别是:
类的构造函数:Complex()
实数加法运算函数: Complex operator+(Complex &c1)
实数减法运算函数:Complex operator-(Complex &c1)
实数乘法运算函数:Complex operator*(Complex &c1)
实数除法运算函数:Complex operator/(Complex &c1)
实数前置自增函数:Complex operator++()
实数后置自增函数:Complex operator++(int)
实数前置自减函数:Complex operator--()
实数后置自减函数:Complex operator--(int)
实数求平方根函数:float RootOfTwo()
实数求平方函数:float Square()
实数输入数据函数:virtual void enter()
3.1.2.复数类(Complex1)
私有部分仅数据成员float 型数据real和float 型数据imag,二者分别代表复数的实部和虚部;
公共部分包含11个成员函数,分别是:
类的构造函数:Complex1()
复数加法运算函数:Complex1 operator+(Complex1 &c1)
复数减法运算函数:Complex1 operator-(Complex1 &c1)
复数乘法运算函数: Complex1 operator*(Complex1 &c1)
复数除法运算函数:Complex1 operator/(Complex1 &c1)
复数求模长函数:float Length()
复数求平方函数:Complex1 Square1()
求共轭复数函数:Complex1 Conjugate()
复数求角度函数:float Angle1()
复数间求角度函数:float Angle2(Complex1 &c1)
复数输入数据函数:void enter()
3.2类中各成员函数的定义
3.2.1实数类(Complex)
1 类的构造函数
函数体部分:Complex(){};
2实数加法运算函数
函数体部分
Complex operator+(Complex &c1)
{
Complex c;
cout<<"the result is: "<<endl;
c.real=real+c1.real;
cout<<real<<"+"<<c1.real<<"="<<c.real<<endl;
return c;
}
3实数减法运算函数
函数体部分
Complex operator-(Complex &c1)
{
Complex c;
cout<<"the result is: "<<endl;
c.real=real-c1.real;
cout<<real<<"-"<<c1.real<<"="<<c.real<<endl;
return c;
}
4实数乘法运算函数
函数体部分
Complex operator*(Complex &c1)
{
Complex c;
c.real=real*c1.real;
cout<<"the result is: "<<endl;
cout<<real<<"*"<<c1.real<<"="<<c.real<<endl;
return c;
}
5实数除法运算函数
函数体部分
Complex operator/(Complex &c1)
{
Complex c;
cout<<"the result is: "<<endl;
c.real=real/c1.real;
cout<<real<<"/"<<c1.real<<"="<<c.real<<endl;
return c;
}
6实数前置自增函数
函数体部分
Complex operator++()
{
cout<<"the result is: "<<endl;
cout<<"++"<<real;
++real;
cout<<"="<<real<<endl;
return *this;
}
7实数后置自增函数
函数体部分
Complex operator++(int)
{
Complex temp(*this);
cout<<"the result is: "<<endl;
cout<<real<<"++";
real++;
cout<<"="<<real-1<<endl;
cout<<real<<endl;
return temp;
}
8实数前置自减函数
函数体部分
Complex operator--()
{
cout<<"the result is: "<<endl;
cout<<"--"<<real;
--real;
cout<<"="<<real<<endl;
return *this;
}
9实数后置自减函数
函数体部分
Complex operator--(int)
{
Complex temp(*this);
cout<<"the result is: "<<endl;
cout<<real<<"--";
real--;
cout<<"="<<real+1<<endl;
cout<<real<<endl;
return temp;
}
10实数求平方根函数
函数体部分
float RootOfTwo()
{
Complex c;
if(real<0)
{
cout<<"the number is wrong!!!"<<endl;
}
else
{
cout<<"the result is: "<<endl;
c.real=sqrt(real);
cout<<"sqrt"<<real<<"="<<c.real<<endl;
}
return c.real;
}
11实数求平方函数
函数体部分
float Square()
{
Complex c;
cout<<"the result is: "<<endl;
c.real=real*real;
cout<<"square"<<real<<"="<<c.real<<endl;
return c.real;
}
12实数输入数据函数(虚函数)
函数体部分
virtual void enter()
{
cin>>real;
}
3.2.2复数类(Complex1)
1.类的构造函数
函数体部分
Complex1(){};
2. 复数加法运算函数
函数体部分
Complex1 operator+(Complex1 &c1)
{
Complex1 c;
cout<<"the result is: "<<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;
return c;
}
3复数减法运算函数
函数体部分
Complex1 operator-(Complex1 &c1)
{
Complex1 c;
cout<<"the result is: "<<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;
return c;
}
4复数乘法运算函数
函数体部分
Complex1 operator*(Complex1 &c1)
{
Complex1 c;
cout<<"the result is: "<<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;
return c;
}
5.复数除法运算函数
函数体部分
Complex1 operator/(Complex1 &c1)
{
Complex1 c;
cout<<"the result is: "<<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;
return c;
}
6.复数求模长函数
函数体部分
float Length()
{
float x;
cout<<"the result is: "<<endl;
x=sqrt(real*real+imag*imag);
cout<<"|"<<real<<"+"<<imag<<"j"<<"|"<<"="<<x<<endl;
return x;
}
7.复数求平方函数
函数体部分
Complex1 Square1()
{
Complex1 c;
cout<<"the result is: "<<endl;
c.real=real*real-imag*imag;
c.imag=real*imag+imag*real;
cout<<" the square of "<<"("<<real<<"+"<<imag<<"j"<<")"
<<" is "<<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl;
return c;
}
8.求共轭复数函数
函数体部分
Complex1 Conjugate()
{
Complex1 c;
cout<<"the result is: "<<endl;
c.real=real;
c.imag=imag*(-1);
if(c.imag>=0)
{
cout<<"conjugate complex number of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<"("<<c.real<<"+"<<c.imag<<"j"<<")"<<endl;
}
if(c.imag<0)
{
cout<<"conjugate complex number of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<"("<<c.real<<c.imag<<"j"<<")"<<endl;
}
return c;
}
8.复数求角度函数
函数体部分
float Angle1()
{
float y;
cout<<"the result is: "<<endl;
y=atan((imag)/(real))/i*180;
if(real==0&&imag!=0)
{
cout<<"the angle of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<90<<endl;
}
if(real>0)
{
cout<<"the angle of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<y<<endl;
}
if(real<0&&imag>0)
{
cout<<"the angle of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<y+180<<endl;
}
if(real<0&&imag<0)
{
cout<<"the angle of"
<<"("<<real<<"+"<<imag<<"j"<<")"<<" is "
<<y-180<<endl;
}
return y;
}
9.复数间求角度函数
函数体部分
float Angle2(Complex1 &c1)
{
float z,r,s,t;
cout<<"the result is: "<<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<<"the angle between"<<" ("<<real<<"+"<<imag<<"j"<<") "<<"and"
<<" ("<<c1.real<<"+"<<c1.imag<<"j"<<") "<<" is "
<<z<<endl;
return z;
}
10.复数输入数据函数(虚函数)
函数体部分
void enter()
{
cin>>real;
cin>>imag;
cout<<endl;
}
3. 3主程序和重要模块的算法
3.3.1主程序和重要模块具体代码如下:
int main() //主函数
{
int flag,w,v;
Complex grad1,grad2;
Complex *pt1,*pt2;
Complex1 *pm1,*pm2;
Complex1 revd1,revd2;
cout<<"姓名: 曾立弘"<<endl;
cout<<"专业: 电气信息工程"<<endl;
cout<<"班级: 1401班"<<endl;
cout<<"学号: 0903140114"<<endl;
while(flag!=0) //while循环 ,flag=0时终止循环
{
pt1=&grad1;
pt2=&grad2;
cout<<"Please choose the type of the numbers:"<<endl;
cout<<"'0'---------------------------------exit system"<<endl;
cout<<"'1'---------------------------------real number"<<endl;
cout<<"'2'-------------------------------plural number"<<endl;
cin>>flag;
if(flag==1) //条件语句:如果flag=1,则进行实数运算
{
cout<<"please choose the operation of the numbers:"<<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'--------------------------------'root of two'"<<endl;
cout<<"'12'--------------------------------'square'"<<endl;
cin>>w;
if(w==3)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pt1->operator+(grad2);
}
if(w==4)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pt1->operator-(grad2);
}
if(w==5)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pt1->operator*(grad2);
}
if(w==6)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pt1->operator/(grad2);
}
if(w==7)
{
cout<<"please enter the number: ";
pt1->enter();
pt1->operator++();
}
if(w==8)
{
cout<<"please enter the number: ";
pt1->enter();
pt1->operator--();
}
if(w==9)
{
cout<<"please enter the number: ";
pt1->enter();
grad1++;
}
if(w==10)
{
cout<<"please enter the number: ";
pt1->enter();
grad1--;
}
if(w==11)
{
cout<<"please enter the number: ";
pt1->enter();
pt1->RootOfTwo();
}
if(w==12)
{
cout<<"please enter the number: ";
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<<"please choose correct option!"<<endl;
} //出错提示
}
pt1=&revd1;
pt2=&revd2;
pm1=&revd1;
pm2=&revd2;
if(flag==2) //条件语句:如果flag=1,则进行实数运算
{
cout<<"please choose the operation of the numbers:"<<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<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pm1->operator+(revd2);
}
if(v==4)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pm1->operator-(revd2);
}
if(v==5)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pm1->operator*(revd2);
}
if(v==6)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
pt2->enter();
cout<<endl;
pm1->operator/(revd2);
}
if(v==7)
{
cout<<"please enter the number: ";
pt1->enter();
cout<<endl;
pm1->Conjugate();
}
if(v==8)
{
cout<<"please enter the number: ";
pt1->enter();
cout<<endl;
pm1->Square1();
}
if(v==9)
{
cout<<"please enter the number: ";
pt1->enter();
cout<<endl;
pm1->Conjugate();
}
if(v==10)
{
cout<<"please enter the number: ";
pt1->enter();
cout<<endl;
pm1->Angle1();
}
if(v==11)
{
cout<<"please enter first number: ";
pt1->enter();
cout<<endl;
cout<<"please enter second number: ";
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<<"please choose correct option!"<<endl;
} //出错提示
}
if(flag!=1&&flag!=2&&flag!=0)
{
cout<<"please choose correct option!"<<endl;
} //出错提示
if(flag!=0)
{
cout<<"\n";
cout<<"Please carry out the next operation:"<<endl;//继续运算提示
cout<<"\n";
}
}
cout<<"Welcome next time!"<<endl;
return 0;
}
3.4函数调用关系图
3.4.1函数调用关系图
如图3.4—1所示
flag!=0
0 其他
输入w ,w=? exit error 输入v,v=?
3 4 5 6 7 8 9 10 11 12
输出
“please
choose
correct
option!”
“
输入flag
float Length()
Complex1 Conjugate()
float Angle1()
Complex operator++()
Complex1 operator+(Complex1 &c1)
Complex1 operator-(Complex1 &c1)
Complex1 operator*(Complex1 &c1)
Complex1 operator/(Complex1 &c1)
f
展开阅读全文