复数四则运算 编程

复数四则运算 编程,第1张

#include <iostream.h>

class Complex{

private:

float _real

float _image

public:

Complex(float real=0,float image=0)

Complex operator +(const Complex &rhs)

Complex operator -(const Complex &rhs)

Complex operator *(const Complex &rhs)

float GetReal()const

float GetImage()const

}

Complex::Complex(float real,float image)

{

_real=real

_image=image

}

Complex Complex::operator +(const Complex &rhs)

{

_real+=rhs.GetReal()

_image+=rhs.GetImage()

return *this

}

Complex Complex::operator -(const Complex &rhs)

{

_real-=rhs.GetReal()

_image-=rhs.GetImage()

return *this

}

Complex Complex::operator *(const Complex &rhs)

{

_real=_real*rhs.GetReal()-_image*rhs.GetImage()

_image=_real*rhs.GetImage()+_image*rhs.GetReal()

return *this

}

float Complex::GetReal()const

{

return _real

}

float Complex::GetImage()const

{

return _image

}

void main()

{

cout<<"依次输入两个复数的实部和虚部"<<endl

float realA,imageA,realB,imageB

cin>>realA>>imageA>>realB>>imageB

Complex A(realA,imageA)

Complex B(realB,imageB)

Complex temp

//减法和乘法类似

temp=A+B

cout<<"两者之和为:"<<temp.GetReal()<<"+"<<temp.GetImage()<<"i"<<endl

cout<<"其实部为"<<temp.GetReal()<<"虚部为"<<temp.GetImage()<<endl

}

structcomplex{

floatrmz//实部

floatlmz//虚部

}

//产生一个复数.

complexgetacomplex(floata,floatb){

complexnode=newcomplex()

node.rmz=a

node.lmz=b

returnnode}

//两个复数求和

complexaddcomplex(complexcomplex1,complexcomplex2)

{

complexnode=newcomplex()

node.rmz=complex1.rmz+complex2.rmz

node.lmz=complex1.lmz+complex2.lmz

returnnode

}

//求两个复数的差

complexsubcomplex(complexcomplex1,complexcomplex2)

{

complexnode=newcomplex()

node.rmz=complex1.rmz-complex2.rmz

node.lmz=complex1.lmz-complex2.lmz

returnnode

}

//求两个复数的积

complexproductcomplex(complexcomplex1,complexcomplex2)

{

complexnode=newcomplex()

node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz

node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz

returnnode

}

//求实部

floatgetcomplexrmz(complexcomplex1)

{

returncomplex1.rmz

}

//求虚部

floatgetcomplexlmz(complexcomplex1)

{

returncomplex1.lmz

}

我们设计一个可进行复数运算的演示程序。要求实现下列六种基本运算

:1)由输入的实部和虚部生成一个复数

2)两个复数求和

3)两个复数求差

4)两个复数求积,

5)从已知复数中分离出实部

6)从已知复数中分离出虚部。

运算结果以相应的复数或实数的表示形式显示(最好用结构体的方法)

要是能用c++和stl,可以这样写#include <complex>#include <iostream>void main(){using namespace std complex<double>a(3, 2) complex<double>b(5, 6) complex<double>result(0,0) result = a*b/(a+b) cout <<result}

下面是具体的 *** 作:

stdio.h>

#include<conio.h>

#include<stdlib.h>

#define ERR -1

#define MAX 100 /*定义堆栈的大小*/

int stack[MAX]/*用一维数组定义堆栈*/

int top=0/*定义堆栈指示*/

int push(int i) /*存储运算数,入栈 *** 作*/

{

if(top<MAX)

{

stack[++top]=i/*堆栈仍有空间,栈顶指示上移一个位置*/

return 0

}

else

{

printf("The stack is full")

return ERR

}

}

int pop() /*取出运算数,出栈 *** 作*/

{

int var/*定义待返回的栈顶元素*/

if(top!=NULL) /*堆栈中仍有元素*/

{

var=stack[top--]/*堆栈指示下移一个位置*/

return var/*返回栈顶元素*/

}

else

printf("The stack is empty!\n")

return ERR

}

void main()

{

int m,n

char l

int a,b,c

int k

do{

printf("\tAriothmatic Operate simulator\n")/*给出提示信息*/

printf("\n\tPlease input first number:")/*输入第一个运算数*/

scanf("%d",&m)

push(m)/*第一个运算数入栈*/

printf("\n\tPlease input second number:")/*输入第二个运算数*/

scanf("%d",&n)

push(n)/*第二个运算数入栈*/

printf("\n\tChoose operator(+/-/*//):")

l=getche()/*输入运算符*/

switch(l) /*判断运算符,转而执行相应代码*/

{

case '+':

b=pop()

a=pop()

c=a+b

printf("\n\n\tThe result is %d\n",c)

printf("\n")

break

case '-':

b=pop()

a=pop()

c=a-b

printf("\n\n\tThe result is %d\n",c)

printf("\n")

break

case '*':

b=pop()

a=pop()

c=a*b

printf("\n\n\tThe result is %d\n",c)

printf("\n")

break

case '/':

b=pop()

a=pop()

c=a/b

printf("\n\n\tThe result is %d\n",c)

printf("\n")

break

}

printf("\tContinue?(y/n):")/*提示用户是否结束程序*/

l=getche()

if(l=='n')

exit(0)

}while(1)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11135542.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-13
下一篇2023-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存