问题描述
为什么不能接受&x1
with open('my-file','r') as lines:
numbers = [int(line[9:16]) for line in lines.readlines() if len(line.strip()) > 0]
print(numbers)
我正在尝试求解二次方程,这是我的完整程序
[3933789,4763238,2821926,3154047,3471816,4350884,2809798,2861733,4556980,4811477,3271181,3549280,4879671,2938390,3186417,3498278,3601842,3445503,3491835]
我是编程新手,所以希望你们能向我解释更多细节。
解决方法
不能在 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,&x1,&x2); /* add & to get the pointers */
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f",x2);
}
}