实验目的:

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++ 实验 使用重载运算符实现一个复数类的更多相关文章

  1. c++primer,自定义一个复数类

    #include<iostream> #include<string> #include<vector> #include<algorithm> #in ...

  2. JavaScript实现一个复数类

    <script type="text/javascript"> /** * 这里定义Complex类,用来描述复数 */ /** * 这个构造函数为它所创建的每个实例定 ...

  3. 15.C++-操作符重载、并实现复数类

    首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...

  4. C++重载运算符练习--对people类重载“= =”运算符和“=”运算符

    题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等:“=”运算符实现people类对象的赋值操作. 代码如下 #include&l ...

  5. 定义一个复数类Complex

    #include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex( ...

  6. sdut 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...

  7. 【c++习题】【17/5/8】重载运算符

    1.设计一个Complex(复数)类,完成如下要求: 该类具有实部(Real_Part)和虚部(Image_Part)通过重载运算符“+”实现两个复数的相加通过重载运算符“+”实现一个复数与一个数值的 ...

  8. C++习题 复数类--重载运算符2+

    Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...

  9. C++习题 复数类--重载运算符+

    Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...

随机推荐

  1. C# 利用VS自带的WSDL工具生成WebService服务类(转载)

    WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲解了. 那么,既然有直接引用的方式,为什么还要通过WSDL生成呢? ...

  2. PyQt5--ToolBar

    # -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  3. 原生js返回顶部(匀速、由快到慢)

    在项目中我们经常有需求要求页面滚动到一定位置时出现返回顶部按钮,点击即返回顶部. 方法一: 锚点,这是最简单的.(a标签的href属性等于一直要到达位置元素的id值) 方法二: js直接给页面根节点设 ...

  4. C语言:值传递,地址传递和引用传递(example:值交换)

    于C语言中值传递.地址传递和引用传递的我个人理解. 通过一个例子:swap(交换两个整型变量的值)来表现! #include <stdio.h> void swap1(int* a,int ...

  5. iframe-metamask

    iframe--require('iframe') higher level api for creating and removing iframes in browsers 用于创建或移除浏览器中 ...

  6. leetcode121—Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. PAT B1034 有理数四则运算 (20 分)

    本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前 ...

  8. 《MySQL:菜鸟入门系列》

    关于数据库相关知识,几乎是互联网从业者逃不开的一个必备技能,特别是对于DB.开发和测试童鞋来说,更显得重要... 关于MySQL,推荐如下几本书: 入门级:<MySQL必知必会> 进阶级: ...

  9. Android soundpool初探

    内容:本编播客主要讲解一下“即时音效”: 特点:快,短. 在播放这类时间短但是要求反应迅速的的音效,就不能够用不能够使用播放时间较长的音乐播放技术了,而应该采取soundpool技术来播放. soun ...

  10. 创建一个目录的软连接ln -s和打印当前目录pwd的一个知识点

    创建一个目录的软连接,比如我在家目录下创建一个/data/www/的软连接,如下 # cd ~ # ln -s /data/www hehe       #这里一定要注意顺序哈哈 然后当我进入hehe ...