编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:
要求:(1)定义一个结构体类型来描述复数。
(2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
(3)必须使用结构体指针的方法把函数的计算结果返回。
说明:用户输入:运算符号(+,-,*,/) a b c d.
输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。
输入:
- 2.5 3.6 1.5 4.9
输出:
要求:(1)定义一个结构体类型来描述复数。
(2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
(3)必须使用结构体指针的方法把函数的计算结果返回。
说明:用户输入:运算符号(+,-,*,/) a b c d.
输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。
输入:
- 2.5 3.6 1.5 4.9
输出:
1.00+-1.30i
AC Code
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#define inf 0x3f3f3f3f
#define maxn 7003
#define MOD 50000
using namespace std;
const double pi=acos(-1.0),ee=2.7182818284590452354;
typedef struct node{
double x,y;
}*pp;
pp p=(pp)malloc(sizeof(node));
void plu(double a,double b,double c,double d)
{
p->x=a+c;
p->y=b+d;
}
void min(double a,double b,double c,double d)
{
p->x=a-c;
p->y=b-d;
}
void mul(double a,double b,double c,double d)
{
p->x=a*c-b*d;
p->y=a*d+b*c;
}
void div(double a,double b,double c,double d)
{
mul(a,b,c,-d);
p->x/=c*c+d*d;
p->y/=c*c+d*d;
}
int main(int argc, char** argv) {
double a,b,c,d;
char s;
while(cin>>s>>a>>b>>c>>d)
{
switch(s){
case '+': plu(a,b,c,d);break;
case '-': min(a,b,c,d);break;
case '*': mul(a,b,c,d);break;
case '/': div(a,b,c,d);break;
}
printf("%.2f+%.2fi\n",p->x,p->y);
}
return 0;
}