gpt4 book ai didi

c - 二次方程 - 不理解编译器错误

转载 作者:行者123 更新时间:2023-11-30 21:49:05 25 4
gpt4 key购买 nike

所以我正在尝试编写一个 C 程序来求解二次方程。我首先从头开始编写它,但它显示了相同的错误,因此我根据 C 编程书籍对其进行了一些更改。结果如下:

/*
Solves any quadratic formula.
*/
#include <stdio.h>
#include <math.h>

void main()
{
float a, b, c, rt1= 0, rt2=0, discrim;
clrscr();
printf("Welcome to the Quadratic Equation Solver!");
getch();
printf("\nYour quadratic formula should be of the form a(x*x)+bx+c = 0");
printf("\nPlease enter a\'s value:");
scanf("%f", &a);
printf("Great! Now enter b\'s value:");
scanf("%f", &b);
printf("One more to go! Enter c\'s value:");
scanf("%f", &c);
discrim = b*b - 4*a*c;
if (discrim < 0)
printf("\nThe roots are imaginary.");
else
{
rt1 = (-b + sqrt(discrim)/(2.0*a);
rt2 = (-b - sqrt(discrim)/(2.0*a);
printf("\nThe roots have been calculated.");
getch();
printf("\nThe roots are:\nRoot 1:%f\nRoot 2:%f",rt1, rt2);
getch();
printf("\nThank you!");
getch();
}
}

最佳答案

你在这里漏掉了一些括号:

rt1 = (-b + sqrt(discrim)/(2.0*a);
rt2 = (-b - sqrt(discrim)/(2.0*a);

它应该看起来像这样:

rt1 = (-b + sqrt(discrim))/(2.0*a);
rt2 = (-b - sqrt(discrim))/(2.0*a);

您可以查看编译器警告。我的 (g++) 打印如下内容:

file.c:24:36: error: expected ‘)’ before ‘;’ token

24 是您应该检查错误的行号,36 是该行中的列号。

关于c - 二次方程 - 不理解编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17791480/

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