我得到:"expected ';', ',' or ')' before '&' token "C
I got: "expected ';', ',' or ')' before '&' token "in C
为什么&x1不能被接受
int calculte (float a, float b, float c, float &x1, float &x2)
我正在尝试求解二次方程,这是我的完整程序
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float &x1, float &x2){
float delta = b*b -4*a*c;
if (delta <0) {
x1 =x2 = 0.0;
return 0;
} if (delta == 0) {
x1 = x2 = -b/(2*a);
return 1;
} if (delta >0) {
delta = sqrt(delta);
x1 = (-b-delta)/(2*a);
x2 = (-b - delta)/(2*a);
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,x1,x2);
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
我是编程新手,所以希望你们能详细解释我。
不能在 C 中使用引用
相反,您应该通过将 &
更改为 *
来使参数指向指针。
而且函数体和调用也必须根据那个改变。
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float *x1, float *x2){ /* change the arguments to pointers */
float delta = b*b -4*a*c;
if (delta <0) {
*x1 =*x2 = 0.0; /* dereference the pointers */
return 0;
} if (delta == 0) {
*x1 = *x2 = -b/(2*a); /* dereference the pointers */
return 1;
} if (delta >0) {
delta = sqrt(delta);
*x1 = (-b-delta)/(2*a); /* dereference the pointer */
*x2 = (-b - delta)/(2*a); /* dereference the pointer */
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,&x1,&x2); /* add & to get the pointers */
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
为什么&x1不能被接受
int calculte (float a, float b, float c, float &x1, float &x2)
我正在尝试求解二次方程,这是我的完整程序
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float &x1, float &x2){
float delta = b*b -4*a*c;
if (delta <0) {
x1 =x2 = 0.0;
return 0;
} if (delta == 0) {
x1 = x2 = -b/(2*a);
return 1;
} if (delta >0) {
delta = sqrt(delta);
x1 = (-b-delta)/(2*a);
x2 = (-b - delta)/(2*a);
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,x1,x2);
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
我是编程新手,所以希望你们能详细解释我。
不能在 C 中使用引用
相反,您应该通过将 &
更改为 *
来使参数指向指针。
而且函数体和调用也必须根据那个改变。
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float *x1, float *x2){ /* change the arguments to pointers */
float delta = b*b -4*a*c;
if (delta <0) {
*x1 =*x2 = 0.0; /* dereference the pointers */
return 0;
} if (delta == 0) {
*x1 = *x2 = -b/(2*a); /* dereference the pointers */
return 1;
} if (delta >0) {
delta = sqrt(delta);
*x1 = (-b-delta)/(2*a); /* dereference the pointer */
*x2 = (-b - delta)/(2*a); /* dereference the pointer */
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,&x1,&x2); /* add & to get the pointers */
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}