C程序 两个复数相加

C程序 两个复数相加

在这里,我们要用C语言程序对两个复数进行加法。

输入:

a = ( 2 + 3i )
b = ( 4 + 5i )

输出:

Sum = ( 6 + 8i )

解释一下。

( 2 + 3i ) + ( 4 + 5i )
= (2 + 4) + (3 + 5)i
= 6 + 8i

步骤:

这个程序的方法非常简单,按照以下步骤进行。首先,我们定义一个复数的结构。然后定义一个函数,用于两个复数的相加。这个函数接受两个复数作为参数,并返回一个复数。

示例:

// C program to demonstrate
// addition of complex numbers
#include <stdio.h>
 
// define a structure for complex number
typedef struct complexNumber {
    int real;
    int img;
} complex;
 
// complex add(complex x, complex y) function C Program to
// Add Two Complex numbers. This function accepts two
// complex type numbers as parameter as return addition of
// them.
complex add(complex x, complex y);
 
// driver code
int main()
{
 
    // define three complex type numbers
    complex a, b, sum;
 
    // first complex number
    a.real = 2;
    a.img = 3;
 
    // second complex number
    b.real = 4;
    b.img = 5;
 
    // print first complex number
    printf("\n a = %d + %di", a.real, a.img);
 
    // print second complex number
    printf("\n b = %d + %di", b.real, b.img);
 
    // call add(a,b) function and
    // pass complex numbers a & b
    // as an parameter.
    sum = add(a, b);
 
    // print result
    printf("\n sum = %d + %di", sum.real, sum.img);
 
    return 0;
}
 
// complex add(complex x, complex y)
// function definition
complex add(complex x, complex y)
{
 
    // define a new complex number.
    complex add;
 
    // add real part of a&b
    add.real = x.real + y.real;
 
    // add Imaginary part of a&b
    add.img = x.img + y.img;
 
    // return add
    return (add);
}

输出

 a = 2 + 3i
 b = 4 + 5i
 sum = 6 + 8i

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C语言 实例