C语言高分悬赏简单题

来源:百度知道 编辑:UC知道 时间:2024/06/05 10:49:00
void main()
{
int a,b,c,x1,x2,d;
printf("Please input the three coefficients:");
scanf("%d,%d,%d",&a,&b,&c);
d=(b^2-4*a*c)^1/2;
if(b^2>=4*a*c){if(b^2==4*a*c){printf("x1=x2=(-%d)/2/%d\n",b,a);

}else printf("x1=(-%d+%d)/2/%d,x2=(-%d-%d)/2/%d\n",b,d,a,b,d,a);

}else printf("There is no real root.\n");

getch();
}运行的时候无论3个系数输什么,它都只属于两实根相等的情况。另外,得出的结果,是10/3/2之类的未计算完整的式子。不是一个数字。。。到底哪有问题?感激大家

^是位运算符,若想求数的平方可用sqrt函数,
原型:extern float sqrt(float x);
头文件:math.h

#include "stdio.h"
#include "math.h"

void main()
{
int a,b,c,x1,x2,d;
printf("Please input the three coefficients:");
scanf("%d,%d,%d",&a,&b,&c);

if(b*b>=4*a*c)
{
d=sqrt(b*b-4*a*c);//应该放在这里,要不然b*b<4*a*c时报错;
if(b*b==4*a*c){printf("x1=x2=(-%d)/2/%d\n",b,a);}
else printf("x1=(-%d+%d)/2/%d,x2=(-%d-%d)/2/%d\n",b,d,a,b,d,a);
}
else printf("There is no real root.\n");
getch();
}

楼上说得对:^是位运算符,若想求数的平方可用sqrt函数

1,你在声明中没有用math这个库函数,这个在又的编译器中有用,有的编译器没有用,
2.不能用^这个符号,开方的话你可以用 sqrt这个函数,呵呵

#include "math.h"
power(x,2);//x的平方

整个程序编的都有错误!重写吧!