c语言编程题精度扣分,C语言的精度问题怎么解决?

C语言的精度问题怎么解决?

#include

#include

#include

int main()

{

int count=0;

double x1,x2,a,b,c,san,A,B;

//freopen("in.txt","r",stdin);

//freopen("out.txt","w",stdout);

for (;;)

{

scanf("%lf",&a);

count++;

if (a==0)

{

return 0;

}

else

{

scanf("%lf %lf",&b,&c);

printf("Case %d :\n",count);

if(a<0)

{

a=-a;

b=-b;

c=-c;

}

if (c==0)

{

if (a==1)

{

if (b==1)

{

printf("x^2 + x = 0\n");

}

else if (b==0)

{

printf("x^2 = 0\n");

}

else if (b==-1)

{

printf("x^2 - x = 0\n");

}

else if (b>0)

{

printf("x^2 + %lfx = 0\n",b);

}

else

{

printf("x^2 - %lfx = 0\n",-b);

}

}

else

{

if (b==1)

{

printf("%lfx^2 + x = 0\n",a);

}

else if (b==0)

{

printf("%lfx^2 = 0\n",a);

}

else if (b==-1)

{

printf("%lfx^2 - x = 0\n",a);

}

else if (b>0)

{

printf("%lfx^2 + %lfx = 0\n",a,b);

}

else

{

printf("%lfx^2 - %lfx = 0\n",a,-b);

}

}

}

else if (c>0)/*c>0*/

{

if (a==1)  /*c>0*/

{

if (b==1)

{

printf("x^2 + x + %lf = 0\n",c);

}

else if (b==0)

{

printf("x^2 + %lf = 0\n",c);

}

else if (b==-1)

{

printf("x^2 - x + %lf = 0\n",c);

}

else if (b>0)

{

printf("x^2 + %lfx + %lf = 0\n",b,c);

}

else

{

printf("x^2 - %lfx + %lf = 0\n",-b,c);

}

}

else /*a!=1 && c>0*/

{

if (b==1)

{

printf("%lfx^2 + x + %lf = 0\n",a,c);

}

else if (b==0)

{

printf("%lfx^2 + %lf = 0\n",a,c);

}

else if (b==-1)

{

printf("%lfx^2 - x + %lf = 0\n",a,c);

}

else if (b>0)

{

printf("%lfx^2 + %lfx + %lf = 0\n",a,b,c);

}

else

{

printf("%lfx^2 - %lfx + %lf = 0\n",a,-b,c);

}

}

}

else /*c<0*/

{

if (a==1)  /*c<0*/

{

if (b==1)

{

printf("x^2 + x - %lf = 0\n",-c);

}

else if (b==0)

{

printf("x^2 - %lf = 0\n",-c);

}

else if (b==-1)

{

printf("x^2 - x - %lf = 0\n",-c);

}

else if (b>0)

{

printf("x^2 + %lfx - %lf = 0\n",b,-c);

}

else

{

printf("x^2 - %lfx - %lf = 0\n",-b,-c);

}

}

else /*a!=1 && c<0*/

{

if (b==1)

{

printf("%lfx^2 + x - %lf = 0\n",a,-c);

}

else if (b==0)

{

printf("%lfx^2 - %lf = 0\n",a,-c);

}

else if (b==-1)

{

printf("%lfx^2 - x - %lf = 0\n",a,-c);

}

else if (b>0)

{

printf("%lfx^2 + %lfx - %lf = 0\n",a,b,-c);

}

else

{

printf("%lfx^2 - %lfx - %lf = 0\n",a,-b,-c);

}

}

}

//***********************************************

san=b*b-4*a*c;

if (san>0)

{

x1=(-b-sqrt(san))/(2*a);

x2=(-b+sqrt(san))/(2*a);

printf("two real roots : %lf, %lf\n",x1,x2);

printf("\n");

}

else if (san==0)

{

x1=-b/(2*a);

printf("only one real root : %lf\n",x1);

printf("\n");

}

else

{

san=-san;

A=-b/(2*a);

B=sqrt(san)/(2*a);

if(A!=0 && B!=0)

printf("two real roots : %lf+%lfi, %lf-%lfi\n",A,B,A,B);

if(A==0 && B!=0)

printf("two real roots : %lfi, -%lfi\n",B,B);

printf("\n");

}

}

}

return 0;

}

这是求二次方程的跟,**行之前的差不多都是为了格式

Input

输入为多行,每行为一元二次方程的三个常数a,b,c,在double类型范围之内。当输入的a为0时,表示输入结束。

Output

每行输入的样例对应三行输出。

第一行输出为样例的编号。

第二行输出为所输入常数a,b,c对应的一元二次方程的标准形式,要求输出满足a>0。

第三行输出为所输入方程的根,分为三种情况:

1. 若方程满足Δ>0,即有两不等实根x1、x2,则按顺序(先小后大)输出这两个实根。

2. 若方程满足Δ=0,即有两相同实根x,则输出一个实根。

3. 若方程满足Δ<0,即有两共轭的虚根x1、x2,则输出两个虚根,虚部符号为正的(即u+vi形式)先输出,虚部符号为负的(x-yi形式)后输出。

以上输出均不输出数学上无意义或可省略的的符号,所有数值最多保留6位有效数字。每个样例之后都有一个空行分隔。

Sample Input

1 2 1

-1 2 -1

-5 2 -0.2

-3 2 0

3 0 12

2 4 4

0

Sample Output

Case 1 :

x^2 + 2x + 1 = 0

only one real root : -1

Case 2 :

x^2 - 2x + 1 = 0

only one real root : 1

Case 3 :

5x^2 - 2x + 0.2 = 0

only one real root : 0.2

Case 4 :

3x^2 - 2x = 0

two real roots : 0, 0.666667

Case 5 :

3x^2 + 12 = 0

two imaginary roots : 2i, -2i

Case 6 :

2x^2 + 4x + 4 = 0

two imaginary roots : -1+i, -1-i

这是输入方法,当输入

-5 2 -0.2

0

来计算的时候,貌似是出现了精度问题,算出来的不对,san变量是△。

求解怎么改。。老师讲过,不过我没听懂。。。还有就是我的输出都是浮点型的,要求是有几位小数就输出几位小数,要是各位会的话帮我也改掉吧。。3Q

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值