给定a = 1,b = 5和c = 6的值,x的值应该是-2和-3,但是下面的程序给出x的值为6和-11,这是不正确的.如果有人能弄清楚该计划有什么问题,我将不胜感激.
#include<iostream.h>
#include<conio.h>
int main()
{
char reply;
int a,b,c,q,z;
do
{
cout<<"Enter the value of a: ";
cin>>a;
cout<<"\nEnter the value of b: ";
cin>>b;
cout<<"\nEnter the value of c: ";
cin>>c;
q=(-b-(b*b-4*a*c)sqrt(b))/2/a;
z=(-b+(b*b-4*a*c)sqrt(b))/2/a;
cout<<"\nThe values of x are "<<q<<" and "<<z;
cout<<"\nDo you want to find another values of x(y/n)?";
cin>>reply;
}
while(reply=='y');
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该^符号实际上是按位异或运算符,而不是权力或指数运算符,所以b^2实际上是b xor 2.试试吧b*b.
如果需要将基数增加到2以外的幂指数,则需要使用该pow函数.
并使用sqrt函数(in <math.h>)计算平方根,而不是将数字提高到1/2的幂.
此外,a/b*c被解析为(a/b)*c,所以你需要括号:
(...)/(2*a);
Run Code Online (Sandbox Code Playgroud)
或者做第二个部门:
(...)/2/a;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2733 次 |
| 最近记录: |