四则运算
描述:
请实现如下接口
/* 功能:四则运算
* 输入:strExpression:字符串格式的算术表达式,如: "3+2*{1+2*[-4/(8-6)+7]}"
* 返回:算术表达式的计算结果
*/
public static int calculate(String strExpression)
{
/* 请实现*/
return 0;
}
约束:
pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。
pucExpression算术表达式的有效性由调用者保证;
知识点: 栈
题目来源: 内部整理
练习阶段: 中级
运行时间限制: 10Sec
内存限制: 128MByte
输入:
输入一个算术表达式
输出:
得到计算结果
样例输入:
3+2*{1+2*[-4/(8-6)+7]}
样例输出:
25
#include "stdio.h" #include "stdlib.h" #include "string.h" #define MAX_PATH 256 char stack_all[MAX_PATH] = {0}; char stack_symbol[MAX_PATH] = {0}; char stack_num[MAX_PATH] = {0}; int num_all=0; int num_symbol =0; int num_num = 0; int cal(int a,int b,char op) { switch(op) { case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': if (b==0) { return 0; } return a/b; default: return 0; } } //有效字符是0-9 int calculate(char *strExpression) { int length = strlen(strExpression); for (int i=length-1;i>=0;i--) { char t = strExpression[i]; if(t=='*' || t=='/' || t==')' || t==']' || t=='}') { stack_symbol[num_symbol] = t; num_symbol++; } else if (t=='(') { while(true) { if (stack_symbol[num_symbol-1]==')') { num_symbol--; break; } stack_all[num_all]=stack_symbol[num_symbol-1]; num_all++; num_symbol--; } } else if (t=='[') { while(true) { if (stack_symbol[num_symbol-1]==']') { num_symbol--; break; } stack_all[num_all]=stack_symbol[num_symbol-1]; num_all++; num_symbol--; } } else if (t=='{') { while(true) { if (stack_symbol[num_symbol-1]=='}') { num_symbol--; break; } stack_all[num_all]=stack_symbol[num_symbol-1]; num_all++; num_symbol--; } } else if(t=='+' || t=='-') { while((stack_symbol[num_symbol-1]=='*' || stack_symbol[num_symbol-1]=='/') && num_symbol!=0 ) { stack_all[num_all]=stack_symbol[num_symbol-1]; num_all++; num_symbol--; } stack_symbol[num_symbol] = t; num_symbol++; } else { stack_all[num_all] = t; num_all++; } } for (int i=num_symbol-1;i>=0;i--) { stack_all[num_all]=stack_symbol[i]; num_all++; } for (int i=0;i<num_all;i++) { char t = stack_all[i]; if (t>='0'&&t<='9') { stack_num[num_num]=t-'0'; num_num++; } else { stack_num[num_num-2] = cal(stack_num[num_num-1],stack_num[num_num-2],t); num_num--; } } return (int)stack_num[0]; } void init(char *buffer,char *buffer2) { int length = strlen(buffer); int k = 0; for (int i=0;i<length;i++) { char t = buffer[i]; if (t=='+'||t=='-') { if(i>=1 && (buffer[i-1]=='(' || buffer[i-1]=='[' || buffer[i-1]=='{')) { //可以不使用括号 // buffer2[k]='0'; // k++; // buffer2[k]=buffer[i]; // k++; buffer2[k]='('; k++; buffer2[k]='0'; k++; buffer2[k]=buffer[i]; k++; buffer2[k]=buffer[i+1]; i++; k++; buffer2[k]=')'; k++; } else { buffer2[k]=buffer[i]; k++; } } else { buffer2[k]=buffer[i]; k++; } } } /* 2 3 4 6 8 10 特比注意:负数需要补0为0-正数。 */ int main() { int m=0,n=0; char buffer[MAX_PATH] = {0}; gets(buffer); // char buffer[MAX_PATH] = "3+2*{1+2*[-4/(8-6)+7]}"; char buffer2[MAX_PATH] = {0}; init(buffer,buffer2); // printf(buffer2); // printf("\n"); int ret = calculate(buffer2); // printf(stack_all); // printf("\n"); printf("%d",ret); return 0; }
原文地址:http://blog.csdn.net/hepfei90/article/details/44079373