This question asks you to write a calculator that can perform a single arithmeti
ID: 3631462 • Letter: T
Question
This question asks you to write a calculator that can perform a single arithmetic operation on two complex numbers, including addition, subtraction, multiplication and division. The calculator will be implemented as a UNIX filter which gets its input from the standard input, and outputs its result to the standard output. The syntax of the language of the Input for this calculator is: option(real1, imaginary1)(real2,imaginary2) There is no white space in the input. Here option is an integer, and real1, imaginary1, real2 and imaginary2 are floating point numbers (i.e. use the keyword float of C in your program to define variables for them). The value of option can only be one of the following four integers: 1, 2, 3 and 4, which correspond to addition, subtraction, multiplication and division, respectively. The real and imaginary parts of the left operand are real1 and imaginary1, respectively. The real and imaginary parts of the right operand are real2 and imaginary2, respectively. For example, the input 2(5.335,2)(7.1,5.3) means (5.335 +2i) - (7 .1 +5.31). The following is the syntax for output: There is no white space in the output, but there is a trailing newline. In the output, result re al and result imaginary are floating point numbers (your program should display exactly 2 digits after the decimal point). For example, the output (-1.76,-3.30) means - l.76 - 3.30i, which is the result of the example above.Explanation / Answer
#include<stdio.h> int main() { float real1, imag1, real2,imag2; //variables to hold input values float real_res, imag_res; //variables to hold result int opr; //variable to hold operator choice scanf("%d(%f,%f)(%f,%f)",&opr,&real1,&imag1,&real2,&imag2); //to take input in specified format switch (opr) { case 1: //addition real_res=real1+real2; imag_res=imag1+imag2; break; case 2: real_res=real1-real2; //subtraction imag_res=imag1-imag2; break; case 3: //multiplication real_res=real1*real2-imag1*imag2; imag_res=real1*imag2+real2*imag1; break; case 4: //division real_res=(real1*real2+imag1*imag2)/(real2*real2+imag2*imag2); imag_res=(-real1*imag2+real2*imag1)/(real2*real2+imag2*imag2); break; default: //to throw error message printf("Invalid operator!"); break; } printf("(%7.2f,%7.2f)",real_res,imag_res); //output result return 1; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.