Objective: The purpose of this assignment is to learn and use structure type and
ID: 646342 • Letter: O
Question
Objective:
The purpose of this assignment is to learn and use structure type and makefile. You will use Figure 10.10 in the PPT notes as a template for implemetation.
Description:
You are to implement a program that will demonstrate complex number operations as in Figure 10.10. The program is divided into several files: "complex.h", "complex.c", and "main.c". You should have a Makefile that will allow the user to type "make" to generate the executable "complex".
Instructions:
Here are some basic components for your program design.
First, in the Makefile, you should compile "complex.c" and "main.c" into object files separately, and make sure the object file depends on "complex.h" as well. In other words, if you modify "complex.h", both "complex.c" and "main.c" will be compiled again if you type "make". The final executable "complex" depends on linking "main.o" and "complex.o".
Second, in general, global variables and constants, structure definitions, and function prototypes are defined in the header file for inclusions. In "complex.h", you should have structure type definition for "complex_t" and function prototypes for "add_complex", "subtract_complex", "multiply_complex", "abs_complex", and "print_complex".
Third, in general, related functions are grouped/implemented in the same file. In "complex.c", implement all the functions related to complex number operations as defined in the prototypes.Remember, in addition to the system include files, you need to add #include "complex.h" in order to have the defined structure and function prototypes.
Finally, in the main program, in addition to Fig 10.10's implementation, you also form and display the multiplication of two complex numbers.
You may develop this code on whatever environment you wish, but before submitting test it on Mason, be sure it works properly. As you have previously done, create a typescript that prints your program to the screen, confirms the system you are on, runs it using various input, and submit both the typescript and the source file containing your programs to Blackboard as HW 6.
Submission:
You will submit five files: (1) complex.h, complex.c, main.c, and Makefile; (2) a script file containing a sample run.
FIGURE 10.10 CODE GIVEN:
// Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers
/*
* Operators to process complex numbers
*/
/* User-defined complex number type */
typedef struct {
double real, imag;
} complex_t;
int scan_complex(complex_t *c);
void print_complex(complex_t c);
complex_t add_complex(complex_t c1, complex_t c2);
complex_t subtract_complex(complex_t c1, complex_t c2);
complex_t multiply_complex(complex_t c1, complex_t c2);
complex_t divide_complex(complex_t c1, complex_t c2);
complex_t abs_complex(complex_t c);
/* Driver */
int
main(void)
{
complex_t com1, com2;
/* Gets two complex numbers */
printf("Enter the real and imaginary parts of a complex number ");
printf("separated by a space> ");
scan_complex(&com1);
printf("Enter a second complex number> ");
scan_complex(&com2);
/* Forms and displays the sum */
printf(" ");
print_complex(com1);
printf(" + ");
print_complex(com2);
printf(" = ");
print_complex(add_complex(com1, com2));
/* Forms and displays the difference */
printf(" ");
print_complex(com1);
printf(" - ");
print_complex(com2);
printf(" = ");
print_complex(subtract_complex(com1, com2));
/* Forms and displays the absolute value of the first number */
printf(" |");
print_complex(com1);
printf("| = ");
print_complex(abs_complex(com1));
printf(" ");
return (0);
}
/*
* Complex number input function returns standard scanning error code
* 1 => valid scan, 0 => error, negative EOF value => end of file
*/
int
scan_complex(complex_t *c) /* output - address of complex variable to
fill */
{
int status;
status = scanf("%lf%lf", &c->real, &c->imag);
if (status == 2)
status = 1;
else if (status != EOF)
status = 0;
return (status);
}
/*
* Complex output function displays value as (a + bi) or (a - bi),
* dropping a or b if they round to 0 unless both round to 0
*/
void
print_complex(complex_t c) /* input - complex number to display */
{
double a, b;
char sign;
a = c.real;
b = c.imag;
printf("(");
if (fabs(a) < .005 && fabs(b) < .005) {
printf("%.2f", 0.0);
} else if (fabs(b) < .005) {
printf("%.2f", a);
} else if (fabs(a) < .005) {
printf("%.2fi", b);
} else {
if (b < 0)
sign = '-';
else
sign = '+';
printf("%.2f %c %.2fi", a, sign, fabs(b));
}
printf(")");
}
/*
* Returns sum of complex values c1 and c2
*/
complex_t
add_complex(complex_t c1, complex_t c2) /* input - values to add */
{
complex_t csum;
csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;
return (csum);
}
/*
* Returns difference c1 - c2
*/
complex_t
subtract_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;
return (cdiff);
}
/* ** Stub **
* Returns product of complex values c1 and c2
*/
complex_t
multiply_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function multiply_complex returning first argument ");
return (c1);
}
/* ** Stub **
* Returns quotient of complex values (c1 / c2)
*/
complex_t
divide_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function divide_complex returning first argument ");
return (c1);
}
/*
* Returns absolute value of complex number c
*/
complex_t
abs_complex(complex_t c) /* input parameter */
{
complex_t cabs;
cabs.real = sqrt(c.real * c.real + c.imag * c.imag);
cabs.imag = 0;
return (cabs);
}
/* Enter the real and imaginary parts of a complex number
separated by a space> 3.5 5.2
Enter a second complex number> 2.5 1.2
(3.50 + 5.20i) + (2.50 + 1.20i) = (6.00 + 6.40i)
(3.50 + 5.20i) - (2.50 + 1.20i) = (1.00 + 4.00i)
|(3.50 + 5.20i)| = (6.27) */
Explanation / Answer
#include
#include
struct complex
{
double real, img;
};
int main()
{
int choice, temp1, temp2, temp3;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers. ");
printf("Press 2 to subtract two complex numbers. ");
printf("Press 3 to multiply two complex numbers. ");
printf("Press 4 to divide two complex numbers. ");
printf("Press 5 to exit. ");
printf("Enter your choice ");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf(" a = ");
scanf("%lf", &a.real);
printf("b = ");
scanf("%lf ", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf(" c = ");
scanf("%lf ", &b.real);
printf("d = ");
scanf("%lf ", &b.img);
}
if ( choice == 1 )
{
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = % lf + % lf i",c.real,c.img);
else
printf("Sum of two complex numbers = % lf % lf i",c.real,c.img);
}
else if ( choice == 2 )
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if ( c.img >= 0 )
printf("Difference of two complex numbers = % lf + % lf i",c.real,c.img);
else
printf("Difference of two complex numbers = % lf % lf i",c.real,c.img);
}
else if ( choice == 3 )
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if ( c.img >= 0 )
printf("Multiplication of two complex numbers = % lf + %lf i",c.real,c.img);
else
printf("Multiplication of two complex numbers = % lf % lf i",c.real,c.img);
}
else if ( choice == 4 )
{
if ( b.real == 0 && b.img == 0 )
printf("Division by 0 + 0i is not allowed.");
else
{
temp1 = a.real*b.real + a.img*b.img;
temp2 = a.img*b.real - a.real*b.img;
temp3 = b.real*b.real + b.img*b.img;
if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = % lf + % lf i",temp1/temp3,temp2/temp3);
else
printf("Division of two complex numbers = % lf % lf i",temp1/temp3,temp2/temp3);
}
else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = % lf + % lf /% lf i",temp1/temp3,temp2,temp3);
else
printf("Division of two complex numbers = % lf % lf /% lf i",temp1/temp3,temp2,temp3);
}
else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = % lf /% lf + % lf i",temp1,temp3,temp2/temp3);
else
printf("Division of two complex numbers = % lf % lf /% lf i",temp1,temp3,temp2/temp3);
}
else
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = % lf /% lf + % lf /% lf i",temp1,temp3,temp2,temp3);
else
printf("Division of two complex numbers = % lf /% lf % lf /% lf i",temp1,temp3,temp2,temp3);
}
}
}
else
printf("Invalid choice.");
printf(" Press any key to enter choice again... ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.