我一直在尝试编写一个求解二次方程的程序。对我来说一切似乎都很好,但我尝试再次使用的功能在我调用它们之后并没有启动。我不明白我做错了什么。
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, delta, x1, x2;
char YN;
void enter(){
printf("Insert the value of 'a':\n");
scanf("%f", &a);
printf("Insert the value of 'b':\n");
scanf("%f", &b);
printf("Insert the value of 'c':\n");
scanf("%f", &c);
delta = (b*b) - (4 * a * c);
x1 = (-b-sqrt(delta))/(2*a);
x2 = (-b+sqrt(delta))/(2*a);
}
void solve(){
if (delta > 0){
printf("The first root of the equation is %f.", x1);
printf("The second root of the equation is %f.", x2);
}
else if (x1 == x2){
printf("The only root of the equation is %f.", x1);
}
else{
printf("The equation has no real roots.");
}
}
void input(){
scanf("%c", &YN);
}
void check(){
if (YN == 'Y'){
solve();
}
else if (YN == 'N'){
enter();
}
else {
input();
}
}
enter();
printf("Your equation must be: %f x^2 + %f x + %f, is it correct? Type Y for yes, N for no.\n", a, b, c);
input();
check();
return 0;
}
因为我认为变量使函数不起作用,所以我尝试在 solve 函数之外使用变量,但它并没有真正起作用。