当结果中a或者b小于0,大于0.005时,保留两位小数会输出-0.00,但应输出0.00。
1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 5 int main() 6 { 7 double r1, p1, r2, p2; 8 cin >> r1 >> p1 >> r2 >> p2; 9 double a1 = r1 * cos(p1), b1 = r1 * sin(p1); 10 double a2 = r2 * cos(p2), b2 = r2 * sin(p2); 11 double a3 = a1 * a2 - b1 * b2; 12 double b3 = a1 * b2 + b1 * a2; 13 if (a3 > -0.005 && a3 < 0) a3 = -a3; 14 if (b3 > -0.005 && b3 < 0) b3 = -b3; 15 if (b3 < 0) 16 printf("%.2f%.2fi", a3, b3); 17 else 18 printf("%.2f+%.2fi", a3, b3); 19 return 0; 20 }