-
2021-05-20 04:36:54
[求助]关于复数加减乘除,共轭的程序...急用
要求实现复数加减乘除,求复数的共轭复数,以及输入复数,提取其实部'虚部并输出,高手帮忙了```
搜索更多相关的解决方案:
共轭 复数 加减乘除
----------------解决方案--------------------------------------------------------
在输出的时候加i就可以了!
----------------解决方案--------------------------------------------------------
实现了加减乘除,输出,其余功能自己加
class Complex
{
private:
double real; //实部
double imag; //虚部
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator +(const Complex &c2 );
Complex operator -(const Complex &c2 );
Complex operator *(const Complex &c2 );
Complex operator /(const Complex &c2 );
void display();
};
Complex Complex::operator+(const Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator-(const Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(const Complex &c2)
{
Complex c;
c.real=real*c2.real;
c.imag=imag*c2.imag;
return c;
}
Complex Complex::operator/(const Complex &c2)
{
Complex c;
c.real=real/c2.real;
c.imag=imag/c2.imag;
return c;
}
void Complex::display()
{
cout<
}
[此贴子已经被作者于2006-11-9 18:27:39编辑过]
----------------解决方案--------------------------------------------------------
更多相关内容 -
JAVA编写的复数加减乘除运算
2011-12-10 15:15:33老师布置的作业,编写的复数+、—、*、/ 的运算 -
c++第二次实验 设计复数类实现复数的加减乘除运算_Visualc++原代码_
2021-10-04 03:51:06设计复数类实现复数的加减乘除的运算设计一个银行账户(Account)类,包含户名、帐号以及当前余额属性,可完成开户、存款、取款和查询余额等行为。银行账户类的定义如下:class Account{public: Account(char name[]... -
asp.net(c#)复数类(复数加减乘除四则运算)
2020-10-30 18:48:33asp.net(c#)复数类(复数加减乘除四则运算) -
复数加减乘除实现(C++)
2019-10-17 16:03:05源码 (因为是昨晚写的,分析过程忘了,大家就直接看源码好了,反正还蛮简单的hhh) #include <iostream> using namespace std; class complex { public: complex(int i = 0, int j = 0) :a(i), b(j) {} ...源码
(因为是昨晚写的,分析过程忘了,大家就直接看源码好了,反正还蛮简单的hhh)
#include <iostream> using namespace std; class complex { public: complex(int i = 0, int j = 0) :a(i), b(j) {} ~complex() {}; complex operator+(complex y) { complex temp; temp.a = a + y.a; temp.b = b + y.b; return temp; } complex operator-(complex y) { complex temp; temp.a = a - y.a; temp.b = b - y.b; return temp; } complex operator*(complex y) { complex temp; temp.a = a * y.a - b * y.b; temp.b = a * y.b + b * y.a; return temp; } complex operator/(complex y) { complex temp; temp.a = (a * y.a + b * y.b) / (y.a * y.a + y.b * y.b); temp.b = (b * y.a + a * y.b) / (y.a * y.a + y.b * y.b); return temp; } void getcomplex(float& aa, float& bb) { aa = a; bb = b; } private: float a, b; }; ostream& operator<<(ostream& os, complex& s) { float a, b; s.getcomplex(a, b); os << a << "+" << b << "i"; return os; } int main() { complex x(1,1),y(1,1),z; z = x + y; cout << z << endl; z = x - y; cout << z << endl; z = x * y; cout << z << endl; z = x / y; cout << z << endl; return 0; }
-
两个复数的加减乘除
2018-05-06 16:31:39本人亲自编码,其中内容是对初学者有较大帮助,可以提供c++中关于友元类的例子 -
C#实现复数类,包括加减乘除乘方开方N次方等操作
2018-04-19 11:15:47C#实现复数类,包括加减乘除乘方开方N次方等操作,封装在一个类里面,可以直接调用 -
复数加减乘除
2011-10-06 22:32:33有界面的java编写的复数加减乘除……包括复数类,界面类! -
复数加减乘除运算运算
2010-11-03 23:10:01c#加减乘除,调试通过的,整个项目都在了 -
C++ 操作符重载(2) 复数加减乘除
2018-06-27 22:44:35复数由实部和虚部组成 复数间的运算通过实部和虚部,我们构建一个复数类来模拟复数间的操作类定义如下://复数的运算class Complex {private: int real; //复数的实部 int image; //复数的虚部public: void myPrint()...复数由实部和虚部组成 复数间的运算通过实部和虚部,我们构建一个复数类来模拟复数间的操作
类定义如下:
-
//复数的运算
-
class Complex {
-
private:
-
int real; //复数的实部
-
int image; //复数的虚部
-
public:
-
-
void myPrint() {
-
cout << this->real << " " << this->image << "i" << endl; //显示复数
-
}
-
-
Complex( int real= 0, int image= 0) { //构造函数
-
this->real = real;
-
this->image = image;
-
}
现在我们通过操作符的重载来实现复数间的直接运算加号的重载:
-
//重载加号
-
Complex operator+( const Complex &obj) {
-
Complex res;
-
res.real = this->real + obj.real;
-
res.image = this->image + obj.image;
-
return res;
-
}
重载的重要信息:1)为了能实现操作符重载,至少得有一个对象是用户自定义的对象,如上述加号重载是基于Complex类对象
2)赋值操作符: 编译器会自动的给每个类创建一个默认的赋值操作符。它会将所有成员从右边赋给左边,大多数情况下可以正常工作(不正常的地方与拷贝构造一样)。
全局运算符重载函数:
运算符函数与普通函数一样。唯一的不同在于,运算符函数的名字通常由operator关键字加上运算操作符组成。
我们可以把操作符函数定义为全局的
-
Complex operator * ( const Complex &obj1, const Complex &obj2) {
-
Complex res;
-
res.real = obj1.real * obj2.real;
-
res.image = obj1.image * obj2.image;
-
return res;
-
}
friend Complex operator * (const Complex &obj1, const Complex &obj2);
这样不但实现了全局函数,还可以传入两个形参
类型转换的重载:我们可以自定义类型转换操作符,把一个类型转换成另一个类型
-
operator float() const {
-
return float( this->real) / float( this->image);
-
}
使用:-
float val = C;
-
-
cout << val << endl;
整个程序如下:
-
-
using namespace std;
-
-
-
//复数的运算
-
class Complex {
-
private:
-
int real; //复数的实部
-
int image; //复数的虚部
-
public:
-
-
void myPrint() {
-
cout << this->real << " " << this->image << "i" << endl; //显示复数
-
}
-
-
Complex( int real= 0, int image= 0) { //构造函数
-
this->real = real;
-
this->image = image;
-
}
-
-
//重载加号
-
Complex operator+( const Complex &obj) {
-
Complex res;
-
res.real = this->real + obj.real;
-
res.image = this->image + obj.image;
-
return res;
-
}
-
-
-
//重载减号
-
Complex operator-( const Complex &obj) {
-
Complex res;
-
res.real = this->real - obj.real > 0 ? this->real - obj.real : 0;
-
res.image = this->image - obj.image > 0 ? this->image - obj.image : 0;
-
return res;
-
}
-
-
//重载乘号
-
//使全局函数可以使用私有的类
-
friend Complex operator * ( const Complex &obj1, const Complex &obj2);
-
-
operator float() const {
-
return float( this->real) / float( this->image);
-
}
-
-
-
};
-
-
-
//全局的乘号重载
-
Complex operator * ( const Complex &obj1, const Complex &obj2) {
-
Complex res;
-
res.real = obj1.real * obj2.real;
-
res.image = obj1.image * obj2.image;
-
return res;
-
}
-
-
-
-
int main() {
-
-
Complex A(10, 20);
-
Complex B(20, 30);
-
-
A.myPrint();
-
cout << endl << endl;
-
B.myPrint();
-
cout << endl << endl;
-
-
Complex C = A + B; //重载的加号
-
C.myPrint();
-
-
C = B - A; //重载的减号
-
C.myPrint();
-
-
C = B * A; //重载的乘号
-
C.myPrint();
-
-
float val = C;
-
-
cout << val << endl;
-
-
system( "PAUSE");
-
return 0;
-
}
-
-
-
C++实现复数加减乘除实验报告.doc
2021-05-22 06:48:47实验 C++实现复数的加减乘除试用C语言的结构类型定义表示复数Complex的抽象数据类型。(1)在复数内部用浮点数定义其实部与虚部;(2)设计实现复数的+、-、、等运算的函数。基本操作函数接口:InitComplex( &Z, ...实验 C++实现复数的加减乘除
试用C语言的结构类型定义表示复数Complex的抽象数据类型。
(1)在复数内部用浮点数定义其实部与虚部;
(2)设计实现复数的+、-、、等运算的函数。
基本操作函数接口:
InitComplex( &Z, v1, v2 ):操作结果:构造复数Z,其实部和虚部分别被赋以参数v1和v2的值。
GetReal( Z, &realPart ):初始条件:复数已存在。
操作结果:用realPart返回复数Z的实部值。
GetImag( Z, &ImagPart ):初始条件:复数已存在。
操作结果:用ImagPart返回复数Z的虚部值。
Add( z1,z2, &sum ):初始条件:z1,z2是复数。
操作结果:用sum返回两个复数z1,z2的和值。
Sub( z1,z2, &residue):初始条件:z1,z2是复数。
操作结果:用res返回两个复数z1,z2的差值。
mul( z1,z2, &product ):初始条件:z1,z2是复数。
操作结果:用product返回两个复数z1,z2的积。
div( z1,z2, "ient,&residue):初始条件:z1,z2是复数。
操作结果:用quotient 返回两个复数z1除z2的商, 用residue返回它们的余数。
要求main函数中只能是基本的输入输出语句和函数调用语句,其运行界面如下:
please input first complex number : a + bi
the other one : c+ di
please choice operation: +, -, * or /:+
(a + bi)+ (c+ di)=……
其中a,b,c,d是用户从键盘上输入的实型值,分别代表两个复数的实部和虚部。
实验代码:
#include//定义一个复数类
typedef struct {
float realpart;
float imagpart;
}Complex;
Complex InitComplex(float v1,float v2) //初始化
{ Complex z;
z.realpart=v1;
z.imagpart=v2;
return z;
}
float Getreal(Complex z)
{return z.realpart;
}
float Getimagepart(Complex z)
{return z.imagpart;
}
Complex Add(Complex z1,Complex z2) //实现加法运算
{
z1.realpart=z1.realpart+z2.realpart;
z1.imagpart=z1.imagpart+z2.imagpart;
return z1;
}
Complex Sub(Complex z1,Complex z2) //实现减法运算
{z1.realpart=z1.realpart-z2.realpart;
z1.imagpart=z1.imagpart-z2.imagpart;
return z1;
}
Complex Mul(Complex z1,Complex z2) //实现乘法运算
{z1.realpart=z1.realpart*z2.realpart;
z1.imagpart=z1.imagpart*z2.imagpart;
return z1;
}
Complex Div(Complex z1,Complex z2) // 实现除法运算
{z1.realpart=z1.realpart/z2.realpart;
z1.imagpart=z1.imagpart/z2.imagpart;
return z1;
}
int main ()
{ float a,b,c,d;
char e;
Complex z1,z2,z3;
printf("please input first complex number a+bi:"); //只需输入实部a,虚部b
scanf("%f %f",&a,&b);
z1= InitComplex(a,b);
printf("please input the other :");
scanf("%f %f",&c,&d);
z2=InitComplex(c,d);
printf("please choice operation :+,-,*,/ :"); //选择运算符
getchar();
scanf("%c",&e);
if (e==+) //选择运算符
z3= Add( z1,z2);
if(e==-)
z3=Sub(z1,z2);
if(e==*)
z3=Mul(z1,z2);
if(e==/)
z3=Div(z1, z2);
printf("%f+%fi",z3.realpart,z3.imagpart);
return 0;
}
运行结果:
-
C语言之复数的加减乘除
2021-07-09 16:07:07C语言之复数的加减乘除1 源代码2 结果 1 源代码 #include<stdio.h> #include<stdlib.h> // 定义一个结构体 typedef struct//结构体 { double real; double img; }complex; complex a={1,2}; ... -
C++第四章运算符重载习题-2:成员函数复数加减乘除
2018-12-24 09:37:55#include<iostream> using namespace std; class Complex { public: Complex(){real=0,imag=0;} Complex(double r ,double i):real(r),imag(i){} Complex operator + (Complex &... -
用java实现复数的加减乘除运算
2021-03-06 13:46:19用java实现复数的加减乘除运算1. 背景老师在课上布置了几道java编程题,此为其中之一2. 题目内容设计一个类Complex,用于封装对复数的下列操作:(1)一个带参数的构造函数,用于初始化复数成员(2)一个不带参数的构造... -
复数的 加减乘除 运算。
2021-03-16 15:52:44用友员运算符重载函数 来实现复数的加减乘除运算。两个复数a+bi和c+di加减乘除的方法如下:加法:(a+bi)+(c+di)=(a+c)+(b+d)i减法:(a+bi)-(c+di)=(a-c)+(b-d)i乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i除法:(a+bi)/(c+... -
java-复数加减乘除
2019-09-12 09:18:56System.out.println("请输入第二个复数的实部和虚部:"); Complex d2=new Complex(); System.out.print("加法结果为:"); Complex result_add=d1.add(d2); result_add.print(); System.out.print("减法... -
Java作业 --- 复数的加减乘除运算
2022-05-03 16:16:12老师布置了一项作业,实现复数的加减乘除运算,本来想偷个懒,到C站搜个现成代码交上去,然后找了很多找不到心仪的,最后自己写了一个。。。。。。。。。。。。。。。。。。。。。。。。。内容仅供参考。 运行的话... -
C++8-1多态:复数加减乘除(运算符重载,静态多态性)
2022-04-17 18:42:18求两个复数的加减乘除。 要求使用c++ class编写程序。可以创建如下class #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; ... -
用java实现复数的加减乘除运算(改进第1次)
2021-03-16 15:53:10用java实现复数的加减乘除运算(改进第1次)1.背景前两天为了赶速度,自己很快实现了如题java老师布置的作业,并在csdn上写了博客,记录学习过程,原博客链接为:... -
C++ 定义复数的加减乘除基本运算
2020-11-27 18:22:21在C++中定义复数operations #include <iostream> using namespace std; class complex{ public: complex(){ real = 0.0; imag = 0.0; } complex(double r_, double i_){ real = r_; imag = i_; } ... -
C8-1 复数加减乘除
2017-01-26 22:46:19C8-1 复数加减乘除 (100/100 分数) 题目描述 求两个复数的加减乘除。 要求使用c++ class编写程序。可以创建如下class #include #include #include #include using namespace std; class ... -
和复数相关的运算加减乘除的重载
2012-03-13 17:08:00复数重载为友元和非友元 已经重载了流运算符"<<" -
C++利用重载函数实现复数的加减乘除运算
2021-12-13 15:17:11加法:(a+bi)+(c+di)=(a+c)+(b+d)i 减法:(a+bi)-(c+di)=(a-c)+(b-d)i 乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i 除法:(a+bi)/(c+di)=((a+bi)*(c-di))/(c^2+d^2) #include <iostream> using namespace std;... -
struts2实现复数加减乘除
2018-09-27 21:05:49请输入第一个复数" theme="simple"/> <s:textfield name="first1" theme="simple"/>+<s:textfield name="first2" theme="simple"/>i ,'-','*','/'}" name="check" label="运算符" theme="simple... -
C8-1 复数加减乘除 (100/100 分数)
2018-05-23 20:17:00求两个复数的加减乘除。 要求使用c++ class编写程序。可以创建如下class #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;class ... -
复数加减乘除运算(自己的小程序)
2010-12-05 19:40:14复数的加减乘除运算,自己写的java小程序 -
复数类实现加减乘除并处理异常
2012-05-05 10:22:07复数类实现加减乘除并处理异常,使用throw try catch和finally -
求复数加减乘除的C语言程序,编译没错误但得不到正确的结果,求高手帮忙。跪谢。。。
2021-05-22 06:48:16求复数加减乘除的C语言程序,编译没错误但得不到正确的结果,求高手帮忙。跪谢。。。0#include#include#includestruct com{double real;double im;};struct com jia(struct com z1,struct com z2){struct com x;x....