java算术运算窗口可混合运算,java做一个计算器怎样实现混合运算(优先级)解决方案...

C/C++ code//表达式求值

#include

#include

#include

/*

*功能:根据运算符计算

*参数:a, b参与运算的数, ch运算符

*返回值:计算结果,操作符错误则返回0

*/

int cal(int a, char ch, int b)

{

switch(ch)

{

case '+':

return a+b;

break;

case '-':

return a-b;

break;

case '*':

return a*b;

break;

case '/':

return a/b;

break;

}

return 0;

}

/*

*功能:计算表达式的值(用数组模拟栈)

*参数:表达式字符串

*返回值:计算结果

*/

int evaluateExpression(char *str)

{

int i = 0, result, numSub = 0, operSub = 0;

int tmp, len = strlen(str);

int *operand = (int*)malloc(sizeof(int)*len);

char *operat = (char*)malloc(sizeof(char)*len);

while(str[i] != '\0')

{

switch(str[i])

{

case '+':

while(operSub > 0 && operat[operSub-1] != '(')

{

printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);

operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);

printf("%d\n", operand[numSub-2]);

--numSub;

--operSub;

}

operat[operSub++] = '+';

break;

case '-':

while(operSub > 0 && operat[operSub-1] != '(')

{

printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);

operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);

printf("%d\n", operand[numSub-2]);

--numSub;

--operSub;

}

operat[operSub++] = '-';

break;

case '*':

if(str[i+1] >= '0' && str[i+1] <= '9')

{

tmp = 0;

while(str[i+1] >= '0' && str[i+1] <= '9')

{

tmp = tmp * 10 + str[i+1] - '0';

++i;

}

--i;

printf("%d * %d = ", operand[numSub-1], tmp);

operand[numSub-1] = cal(operand[numSub-1], '*', tmp);

printf("%d\n", operand[numSub-1]);

++i;

}

else

operat[operSub++] = '*';

break;

case '/':

if(str[i+1] >= '0' && str[i+1] <= '9')

{

tmp = 0;

while(str[i+1] >= '0' && str[i+1] <= '9')

{

tmp = tmp * 10 + str[i+1] - '0';

++i;

}

--i;

printf("%d / %d = ", operand[numSub-1], tmp);

operand[numSub-1] = cal(operand[numSub-1], '/', tmp);

printf("%d\n", operand[numSub-1]);

++i;

}

else

operat[operSub++] = '/';

break;

case '(':

operat[operSub++] = '(';

break;

case ')':

while(operat[operSub-1] != '(')

{

printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);

operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);

printf("%d\n", operand[numSub-2]);

--numSub;

--operSub;

}

--operSub;

break;

default:

tmp = 0;

while(str[i] >= '0' && str[i] <= '9')

{

tmp = tmp * 10 + str[i] - '0';

++i;

}

--i;

operand[numSub++] = tmp;

break;

}

++i;

}

while(numSub > 1 && operSub >= 1)

{

printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);

operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);

printf("%d\n", operand[numSub-2]);

--numSub;

--operSub;

}

result = operand[numSub-1];

free(operand);

free(operat);

return result;

}

int main()

{

char *str = "225/15-20+(4-3)*2";

int result;

printf("计算过程:\n");

result = evaluateExpression(str);

printf("计算结果:result = %d\n", result);

return 0;

}

计算过程:

225 / 15 = 15

15 - 20 = -5

4 - 3 = 1

1 * 2 = 2

-5 + 2 = -3

计算结果:result = -3

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值