图片.png
注意a为0的情况。
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
class Mul{
public:
int a,b,c;
public:
int getRes(int aa,int bb,int cc,double &flag1,double &flag2){
a = aa;
b = bb;
c = cc;
double delta;
delta = b*b*1.0-4*a*c*1.0;
if (delta < 0)
{
flag1 = -1;
flag2 = -1;
return -1;
}
else if (fabs(delta)<1E-8)//算delta==0
{
flag1 = (-b*1.0/(2.0*a));
flag2 = (-b*1.0/(2.0*a));
return 0;
}
else//delta > 0
{
flag1 = (-b*1.0+sqrt(delta))/(2.0*a);
flag2 = (-b*1.0-sqrt(delta))/(2.0*a);
return 1;
}
}
double getRes2(int bb,int cc){
return -(cc*1.0)/(bb*1.0);
}
};
int main()
{
int m;
int a,b,c;
double flag1,flag2;
Mul M;
cin >> m;
for (int i=0;i<m;i++)
{
cin >> a >> b >> c;
if (a==0)
{
printf("x=%.2f\n",M.getRes2(b,c));
}
else
{
switch (M.getRes(a,b,c,flag1,flag2))
{
case -1:printf("%d\n",-1); break;
case 0: printf("x=%.2f\n",flag1);break;
case 1: printf("x1=%.2f,x2=%.2f\n",flag2,flag1);break;
default: break;
}
}
}
return 0;
}
网友评论