C++ 实验 使用重载运算符实现一个复数类
实验目的:
1.掌握用成员函数重载运算符的方法
2.掌握用友元函数重载运算符的方法
实验要求:
1.定义一个复数类,描述一些必须的成员函数,如:构造函数,析构函数,赋值函数,返回数据成员值的函数等。
2.定义运算符重载函数,通过重载运算符:+,-,*,/,直接实现二个复数之间的加减乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求乘法“+”,“*”用友元函数实现重载,除法“-”,“/”用成员函数实现重载,参数是复数或实数。
3.通过重载运算符:>>,<<,=,直接实现复数的输入、输出及赋值运算,通过重载运算符:= =,!=直接实现复数的比较运算,编写一个完整的程序,测试重载运算符的正确性。
操作菜单可参考如下格式:
1输入复数
2查看输入的复数
3复数相加
4复数相减
5复数相乘
6复数相除
7输出结果
0退出
#include <iostream>
#include<cstdio>
using namespace std;
class Complex
{
public:
Complex(double r = ,double i = )//构造函数
{
real=r;
imag=i;
}
~Complex()
{ }
friend Complex operator+(Complex &c1,Complex &c2); //重载为友员函数
friend Complex operator*(Complex &c1,Complex &c2);
Complex operator -(Complex&);//重载为成员函数
Complex operator /(Complex&);
friend istream& operator>>(istream&, Complex&);
friend ostream& operator<<(ostream&, Complex&);
friend bool operator==(Complex &c1,Complex &c2);
friend bool operator!=(Complex &c1,Complex &c2);
void display( );
private:
double real;
double imag;
};
Complex operator + (Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
Complex operator * (Complex &c1,Complex &c2)
{
return Complex(c1.real*c2.real, c1.imag*c2.imag);
}
Complex Complex::operator-(Complex &c)
{
return Complex(real-c.real,imag-c.imag);
}
Complex Complex::operator/(Complex &c)
{
return Complex(real/c.real,imag/c.imag);
} istream& operator>>( istream& in, Complex &c )
{
in >> c.real >> c.imag;
return in;
}
ostream& operator<<( ostream& out, Complex &c )
{
out << c.real << "+" << c.imag << "i\n";
return out;
} bool operator == (Complex &c1,Complex &c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
{
return true;
}
else
{
return false;
}
}
bool operator != (Complex &c1,Complex &c2)
{
if(c1.real!=c2.real||c1.imag!=c2.imag)
{
return true;
}
else
{
return false;
}
}
void Complex::display( )
{
cout<<real<< "+" <<imag<<"i\n"<<endl;
}
int Menu()
{
int t;
cout << endl;
cout<<"=================="<<endl;
cout<<"1.输入复数"<<endl;
cout<<"2.查看输入的复数"<<endl;
cout<<"3.复数相加"<<endl;
cout<<"4.复数相减"<<endl;
cout<<"5.复数相乘"<<endl;
cout<<"6.复数相除"<<endl;
cout<<"7.输出结果"<<endl;
cout<<"0.退出"<<endl;
cout<<"=================="<<endl;
cout<<"请选择(0-7):";
cin>>t;
return t;
} int main()
{
int iChoice =;
Complex c1,c2,c3,c4;
while (iChoice!=)
{
iChoice = Menu();
switch (iChoice)
{
case :
{
cout<<"请输入一个复数:"<<endl;
cin>>c1;
getchar();
break;
}
case :
{
//c1.display();
cout<<c1;
break;
}
case :
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相加:"<<endl;
cin>>c2;
getchar();
c3=c1+c2;
break;
}
case :
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相减:"<<endl;
cin>>c2;
getchar();
c3=c1-c2;
break;
}
case :
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相乘:"<<endl;
cin>>c2;
getchar();
c3=c1*c2;
break; }
case :
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相除:"<<endl;
cin>>c2;
getchar();
c3=c1/c2;
break;
}
case :
{
cout<<"运算的结果:"<<endl;
cout<<c3;
break;
}
case :
{
break; }
}
} return ;
}
C++ 实验 使用重载运算符实现一个复数类的更多相关文章
- c++primer,自定义一个复数类
#include<iostream> #include<string> #include<vector> #include<algorithm> #in ...
- JavaScript实现一个复数类
<script type="text/javascript"> /** * 这里定义Complex类,用来描述复数 */ /** * 这个构造函数为它所创建的每个实例定 ...
- 15.C++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
- C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等:“=”运算符实现people类对象的赋值操作. 代码如下 #include&l ...
- 定义一个复数类Complex
#include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex( ...
- sdut 4-1 复数类的运算符重载
4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...
- 【c++习题】【17/5/8】重载运算符
1.设计一个Complex(复数)类,完成如下要求: 该类具有实部(Real_Part)和虚部(Image_Part)通过重载运算符“+”实现两个复数的相加通过重载运算符“+”实现一个复数与一个数值的 ...
- C++习题 复数类--重载运算符2+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...
- C++习题 复数类--重载运算符+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...
随机推荐
- 更改win系统的鼠标样式
一.找一个你心仪的鼠标样式(.cur文件),并放到 C:\Windows\Cursors 目录下 二.打开,控制面板 -> 硬件和声音 -> 鼠标 ,如下图: 三.浏览鼠标目录,找到你存放 ...
- 第二次SDN上机作业
SDN第二次作业 1.安装floodlight fatter树在floodlight上的连接显示 2.生成拓扑并连接控制器floodlight,利用控制器floodlight查看图形拓扑 floodl ...
- 团队作业7——第二次项目冲刺(Beta版本)day3
项目成员: 曾海明(组长):201421122036 于波(组员):201421122058 蓝朝浩(组员):201421122048 王珏 (组员):201421122057 叶赐红(组员):20 ...
- java疯狂讲义--摘要
1.一个java文件中可以有多个类,但是只能有一个public类,并且该类需要与文件同名 第6章 对象的软,弱和虚引用 1.强引用---创建一个对象,并把这个对象赋给一个引用变量.一个对象被一个以上的 ...
- lua-resty-gearman模块
粘贴一段百度对gearman的解释: Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统. lu ...
- HDU 2159 FATE(有选择物品总个数限制的完全背包,经典!!)
FATE Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 异常处理简单例子--python
捕获所有异常 #!/usr/bin/pythona = 10b = 0try: c = a/b print c print 'nothing happen...'#todo: catch all ex ...
- day69
昨日回顾: 1 路由层: 1简单配置 2无名分组 3有名分组 4反向解析--模板层,视图层 5路由分发 include 6名称空间 7伪静态 2 作业: urlpatterns = ...
- UDP穿越NAT原理(p2p)
转载自:http://blog.csdn.net/ldd909/article/details/5979967 论坛上经常有对P2P原理的讨论,但是讨论归讨论,很少有实质的东西产生(源代码).在这里我 ...
- c# C#获取屏幕鼠标坐标点颜色
[DllImport("user32.dll")] private static extern IntPtr GetDC(IntPtr hwnd); [DllImport(&quo ...