C programming Complex Number Calculator (35 points) Create a complex number calc
ID: 673633 • Letter: C
Question
C programming
Complex Number Calculator (35 points)
Create a complex number calculator (For a brief overview on complex numbers, take a look at http://mathworld. wolfram.com/ComplexNumber.html). Your calculator should be able to compute the following:
• Addition of two complex numbers: z1 + z2
• Subtraction of two complex numbers: z1 z2
• Multiplication of two numbers: z1 · z2
• Division of two complex numbers: z1 z2
• Computation of the magnitude and phase (in degrees) of a complex number z. 1
The program should ask the user which of the five operations he wants to perform. It then asks the user to enter the appropriate values for the complex numbers and performs the necessary computation. Your program should execute continuously until the user requests to terminate it.
Sample run:
Press + for addition, - for subtraction, * for multiplication, / for division, m for magnitude and phase, or any other key to exit: +
Enter a complex number: 2 5
Enter a complex number: 1 8
(2+5i)+(1+8i)=(3+13i)
Press + for addition, - for subtraction, * for multiplication, / for division, m for magnitude and phase, or any other key to exit: n
Your program should include the following functions:
1. void read complex(float *z r, float *z i) // reads a complex number from the keyboard
2. void print complex(float z r, float z i) // prints a float number on the screen in the form a+bi
3. void add complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1+z2
4. void sub complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z2-z1
5. void mul complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1· z2
6. void div complex(float z1 r, float z1 i, float z2 r, float z2 i, float *z3 r, float *z3 i) // z3=z1 z2
7. void par complex(float z1 r, float z1 i, float *mag, float *ph) // magnitude and phase
We cannot use arrays or strings. Can someone please help me
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void readComplex(float *zr, float *zi) {
printf("Enter a complex number: ");
scanf("%f %f", zr, zi);
}
void printComplex(float zr, float zi) {
printf("%10.2f + %10.2fi", zr, zi);
}
void addComplex(float z1r, float z1i, float z2r, float z2i, float *z3r, float *z3i) {
*z3r = z1r+z2r;
*z3i = z1i + z2i;
}
void subComplex(float z1r, float z1i, float z2r, float z2i, float *z3r, float *z3i) {
*z3r = z1r - z2r;
*z3i = z1i - z2i;
}
void divComplex(float z1r, float z1i, float z2r, float z2i, float *z3r, float *z3i) {
*z3r = (z1r*z2r + z1i*z2i)/(z2i*z2i + z2r*z2r);
*z3i = (z1i*z2r - z1r*z2i)/(z2i*z2i + z2r*z2r);
}
void mulComplex(float z1r, float z1i, float z2r, float z2i, float *z3r, float *z3i) {
*z3r = (z1r*z2r - z1i*z2i);
*z3i = (z1r*z2i + z1i*z2r);
}
void parComplex(float z1r, float z1i, float *mag, float *ph) {
*mag = sqrt(z1r*z1r + z1i*z1i);
*ph = atanf(z1i/z1r);
}
int main() {
char ch;
float z1r,z1i;
float z2r,z2i;
float z3r,z3i;
float mag, ph;
while (1) {
printf("Press + for addition, - for subtraction, * for multiplication, / for division, m for magnitude and phase, or any other key to exit:");
scanf("%c", &ch);
readComplex(&z1r, &z1i);
readComplex(&z2r, &z2i);
switch (ch) {
case '+':
addComplex(z1r, z1i, z2r, z2i, &z3r, &z3i);
printComplex(z3r, z3i);
break;
case '-':
subComplex(z1r, z1i, z2r, z2i, &z3r, &z3i);
printComplex(z3r, z3i);
break;
case '/':
divComplex(z1r, z1i, z2r, z2i, &z3r, &z3i);
printComplex(z3r, z3i);
break;
case '*':
mulComplex(z1r, z1i, z2r, z2i, &z3r, &z3i);
printComplex(z3r, z3i);
break;
case 'm':
parComplex(z1r, z1i, &mag, &ph);
printf("Magnitude is: %10.2f and Phase is: %10.2f", mag, ph);
break;
default:
return 0;
break;
}
printf(" ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.