iT邦幫忙

0

為甚麼用float可以執行正確,double就不對???

不明 2019-08-28 16:05:341711 瀏覽
  • 分享至 

  • xImage

#include <stdio.h>
int main ()
{
double a,b,answer;
char c;
printf("Please enter the formula =");
scanf("%f%c%f",&a,&c,&b);
if(c=='+')
{
answer=a+b;
}
else if(c=='-')
{
answer=a-b;
}
else if(c=='')
{
answer=a
b;
}
else
{
answer=a/b;
}
printf("ANS = %f\n",answer);
return 0;
}
為甚麼用float可以執行正確,double就不對???

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
神威
iT邦研究生 4 級 ‧ 2019-08-28 16:31:46
最佳解答

請把 %f 改成 %lf

#include <stdio.h>
int main ()
{
double a,b,answer=0;
char c;
printf("Please enter the formula =");
scanf("%lf %c %lf",&a,&c,&b);
if(c=='+')
{
answer=a+b;
}
else if(c=='-')
{
answer=a-b;
}
else if(c=='*')
{
answer=a*b;
}
else
{
answer=a/b;
}
printf("ANS = %lf\n",answer);
return 0;
}

我要發表回答

立即登入回答