無法計算出兩個複數的總和和差值。C函數無法正常工作
#include <stdio.h>
void sum_diff(double *r3, double *i3, double *r4, double *i4);
int main()
{
double r3, i3, r4, i4, s3, s4, d3, d4;
printf("Enter r3 and i3 where r3 + i3 is the first complex number.\n");
printf("r3 = ");
scanf("%lf", &r3);
printf("i3 = ");
scanf("%lf", &i3);
printf("Enter r4 and i4 where r4 + i4 is the second complex number.\n");
printf("r4 = ");
scanf("%lf", &r4);
printf("i4 = ");
scanf("%lf", &i4);
sum_diff(&r3, &i3, &r4, &i4);
printf("The sum of the two complex numbers = %.3lf + %.3lf\n.", s3, s4);
printf("The difference of the two complex numbers = %.3lf - %.3lf\n.", d3, d4);
return 0;
}
void sum_diff(double *r3, double *i3, double *r4, double *i4)
{
double s3, s4, d3, d4;
s3 = *r3 + *r4;
s4 = *i3 + *i4;
d3 = *r3 - *r4;
d4 = *i3 - *i4;
}
請發表你的意思是「不工作」,或者你得到的錯誤。猜測你正在收到編譯錯誤。 – TenG
我對這個模糊的描述表示歉意。所以基本上當我在CodeBlocks中運行它時,它會提示我輸入複數。總和和差異沒有被計算。它只給我「兩個複數的總和= 0.000 + 0.000」是否因爲我的引用在void函數中是不正確的? – lg713
你實際上*正在計算總和和差值......你只是沒有對它們做任何事情,或者把它們傳回給main()。'sum_diff()'中的's3','s4'等與* main()中的變量不同*,它們是'sum_diff()'函數的本地變量實際上每次調用該函數)。 – Dmitri