复数计算求检验,自己检验过了不过不完全确定正确
//author: Yuchao liao //2014/3/11
//a complex number calculator
//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.
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
void read_complex(float *z_r, float *z_i) // reads a complex number from the keyboard
{
float a,b;
printf("Enter a complex number:");
scanf_s("%f %f",&a,&b);
*z_r=a;
*z_i=b;
}
void print_complex(float z_r, float z_i)
{
printf("result:%.1f+(%.1fi)",z_r,z_i);
}
void add_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z1+z2
{
*z3_r=z1_r+z2_r;
*z3_i=z1_i+z2_i;
}
void sub_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z2-z1
{
*z3_r=z1_r-z2_r;
*z3_i=z1_i-z2_i;
}
void mul_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) //z3=z1*z2
{
*z3_r=z1_r*z2_r-z1_i*z2_i;
*z3_i=z1_i*z2_r+z2_i*z1_r;
}
void div_complex(float z1_r, float z1_i, float z2_r, float z2_i, float *z3_r, float *z3_i) // z3=z1/z2
{
*z3_r=(z1_r*z2_r+z1_i*z2_i)/(z2_r*z2_r+z2_i*z2_i);
*z3_i=(z1_i*z2_r-z2_i*z1_r)/(z2_r*z2_r+z2_i*z2_i);
}
void par_complex(float z1_r, float z1_i, float *mag, float *ph) // magnitude and phase
{
*mag=z1_r*z1_r+z1_i*z1_i;
*ph=atan(z1_i/z1_r)*180/PI;
}
[ 本帖最后由 yuchao130 于 2014-3-14 05:11 编辑 ]