C program 問題(幫我寫program)急!!

2009-04-05 1:41 am
問題如下:(最好解釋下點解要咁寫) THX:)


Write a C program to determine the root(S) of a quadratic equation.
[User inputs are in italic]


Roots = (-b±sqrt(b^2-4ac))/(2a)


Sample output 1:
Please enter the coefficients: 1 2 1
This equation has a double root: -1


Sample output 2:
Please enter the coefficients: 4 2 1
This equation has no real roots.


Sample output 3:
Please enter the coefficients: 1 1 -12
This equation has 2 distinct roots: 3 -4
更新1:

只要satisfy所有sample output就ok啦!

回答 (3)

2009-04-06 1:24 am
✔ 最佳答案
nt main()
{
float a;
float b;
float c;
float x1;
float x2;
float one_root;

printf("Please enter value for a:");
scanf("%f", &a);

printf("Please enter value for b:");
scanf("%f", &b);

printf("Please enter value for c:");
scanf("%f", &c);

if (a == 0 && b ==0)
{
printf("There are no solutions.");
}
if (a == 0)
{
one root = -c / b;
printf("There is one root of %f.);
}
if (sqrt(pow (b ,2)) - (4 * a * c)) < 0)
{
printf("There are no real roots.");
}
if (a < 0 && a > 0 && b < 0 && b > 0)
{
x1 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
x2 = (-b + sqrt ((pow (b, 2)) - (4 * a * c))) / (2 * a);
printf("The two real roots are %f and %f.", x1, x2);
}

return 0;
}int main()
{
float <strong class="highlight">a</strong>;
float b;
float <strong class="highlight">c</strong>;
float x1;
float x2;
float one_root;

printf("Please enter value for <strong class="highlight">a</strong>:");
scanf("%f", &a);

printf("Please enter value for b:");
scanf("%f", &b);

printf("Please enter value for <strong class="highlight">c</strong>:");
scanf("%f", &c);

if (<strong class="highlight">a</strong> == 0 && b ==0)
{
printf("There are no solutions.");
}
if (<strong class="highlight">a</strong> == 0)
{
one root = -c / b;
printf("There is one root <strong class="highlight">of</strong> %f.);
}
if (sqrt(pow (b ,2)) - (4 * <strong class="highlight">a</strong> * <strong class="highlight">c</strong>)) < 0)
{
printf("There are no real roots.");
}
if (<strong class="highlight">a</strong> < 0 && <strong class="highlight">a</strong> > 0 && b < 0 && b > 0)
{
x1 = (-b + sqrt ((pow (b, 2)) - (4 * <strong class="highlight">a</strong> * <strong class="highlight">c</strong>))) / (2 * <strong class="highlight">a</strong>);
x2 = (-b + sqrt ((pow (b, 2)) - (4 * <strong class="highlight">a</strong> * <strong class="highlight">c</strong>))) / (2 * <strong class="highlight">a</strong>);
printf("The two real roots are %f and %f.", x1, x2);
}

return 0;
}

上網copy的,睇下得5得
2009-04-05 9:27 pm
只要satisfy所有sample output就ok啦!
2009-04-05 2:44 pm
Are you sure all numbers are integers? If not, there will be an error term that you will have to provide. As for all numerical calculations, you will have to face the small error due to inaccuracy.


收錄日期: 2021-04-30 12:54:55
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090404000051KK01218

檢視 Wayback Machine 備份