gpt4 book ai didi

c - 在函数内链接函数时出错

转载 作者:行者123 更新时间:2023-11-30 17:39:26 25 4
gpt4 key购买 nike

请耐心等待,因为这可能是一个微不足道的问题!我不知道我做错了什么。当尝试编写一个程序以某种方式求解二次方程时,我定义了 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);}
}

提前致谢!

最佳答案

全部lin_root()int quad_roots() { ... 调用的函数调用不存储从 lin_root() 返回的返回值。这可能是您的代码无法工作的原因。

关于c - 在函数内链接函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21812068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com