Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complex Numbers: A high-school in Davis wants to have a program for their studen

ID: 3726566 • Letter: C

Question

Complex Numbers:

A high-school in Davis wants to have a program for their students that can do basic operations on complex numbers.

A complex number is expressed by the form , where  and  are real values and  is the imaginary number (). The common operations between complex numbers are:

Addition:

Subtraction:

Multiplication:

Division:

Write program complex.c that reads an operation between two complex numbers and print the resulting complex number, as illustrated in the following example:

Here are a list of requirements, assumptions and hints:

This program shall contain no global variables.

You should define a structure to represent complex numbers: struct complex_s.

You must implement the following functions:

int scan_complex_op(struct complex_s *c1, struct complex_s *c2, char *operator)

This function gets the operation from the user and fill out the output parameters accordingly. In case of an invalid input, it should display an error message and loop.

It should return 0 if the program has received EOF (i.e. no more operations to process), or 1 if an operation is being returned in the output parameters.

void print_complex(struct complex_s c)

Prints a complex number.

struct complex_s add_complex(struct complex_s c1, struct complex_s c2)

Returns a complex number resulting from adding c1 and c2.

struct complex_s sub_complex(struct complex_s c1, struct complex_s c2)

Returns a complex number resulting from subtracting c2 from c1.

struct complex_s mult_complex(struct complex_s c1, struct complex_s c2)

Returns a complex number resulting from multiplying c1 with c2.

struct complex_s div_complex(struct complex_s c1, struct complex_s c2)

Returns a complex number resulting from dividing c1 by c2.

The main function should be the brain and contain the processing loop: first getting the requested operation, then performing it by calling the appropriate function and printing the result.

_____________________________________________________________________________________________________________________

PLEASE USE THE FUNCTION NAMES AND VARIABLES THAT ARE LISTED. Please do NOT use other functions that are not named in the instructions.

Explanation / Answer

#include<stdio.h>

#include<io.h>

int scan_complex_op(struct complex_s *, struct complex_s *, char *);

void print_complex(struct complex_s );

struct complex_s add_complex(struct complex_s , struct complex_s);

struct complex_s sub_complex(struct complex_s , struct complex_s );

struct complex_s mult_complex(struct complex_s , struct complex_s);

struct complex_s div_complex(struct complex_s , struct complex_s);

struct complex_s{

float real,img;

};

int scan_complex_op(struct complex_s *c1, struct complex_s *c2, char *operator)

{

if((*operator)!=EOF)

{

while((*operator)!='+'||'-'||'*'||'/')

{

printf("operator '%c' invalid",(*operator));

printf(" Enter an valid operation:");

scanf("%c",&operator);

}

printf("(%f+%fi)%c(%f+%fi)",c1->real,c1->img,(*operator),c2->real,c2->img);

return 1;

}

else

return 0;

}

void print_complex(struct complex_s c)

{

printf("(=%f+%fi)",c.real,c.img);

}

struct complex_s add_complex(struct complex_s c1, struct complex_s c2)

{

struct complex_s sum;

sum.real = c1.real + c2.real;

sum.img = c1.img + c2.img;

return sum;

}

struct complex_s sub_complex(struct complex_s c1, struct complex_s c2)

{

struct complex_s diff;

diff.real = c1.real - c2.real;

diff.img = c1.img - c2.img;

return diff;

}

struct complex_s mult_complex(struct complex_s c1, struct complex_s c2)

{

struct complex_s product;

product.real = c1.real*c2.real - c1.img*c2.img;

product.img = c1.img*c2.real + c1.real*c2.img;

return product;

}

struct complex_s div_complex(struct complex_s c1, struct complex_s c2)

{

struct complex_s quo;

float a,b,c;

if ( c2.real == 0 && c2.img == 0 )

printf("Division by 0 + 0i is not allowed.");

else

{

a = c1.real*c2.real + c1.img*c2.img;

b = c1.img*c2.real - c1.real*c2.img;

c = c2.real*c2.real + c2.img*c2.img;

quo.real=(a/c);

quo.img=(b/c);

}

return quo;

}

void main() {

struct complex_s x={3.5,5.2};

struct complex_s y={2.5,1.2};

struct complex_s z;

int check;

char op;

/* //If you want to initialise complex numbers dynamically, then remove the multiple comments

printf("Enter x and y where x + iy is the first complex number");

scanf("%f,%f", &x.real,&x.img);

printf("Enter u and v where u + iv is the second complex number");

scanf("%f,%f", &y.real,&y.img);

*/

do

{

printf("Enter an operation to perform on complex numbers");

scanf("%c",&op);

scan_complex_op(&x, &y, &op);

if(op=='+')

z=add_complex(x,y);

else if(op=='-')

z=sub_complex(x,y);

else if(op=='*')

z=mult_complex(x,y);

else

z=div_complex(x,y);

print_complex(z);

}

while(check==1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote