我是一個完整的初學者編程和我有一個任務要做,它的幾乎就是從複數中找到R和theta,並採用適當的操作,使用if語句,取決於哪個象限它在。複數 - 尋找theta - C編程
ie;當在象限1 & 2使用計算出的θ表示當在從計算theta和當在象限4象限3減法180degrees加180度至計算THETA
林只是確實很難找到THETA,當我輸入1 + 1J我得到正確的R,但錯誤的theta。我正在使用theta = atan(b/a);
#include <stdio.h>
int main()
{
float a, b, r, j, theta, thetaquadrant3, thetaquadrant4, convert ;
j = -1;
b = b*-1;
thetaquadrant3 = theta - 180;
thetaquadrant4 = theta + 180;
printf ("Please enter intput A and B in the form of a+bj\n");
printf ("Input A:");
scanf ("%f" , &a);
printf ("Input B:");
scanf ("%f" , &b);
if ((a>=0.0) && (b >= 0.0))
{
//take no action as the calculated angle is in quadrant 1
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=%f\n\n" , r);
theta = atan(b/a);
printf ("Theta=%f\n\n", theta);
}
if ((a<=-0.0) && (b >= 0.0))
{
//take no action as the calculated angle is in quadrant 2
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=%f\n\n" , r);
theta = atan(b/a);
printf ("Theta=%f\n\n", theta);
}
if ((a<=-0.0) && (b <= -0.0))
{
//Quadant 3
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=%f\n\n" , r);
theta = atan(b/a);
printf ("Theta=%f\n\n", thetaquadrant3);
}
if ((a>=0.0) && (b <= -0.0))
{
//Quadrant 4
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=%f\n\n" , r);
theta = atan(b/a);
printf ("Theta=%f\n\n", thetaquadrant4);
}
// Converting back to rectangular Co-ordinates
convert = r*cos(theta) + j*r*sin (theta);
printf ("Corresponds to%f\n\n" , convert);
return 0;
}
任何幫助是極大的讚賞
爲此,有一個名爲'atan2(dividend,divisor)'的函數。 – dialer 2013-04-07 09:58:24