请耐心等待,因为这可能是一个微不足道的问题!我不知道我做错了什么。当我尝试编写程序以某种方式求解二次方程时,我定义了3个函数:main,quad_roots和lin_root,其中quad_roots求解二次方程,lin_root求解线性方程。
问题1:对于我将lin_root函数调用到我的二次函数中的情况,它不会输出它应该的内容。我做错了什么?
问题2:我想知道是否有更简洁的编码方式,保持返回值不变?
我的主要功能:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int main(void)
{ double a2,a1,a0;
double root1,root2;
int quad_case;
int lin_case;
printf("Enter the coefficients of linear equation a2*x^2+a1*x+a0=0 \n");
printf("In the order a2,a1,a0, separated by spaces: ");
scanf_s("%lf %lf %lf",&a2,&a1,&a0);
quad_case=quad_roots(a2,a1,a0,&root1,&root2);
lin_case=lin_root(a1,a0,&root1);
switch (quad_case) {
case -3 : printf("\n x = any number i.e. there are infinitely many roots \n");
break;
case -2 : printf("\n There are no roots \n");
break;
case -1 :
switch (lin_case) {
case -1 : printf("\n x = any number i.e. there are infinitely many roots \n");
break;
case 1 : printf("\n This is a linear equation with one root r1 = %g \n",root1);
break;
case 0 : printf("\n There are no roots \n");
break;
}
break;
case 2 : printf("\n Two distinct real roots r1 = %g and r2 = %g \n",root1,root2);
break;
case 1 : printf("\n Two identical real roots r1 = r2 = %g \n",root1);
break;
case 0 : printf("\n Two complex conjugate roots r1 = %g + %gi and r2 = %g - %gi \n",root1,root2,root1,root2);
break;
default: printf("\n NAN \n");
break;
}
}
我的lin_root功能:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int lin_root(double A, double B, double* r1)
{
if(A != 0)
{*r1=-B/A;
return(1);
}
else if(A == 0 && B != 0)
{
return(0);
}
else if(A == 0 && B == 0)
{
return(-1);
}
}
我的quad_roots功能:
#include<stdio.h>
#include<math.h>
#pragma warning(disable: 4996)
int quad_roots(double a2,double a1, double a0, double* r1, double* r2)
{ double discriminant, determinant, two=2, four=4, p, q;
p=(a1/a2);
q=(a0/a2);
discriminant=a1*a1-four*a2*a0;
determinant=p*p-four*q;
if (a2 == 0 && a1 == 0 && a0 == 0)
{
return(-3);
}
else if (a2 == 0 && a1 == 0 && a0 != 0)
{
return(-2);
}
else if (a2 == 0)
{lin_root(a1,a0,r1);
return(-1);
}
else if(a2 != 0 && a1 != 0 && a0 == 0)
{lin_root(a2,a1,r1);
*r2 = 0;
return(2);
}
else if(determinant > 0 && a1 > 0)
{*r1 = -(p/two) + (((sqrt(p))*(sqrt(p-four*(a0/a1))))/two);
*r2 = (a0/a2)/(*r1);
return(2);
}
else if(determinant > 0 && a1 == 0 && a2 < 0)
{*r1 = ((sqrt(-a2))*(sqrt(a0)))/(a2);
*r2 = -((sqrt(-a2))*(sqrt(a0)))/(a2);
return(2);
}
else if(determinant > 0 && a1 == 0 && a2 > 0)
{*r1 = ((sqrt(-a0))*(sqrt(a2)))/(a2);
*r2 = -((sqrt(-a0))*(sqrt(a2)))/(a2);
return(2);
}
else if(determinant > 0 && a1 < 0)
{*r1 = -(p/two) + (((sqrt(-p))*(sqrt(-p-(-four*q*(1/p)))))/two);
*r2 = (a0/a2)/(*r1);
return(2);
}
else if(determinant == 0)
{*r1 = *r2 = -a1/(two*a2);
return(1);
}
else if(determinant < 0 && a1 > 0)
{*r1 = -(p/two);
*r2 = (((sqrt(p))*(sqrt(p-four*(a0/a1))))/two);
return(0);
}
else if(determinant < 0 && a1 == 0 && a2 < 0)
{*r1 = 0;
*r2 = ((sqrt(-a2))*(sqrt(a0)))/(a2);
return(0);
}
else if(determinant < 0 && a1 == 0 && a2 > 0)
{*r1 = 0;
*r2 = ((sqrt(-a0))*(sqrt(a2)))/(a2);
return(0);
}
else if(determinant < 0 && a1 < 0)
{*r1 = -(p/two);
*r2 = (((sqrt(-p))*(sqrt(-p-(-four*q*(1/p)))))/two);
return(0);}
}
提前致谢!
答案 0 :(得分:0)
从lin_root()
调用的所有int quad_roots() { ...
函数调用都不存储从lin_root()
返回的返回值。这可能是您的代码无法工作的原因。
答案 1 :(得分:0)
以下是问题2 :
的答案#include <stdio.h>
#include <math.h>
void test_lin(float b, float c)
{
if((b==0) & (c==0))
puts("x = any number i.e. there are infinitely many roots");
else if(b==0)
puts("There are no roots");
else printf("root, r = %g", -c/b);
return;
}
void test_quad(float a, float b, float c)
{
float d = b*b - 4*a*c, A = 2*a;
if(d == 0)
printf("\n Two identical real roots r1 = r2 = %g", -b/A);
else if(d>0)
printf("\nTwo distinct real roots r1 = %g and r2 = %g", -b/A + sqrt(d)/A, -b/A - sqrt(d)/A );
else {
d *= -1;
printf("\n Two complex conjugate roots r1 = %g + %g i and r2 = %g - %g i \n", -b/A , sqrt(d)/A, -b/A , sqrt(d)/A);
}
}
int main()
{
float a,b,c , r1,r2;
puts("ax*2 + bx + c = 0 : ");
scanf("%f %f %f", &a, &b, &c);
if(a==0)
test_lin(b,c);
else test_quad(a,b,c);
return 0;
}