社区
C++ 语言
帖子详情
100分求一小程序
peter9606
2004-05-11 08:23:13
构造一个复数的类
要求可以进行加减乘除运算,但是必须是运算符重载。要求可以运行的。。谢谢啦
...全文
109
15
打赏
收藏
100分求一小程序
构造一个复数的类 要求可以进行加减乘除运算,但是必须是运算符重载。要求可以运行的。。谢谢啦
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
15 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
vcchunhong
2004-05-12
打赏
举报
回复
如果要想分数那样使用运算符重载的话
复数好象很难办到
不
应该说办不到的
要用库#include <complex>
一路奔跑
2004-05-12
打赏
举报
回复
不能这样的~
自己的作业自己写~
如果是你想自学,这种东西完全得靠自己想出来~
freefalcon
2004-05-12
打赏
举报
回复
有没有人帮忙呀?难道若大的论坛就没有人可以写出来么?
——楼主什么口气,为什么要写呢?别人又不是帮你写程序的工具,别人也有自己的事情
帮你修改倒是可以的
languagec
2004-05-12
打赏
举报
回复
楼主用的什么教材?
goodname
2004-05-12
打赏
举报
回复
写是可以写出来的,但是什么健壮性,可扩展性,运行效率什么的,绝对恐怕
不如标准库写的那么好。
做作业,做这些东西,否则;工业产品的话,放着stl现成的好的不用,为啥要自己写。
楼主贴一个自己的实现,大家或许能够帮你一起找错误,或者分析效率,改进方法等
但是,只是给出一个题目去做,恐怕,也太没意思了吧。
--
得罪莫怪
languagec
2004-05-12
打赏
举报
回复
#include <iostream.h>
class complex
{
public:
complex() {real=imag=0;}
complex(double r,double i)
{
real=r;
imag=i;
}
friend complex operator + (const complex &c1,const complex &c2);
friend complex operator - (const complex &c1,const complex &c2);
friend complex operator * (const complex &c1,const complex &c2);
friend complex operator / (const complex &c1,const complex &c2);
friend void print(const complex &c);
private:
double real,imag;
};
complex operator +(const complex &c1,const complex &c2)
{
return complex(c1.real+c2.real,c1.imag+c2.imag);
}
complex operator -(const complex &c1,const complex &c2)
{
return complex(c1.real-c2.real,c1.imag-c2.imag);
}
complex operator *(const complex &c1,const complex &c2)
{
return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
complex operator /(const complex &c1,const complex &c2)
{
return complex((c1.real*c2.real+c1.imag+c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),
(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
}
void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}
void main()
{
complex c1(2.0,3.0),c2(4.0,-2.0),c3;
c3=c1+c2;
cout<<"\nc1+c2=";
print(c3);
c3=c1-c2;
cout<<"\nc1-c2=";
print(c3);
c3=c1*c2;
cout<<"\nc1*c2=";
print(c3);
c3=c1/c2;
cout<<"\nc1/c2=";
print(c3);
c3=(c1+c2)*(c1-c2)*c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}
你要的是这个吗?
peter9606
2004-05-12
打赏
举报
回复
有没有人帮忙呀?难道若大的论坛就没有人可以写出来么?
peter9606
2004-05-12
打赏
举报
回复
To : languagec 谢谢啦 已经结贴啦
surstar
2004-05-12
打赏
举报
回复
GZ
jmlt1983
2004-05-11
打赏
举报
回复
两种方法
//111111111用库
#include<complex>
#include<iostream>
using namespace std;
void main()
{
complex<int> a(1,2);
complex<int> b(2,1);
complex<int> c;
c=a+b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
c=a*b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
c=a-b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
}
//22222222222用数据结构
#include <stdio.h>
struct data
{
float re;
float im;
};
struct data x,y,z;
int main()
{
float mod;
x.re = 1;
x.im = 2;
y.re = 3;
y.im = 4;
mod = (float)(y.re * y.re + y.im * y.im);
z.re = (float)(x.re * y.re + x.im * y.im) / mod;
z.im = (float)(x.im * y.re - x.re * y.im) / mod;
printf(" x/y = [%f] + [%f]i ", z.re ,z.im);
return 0;
}
peter9606
2004-05-11
打赏
举报
回复
实现呢? 拜托是自己写的好不好? 拜托了
海阔天空的创业与创作
2004-05-11
打赏
举报
回复
complex类接口:
complex
template<class T>
class complex {
public:
typedef T value_type;
T real() const;
T imag() const;
complex(const T& re = 0, const T& im = 0);
complex(const complex& x);
complex& operator=(const complex& rhs);
complex& operator+=(const complex& rhs);
complex& operator-=(const complex& rhs);
complex& operator*=(const complex& rhs);
complex& operator/=(const complex& rhs);
complex& operator=(const T& rhs);
complex& operator=(const T& rhs);
complex& operator+=(const T& rhs);
complex& operator-=(const T& rhs);
complex& operator*=(const T& rhs);
complex& operator/=(const T& rhs);
friend complex<T>
operator+(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator+(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator-(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator-(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator*(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator*(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator/(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator/(const T& lhs, const complex<T>& rhs);
friend bool operator==(const complex<T>& lhs, const T& rhs);
friend bool operator==(const T& lhs, const complex<T>& rhs);
friend bool operator!=(const complex<T>& lhs, const T& rhs);
friend bool operator!=(const T& lhs, const complex<T>& rhs);
};
peter9606
2004-05-11
打赏
举报
回复
拜托 谢谢啦 还是给出源程序吧
sharkhuang
2004-05-11
打赏
举报
回复
不用自己写了!昨天也看到这样的帖子!:)
freefalcon
2004-05-11
打赏
举报
回复
标准库中有
#include <complex>
自己写的话,看看头文件就知道了
200个经典C程序【源码】
001 第一个C程序 002 运行多个源文件 003 求整数之积 004 比较实数大小 005 字符的输出 006 显示变量所占字节数 007 自增/自减运算 008 数列求和 009 乘法口诀表 010 猜数字游戏 011 模拟ATM(自动柜员机...
50道JAVA基础编程练习题
【程序 13】 题目:一个整数,它加上
100
后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? 程序分析:在 10 万以内判断,先将该数加上
100
后再开方,再将该数加上 268 后再开方,如果开方后...
一个微信
小程序
可以跳转多少个其他微信
小程序
?
首先不同的微信
小程序
之间要跳转必须同一个公众号关联。 而一个公众号可关联同一主体的10个
小程序
,不同主体的3个
小程序
。 一个
小程序
可关联最多500个公众号。 结果出来了:(10+3-1)*500 = 6000 个。 是不是很...
微信
小程序
|基于
小程序
+云开发制作一个菜谱
小程序
今天吃什么?这是一个让强迫症左右为难的问题,跟随此文基于
小程序
+云开发制作一个菜谱
小程序
,根据现有食材一键生成菜谱,省心又省力。
Github上源码之微信
小程序
18:使用微信
小程序
实现分答这款APP的基础功能。24: 基于Zhihu Live数据的微信
小程序
。22:仿 「ONE · 一个」 的微信
小程序
。46:医药网原生APP的微信
小程序
DEMO。65:微信
小程序
版的CNodeJs中文社区。35:一个...
C++ 语言
65,208
社区成员
250,517
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章