了解全局变量与局部变量---复数—c语言练习(20)

/分别输入两个复数的实部与虚部,用函数实现计算两个复数之和与之积/

全局变量:为了解决多个函数间的变量共用


当局部变量与全局变量同名时,该函数中全局变量不起作用

#include<stdio.h>
float result_real,result_imag;//运算结果有两个数值,无法通过return返回,所以使用全局变量使其成为主函数与自定义函数均能使用的变量
int main(void)
{
    float imag1,imag2,real1,real2;
    void product(float real1,float real2,float imag1,float imag2);
    void sum(float real1,float real2,float imag1,float iamg2);
    printf("Enter 1st complex number:");
    scanf("%f%f",&real1,&imag1);
    printf("Enter 2st complex number:");
    scanf("%f%f",&real2,&imag2);

    product(real1,real2,imag1,imag2);
    printf("product of complex if %f+%fi\n",result_real,result_imag);
    sum(real1,real2,imag1,imag2);
    printf("addition of complex is %f+%fi\n",result_real,result_imag);
    return 0;
}
//复数之和
void sum(float real1,float real2,float imag1,float imag2)
{
    result_real=real1+real2;
    result_imag=imag1+imag2;
}
//复数之积
void product(float real1,float real2,float imag1,float imag2)
{
    result_real=real1*real2-imag1*imag2;
    result_imag=real1*imag2+real2*imag1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值