c语言 单词变复数_C语言关于复数

本文介绍了如何使用C语言定义并操作复数。通过结构体表示复数,实现了读取复数、加减乘除复数及打印复数的功能。在主函数中,用户可以输入两个复数,程序将展示它们的和、差、积和商。
摘要由CSDN通过智能技术生成

展开全部

#include 

typedef struct {

float r;

float i;

}Complex;

Complex readComlexNumber() {

Complex n;

printf("Input real part:");

scanf("%f", &n.r);

printf("Input imaginary part:");

scanf("%f", &n.i);

return n;

}

Complex sumComplex(Complex a, Complex b) {

Complex c;

c.r = a.r + b.r;

c.i = a.i + b.i;

return c;

}

Complex differeneComplex(Complex a, Complex b) {

Complex c;

c.r = a.r-b.r;

c.i = a.i-b.i;

return c;

}

Complex multiplyComplex(Complex a, Complex b) {

Complex c;

c.i = a.r * b.i + a.i*b.r;

c.r = a.r * b.r - a.i*b.i;

return c;

}

Complex divideComplex(Complex a, Complex b) {

Complex c;

c.r = (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i);

c.i = (a.i*b.r-a.r*b.i)/(b.r*b.r+b.i*b.i);

return c;

}

void printComplex(Complex n) {

printf("%.2f+%.2fi", n.r, n.i);

}

int main(){

Complex a, b, c;

printf("Input Complex number a:\n");

a = readComlexNumber();

printf("Input Complex number b:\n");

b = readComlexNumber();

printf("The 2 Complex a & b is :\n");

printComplex(a); printf("   and   "); printComplex(b);

//sum

c = sumComplex(a, b);

printf("\n (a+b)="); printComplex(c);

//diff

c = differeneComplex(a, b);

printf("\n (a-b)="); printComplex(c);

//multiply

c = multiplyComplex(a, b);

printf("\n (a*b)="); printComplex(c);

//divide

c = divideComplex(a, b);

printf("\n (a/b)="); printComplex(c);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值