3个回答
展开全部
#include <stdlib.h>
#include <string.h>
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<stdio.h>
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);}
}
}
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);}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询