Using structures, write the necessary declarations and functions for implementing addition, subtraction, multiplication and division of complex numbers.

Source Code

Brief explanation is provided after the source code.

/*
    First Class ADT interface for complex numbers (complex2.h)
*/
typedef struct complex * Complex;
Complex ComplexInit(float, float);
float Re(Complex);
float Im(Complex);
Complex ComplexAdd(Complex, Complex);
Complex ComplexSubt(Complex, Complex);
Complex ComplexMult(Complex, Complex);
Complex ComplexDiv(Complex, Complex);

 

/*
    First Class ADT Implementation for complex numbers (complex2.c)
*/
#include "complex2.h"
#include <stdio.h>

struct complex {
    float Re;
    float Im;
};

Complex ComplexInit(float Re, float Im){
    Complex t = malloc(sizeof *t);
    t->Re = Re;
    t->Im = Im;
    return t;
}

float Re(Complex z) {
    return z->Re;
}

float Im(Complex z) {
    return z->Im;
}

Complex ComplexAdd(Complex a, Complex b) {
    return ComplexInit(Re(a) + Re(b), Im(a) + Im(b));
}

Complex ComplexSubt(Complex a, Complex b) {
    return ComplexInit(Re(a) - Re(b), Im(a) - Im(b));
}

Complex ComplexMult(Complex a, Complex b) {
    return ComplexInit(Re(a) * Re(b) - Im(a) * Im(b), Re(a) * Im(b) + Im(a) * Re(b));

}

Complex ComplexDiv(Complex a, Complex b) {
    Complex conjugate = ComplexInit(Re(b), -Im(b));
    Complex t = ComplexMult(a, conjugate);
    Complex x = ComplexMult(b, conjugate);
    return ComplexInit(Re(t)/Re(x), Im(t)/Re(x));
}

 

/*
    Client Program (exercise4.2.c)
*/
#include <stdio.h>
#include "complex2.h"

int main(int argc, char **argv) {
    int choice;
    float Re1, Re2, Im1, Im2;
    Complex t1, t2, x;
    printf("Select an operation to perform\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");

    scanf("%d", &choice);
    while(choice != 1 && choice != 2 && choice != 3 && choice != 4) {
        printf("Invalid choice. Please select a valid choice.\n");
        scanf("%d", &choice);
    }

    printf("Real Part (1st complex number): ");
    scanf("%f", &Re1);
    printf("Imaginary Part (1st complex number): ");
    scanf("%f", &Im1);
    printf("Real Part (2nd complex number): ");
    scanf("%f", &Re2);
    printf("Imaginary Part (2nd complex number): ");
    scanf("%f", &Im2);
    t1 = ComplexInit(Re1, Im1);
    t2 = ComplexInit(Re2, Im2);

    if (choice == 1) {   // Addition
        x = ComplexAdd(t1, t2);
        printf("Result: %6.3f %6.3f\n", Re(x), Im(x));
    } else if (choice == 2) { // Subtraction
        x = ComplexSubt(t1, t2);
        printf("Result: %6.3f %6.3f\n", Re(x), Im(x));
    } else if (choice == 3) { // Multiplication
        x = ComplexMult(t1, t2);
        printf("Result: %6.3f %6.3f\n", Re(x), Im(x));
    } else { // Division
        x = ComplexDiv(t1, t2);
        printf("Result: %6.3f %6.3f\n", Re(x), Im(x));
    }

    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Select an operation to perform
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Real Part (1st complex number): 2
Imaginary Part (1st complex number): 3
Real Part (2nd complex number): 2
Imaginary Part (2nd complex number): 4
Result:  4.000  7.000

Brief Explanation

  • In the interface complex2.h, various function prototypes needed for this program are specified.
  • File complex2.c, provides the definition of the function prototypes in the interface.
  • File exercise4.2.c, is a client program which make use of the functions defined to compute the addition, subtraction, multiplication and division of complex numbers.
  • This program can be compile and executed via the console on linux as follows:

$ cc -o exercise4.2 exercise4.2.c complex2.c
$ ./exercise4.2

Add comment


Security code
Refresh