一道数学运算题目求牛人解答 String value = "6+-*/2+3-4*2";要求先乘除后加减 如果遇到多个运算符取最后一个运算符进行两个数的计算 看那位大牛会哦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String value = "6+-*/2+3-4*2"; StringBuilder afterValue = new StringBuilder(); char preOperator = ' '; for ( int i = 0; i < value.length(); i ++){ char cur = value.charAt(i); if(Character.isDigit(cur)){ if(preOperator != ' '){ afterValue.append(preOperator); } afterValue.append(cur); }else{ preOperator = cur; } } ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName("javascript"); System.out.println(afterValue+" "+se.eval(afterValue.toString())); //表达式求值#include <stdio.h>#include <malloc.h>#include <string.h>/**功能:根据运算符计算*参数: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 = 1515 - 20 = -54 - 3 = 11 * 2 = 2-5 + 2 = -3计算结果:result = -3输入的表达式要求是正确的 Java学习了一年,仍然不知... 关于模态对话框和非模态对话框的问题 做过报表大哥给点资料 关于urlclassloader的一个问题 如何设置Jtable中的行的背景颜色!!! 我刚开始用swing,帮帮忙吧!!! 关于<<thinking in Java>>(V2.0)中的Inner class的疑问 modBus传感器写入数据不准确 女程序员们的故事 请指教:javaw运行后内存释放的问题!!!! jini高手请进. 能不能用反射来获取局部变量 args[]有什么用处哟。
StringBuilder afterValue = new StringBuilder();
char preOperator = ' ';
for ( int i = 0; i < value.length(); i ++){
char cur = value.charAt(i);
if(Character.isDigit(cur)){
if(preOperator != ' '){
afterValue.append(preOperator);
}
afterValue.append(cur);
}else{
preOperator = cur;
}
}
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("javascript");
System.out.println(afterValue+" "+se.eval(afterValue.toString()));
//表达式求值
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
*功能:根据运算符计算
*参数: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输入的表达式要求是正确的