如何通过将结构传递给C语言中的函数来添加两个复数?
C语言中两个复数相加,用户必须将两个复数作为结构成员,通过创建自定义函数对这两个数进行加法运算。
算法
参考下面给出的两个复数相加的算法。
Step 1: Declare struct complex with data members. Step 2: Declare name for structure and variables. Step 3: Enter real and imaginary part for first complex number at run time. Step 4: Enter real and imaginary part for second complex number at runtime Step 5: Compute addition of number1 and number2 by calling function. Go to step 7. Step 6: Print the result. Step 7: Compute additionDeclare temp variabletemp.real = num1.real + num2.real;temp.imag = num1.imag + num2.imag;return (temp);
示例
以下是通过将结构传递给函数来添加两个复数的C程序-
#include输出结果typedef struct complex{ float real; float imag; } complex; complex addition(complex num1, complex num2); int main(){ complex num1, num2, value; printf("entering real and imag parts of first complex no:\n "); scanf("%f %f", &num1.real, &num1.imag); printf("entering real and imag parts of second complex no:\n "); scanf("%f %f", &num2.real, &num2.imag); value= addition(num1, num2); printf("result = %.1f + %.1fi", value.real, value.imag); return 0; } complex addition(complex num1, complex num2){ complex temp; temp.real = num1.real + num2.real; temp.imag = num1.imag + num2.imag; return (temp); }
执行上述程序时,会产生以下结果-
entering real and imag parts of first complex no: entering real and imag parts of second complex no: result = 0.0 + 0.0i
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短