设计一个可进行复数运算的演示程序,要求实现下列四种基本运算:
1)由输入的实部和虚部生成一个复数;
2)两个复数求和;
3)两个复数求差;
4)两个复数求积;
5)打印输出一个复数。
#include <stdio.h>
typedef struct {
float x;
float y;
} complex;
void creat(complex* c);
void outputc(complex a);
complex add(complex k, complex h);
complex sub(complex k, complex h);
complex chengji(complex k, complex h);
complex a, b, a1;
int cmd;
int main() {
creat(&a);
outputc(a);
creat(&b);
outputc(b);
printf("相加");
a1 = add(a, b);
outputc(a1);
printf("相减");
a1 = sub(a, b);
outputc(a1);
printf("相乘");
a1 = chengji(a, b);
outputc(a1);
return 0;
}
void creat(complex* c)
{
float x1, y1;
printf("\n输入实部 x = ");
scanf_s("%f", &x1);
printf("\n输入虚部 y = ");
scanf_s("%f", &y1);
c->x = x1;
c->y = y1;
}
void outputc(complex a)
{
printf("复数: %lf + %lfi\n", a.x, a.y);
}
complex add(complex k, complex h)
{
complex l;
l.x = k.x + h.x;
l.y = k.y + h.y;
return l;
}
complex sub(complex k, complex h)
{
complex l;
l.x = k.x - h.x;
l.y = k.y - h.y;
return l;
}
complex chengji(complex k, complex h)
{
complex l;
l.x = k.x * h.x - k.y * h.y;
l.y = k.x * h.y + k.y * h.x;
return l;
}