#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=ab;
}
else
{
answer=a/b;
}
printf("ANS = %f\n",answer);
return 0;
}
為甚麼用float可以執行正確,double就不對???
請把 %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;
}