一道数学运算题目求牛人解答

kolnick 2011-11-18 09:23:52
String value = "6+-*/2+3-4*2";

要求先乘除后加减
如果遇到多个运算符取最后一个运算符进行两个数的计算

看那位大牛会哦
...全文
207 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
尘缘udbwcso 2011-11-20
  • 打赏
  • 举报
回复

//表达式求值
#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

输入的表达式要求是正确的

copy_copy 2011-11-20
  • 打赏
  • 举报
回复
顶udbwcso
wuyingjielao2 2011-11-19
  • 打赏
  • 举报
回复
刚做完编译原理的语法分析 不过是用C写的 你看看编译原理关于 表达式 函数内容描述你懂了
元心 2011-11-19
  • 打赏
  • 举报
回复
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()));
pl3121605999 2011-11-18
  • 打赏
  • 举报
回复
百度搜 后缀表达式
kolnick 2011-11-18
  • 打赏
  • 举报
回复
谁能贴个代码!!
lliiqiang 2011-11-18
  • 打赏
  • 举报
回复
公式解析器Jep
热带鱼2020 2011-11-18
  • 打赏
  • 举报
回复
飘过,就不再这种 小数学题上耽误工夫了, 开发上用不到根本就
jc8futao 2011-11-18
  • 打赏
  • 举报
回复
这是编译原理的内容,算数符优先算法,回去看看书。我说说思路。首先要硬编码一个算数符优先表,还要构造两个栈,一个是操作数栈,一个是操作符栈。然后遍历字符串,发现数字,将数字放入操作数栈,然后发现操作符,则将操作符和操作符栈顶进行比较。如果新的算数符优先级大,则进行运算,如果优先级小,则入栈,继续遍历。大概就是这个思路。具体的还是看书吧,呵呵。
我嘞个去 2011-11-18
  • 打赏
  • 举报
回复
-2?
righthook8 2011-11-18
  • 打赏
  • 举报
回复
第一步:化简计算式,化遇到多个运算符时取最后一个,得到一个计算字符串
第二步:从左往右扫比较相邻两个运算符的优先级计算,数据结构用栈式存储

牛人可以两步一起搞!!
diqye2011 2011-11-18
  • 打赏
  • 举报
回复
以前做过 现在忘记了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧