#include
#include
int Calculate(char* input, float *ret)
{
int returnRet = 0;
float num1, num2, symbol;
int len = strlen(input);
char* num1Str = (char*)malloc(sizeof(char) * len);
char* num2Str = (char*)malloc(sizeof(char) * len);
memset(num1Str, 0, len);
memset(num2Str, 0, len);
int getSymbol = 0;
for (int i = 0, j = 0; i < len; i++)
{
if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')
{
symbol = input[i];
getSymbol = 1;
continue;
}
if (getSymbol == 0)
num1Str[i] = input[i];
else
num2Str[j++] = input[i];
}
num1 = atof(num1Str);
num2 = atof(num2Str);
if(symbol == '+')
*ret = num1 + num2;
else if(symbol == '-')
*ret = num1 - num2;
else if(symbol == '*')
*ret = num1 * num2;
if(symbol == '/')
{
if(num2 != 0)
*ret = num1 / num2;
else
returnRet = 1;//data error
}
free(num2Str);
free(num1Str);
return returnRet;
}
int main(void)
{
float ret;
char input[100];
memset(input, 0, sizeof(input));
scanf("%s",input);
if(Calculate(input,&ret) == 0)
printf("%.2f",ret);
else
printf("data erro");
return 0;
}
#include
void main() { float a,b,c; char d,e;
scanf("%f%c%f",&a,&d,&b); e=1;
switch ( d ) {
case '+': c=a+b; break;
case '-': c=a-b; break;
case '*': c=a*b; break;
case '/': if ( b==0 ) e=0; else c=a/b; break;
default: e=0; break;
}
if ( e ) printf("%.2f\n",c); else printf("data erro\n");
}
#include
main()
{
double a,b,w,x,y,z;
char ch;
while(scanf("%lf %c %lf",&a,&ch,&b)!=1)
{
if(ch=='*')
{w=a*b;printf("%lf*%lf=%.2lf\n",a,b,w);}
if(ch=='/')
{
if(b==0)printf("输入有误\n");
else {x=a/b;printf("%lf/%lf=%.2lf\n",a,b,x);}
}
if(ch=='+')
{y=a+b;printf("%lf+%lf=%.2lf\n",a,b,y);}
if(ch=='-')
{z=a-b;printf("%lf-%lf=%.2lf\n",a,b,z);}
}
}