YTU 2440: C++习题 复数类--重载运算符+,-,*,/
2440: C++习题 复数类--重载运算符+,-,*,/
时间限制: 1 Sec 内存限制: 128 MB
提交: 1189 解决: 774
题目描述
定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积和商。
输入
两个复数
输出
两个复数之和、差、积和商
样例输入
3 4
5 -10
样例输出
c1+c2=(8.00,-6.00i)
c1-c2=(-2.00,14.00i)
c1*c2=(55.00,-10.00i)
c1/c2=(-0.20,0.40i)
提示
前置代码及类型定义已给定如下,提交时不需要包含,会自动添加到程序前部
/* C++代码 */
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double i);
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/* C++代码 */
int main()
{
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Complex c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double i);
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex::Complex()
{
real=0;
imag=0;
}
Complex::Complex(double r,double i)
{
real=r;
imag=i;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator +(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator /(Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
Complex Complex::operator *(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Complex c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double i);
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex::Complex()
{
real=0;
imag=0;
}
Complex::Complex(double r,double i)
{
real=r;
imag=i;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator +(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator /(Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
Complex Complex::operator *(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Complex c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
YTU 2440: C++习题 复数类--重载运算符+,-,*,/的更多相关文章
- YTU 2443: C++习题 复数类--重载运算符3+
2443: C++习题 复数类--重载运算符3+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1368 解决: 733 题目描述 请编写程序,处理一个复数与一个double数相加的运 ...
- YTU 2441: C++习题 复数类--重载运算符2+
2441: C++习题 复数类--重载运算符2+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 847 解决: 618 题目描述 定义一个复数类Complex,重载运算符"+ ...
- YTU 2439: C++习题 复数类--重载运算符+
2439: C++习题 复数类--重载运算符+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1022 解决: 669 题目描述 定义一个复数类Complex,重载运算符"+ ...
- C++习题 复数类--重载运算符2+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...
- C++习题 复数类--重载运算符+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...
- YTU 2442: C++习题 矩阵求和--重载运算符
2442: C++习题 矩阵求和--重载运算符 时间限制: 1 Sec 内存限制: 128 MB 提交: 1457 解决: 565 题目描述 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运 ...
- sdut 4-1 复数类的运算符重载
4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...
- C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等:“=”运算符实现people类对象的赋值操作. 代码如下 #include&l ...
- YTU 2617: B C++时间类的运算符重载
2617: B C++时间类的运算符重载 时间限制: 1 Sec 内存限制: 128 MB 提交: 284 解决: 108 题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为 ...
随机推荐
- Device eth0 does not seem to be present,delaying initialization问题
1.打开/etc/udev/rules.d/70-persistent-net.rules: cat /etc/udev/rules.d/70-persistent-net.rules 文件内容如图: ...
- 【】关闭QQ右下角各种弹框
[]关闭QQ右下角弹框 一: 二: 超级会员设置过滤(屏蔽)广告后可以过滤哪些广告? 1.可以过滤QQ客户端好友聊天对话框右侧出现的Flash广告.左下角的文案广告: 如图: 2.可以 ...
- python接口自动化-multipart/form-data上传图片
前言 在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片 上传接口 1.以禅道上提交bug为例,在选择图片时,点确定按钮,就是上传图片了 2.用 ...
- SPOJ LCS 后缀自动机找最大公共子串
这里用第一个字符串构建完成后缀自动机以后 不断用第二个字符串从左往右沿着后缀自动机往前走,如能找到,那么当前匹配配数加1 如果找不到,那么就不断沿着后缀树不断往前找到所能匹配到当前字符的最大长度,然后 ...
- HDU4135容斥原理
#include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...
- [luoguP1169] [ZJOI2007]棋盘制作(单调栈)
传送门 和玉蟾宫差不多 ——代码 #include <cstdio> #include <iostream> using namespace std; ; int n, m, ...
- vscode 打开新文件覆盖窗口,始终显示一个窗口
一直在使用vscode 编辑器,里面的扩展用的比较舒服,但是最近遇到一个小问题,一直也没有找好的解决办法,今天无意中把问题给解决了.具体如下 之前使用编辑器,可以同时打开多个文件,而且是多窗口展示的, ...
- 【收藏】实战Nginx与PHP(FastCGI)的安装、配置与优化
拜读南非蚂蚁大牛的文章真是有所收获 http://ixdba.blog.51cto.com/2895551/806622 一.什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP s ...
- Java设计模式之(工厂模式)
工厂模式: 工厂模式可以分为三类: 1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory) 简单工厂模 ...
- Windows Server 2003的一些优化设置 (转至网络)
2003序列号:JCHKR-888KX-27HVK-DT88X-T767M1.禁用配置服务器向导: 禁止“配置你的服务器”(Manage Your Server)向导的出现:在控制面板(Control ...