#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c,disc,x1,x2,p,q;
printf("请输入系数a,b,c的值:(用空格键分隔)\n");
scanf("%lf%lf%lf",&a,&b,&c);
disc = b*b - 4*a*c;
if(disc < 0)
{
printf("此方程无实数根:\n");
return 1;
}
if (disc > 0)
{
p = -b/(2.0*a);
q = sqrt(disc)/(2.0*a);
x1 = p + q;
x2 = p - q;
printf("方程的两个根:\nx1=%7.2f\nx2=%7.2f\n",x1,x2);
}
else
{
printf("方程有一个重根:\nx1=x2=%7.2f",x1);
}
return 0;
}
上面是编写的解一元二次方程 的代码。在ubuntu安装的vscode运行时报错,提示sqrt没有定义。解决方案如下:
*在命令行模式下gcc可以通过加 -lm 解决
自带编译在tasks.json文件中添加
code runner打开设置,搜索code runner,找到Executor Map,在settings.json中修改