HJ50. 四则运算
描述
输入一个表达式(用字符串表示),求这个表达式的值。数据范围:表达式计算结果和过程中满足 ,字符串长度满足
输入描述
输入一个算术表达式
输出描述
得到计算结果
示例1
输入:
3+2*{1+2*[-4/(8-6)+7]}
输出:
25
C 解法, 执行用时: 1ms, 内存消耗: 256KB, 提交时间: 2020-07-06
#include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> typedef struct{ int *base; int *top; }stack; void push(stack *s,int e){ //ÈëÕ»²Ù×÷ *(s->top)=e; (s->top)++; } void pop(stack *s,int *e){ if((s->base)==(s->top)){ e=NULL; } else{ * e=* (--(s->top)); } } int gettop(stack *s){ return *((s->top)-1); } int isempty(stack *s){ return (abs((s->base)==(s->top))); //1:isempty 0:noempty } int ishigh(char c1,char c2){ //c2>c1? if(c1=='+'||c1=='-'){ if(c2=='+'||c2=='-'){ return 0; } else if(c2=='*'||c2=='/'){ return 1; } } if(c1=='*'||c1=='/'){ return 0; } if(c1=='('){ return 1; } } int calcpro(int a,int b,int ch){ switch(ch){ case '+':return(a+b); case '-':return(a-b); case '*':return(a*b); case '/':return(a/b); } } int main(){ stack nums,calcs; char str[101]={0}; char str_ch[101]={0}; while(scanf("%s",str_ch)!=EOF){ nums.base=(int*)malloc(100*sizeof(int)); calcs.base=(int*)malloc(100*sizeof(int)); nums.top=nums.base; calcs.top=calcs.base; int c1,c2,n1,n2; char *p; int len=strlen(str_ch); int i,j=0,temp=0,val=0; for(i=0;i<len;i++){ if((str_ch[i]=='[')||(str_ch[i]=='{')){ str_ch[i]='('; } if((str_ch[i]=='}')||(str_ch[i]==']')){ str_ch[i]=')'; } } if(str_ch[0]=='-'){ str[j++]='0'; str[j++]=str_ch[0]; } else{ str[j++]=str_ch[0]; } for(i=1;i<len;i++){ if((str_ch[i]=='-')&&(str_ch[i-1]=='(')){ str[j++]='0'; str[j++]=str_ch[i]; } else{ str[j++]=str_ch[i]; } } len=strlen(str); for(i=0;i<len;i++) { if(str[i]=='(') { push(&calcs,'('); continue; } if(str[i]==')') { while((c1=gettop(&calcs))!='(') { pop(&calcs,&c1); pop(&nums,&n2); pop(&nums,&n1); temp = calcpro(n1,n2,c1); push(&nums,temp); } pop(&calcs,&c1); continue; } if((str[i]=='+')||(str[i]=='-')||(str[i]=='*')||(str[i]=='/')) { if(isempty(&calcs)==1) { push(&calcs,str[i]); continue; } else { c1=gettop(&calcs); if(ishigh(c1,str[i])==1)//ÓÅÏȼ¶¸ßÈëÕ» { push(&calcs,str[i]); } else { pop(&calcs,&c1); pop(&nums,&n2); pop(&nums,&n1); temp = calcpro(n1,n2,c1); push(&nums,temp); i--;//ÖØÐÂÅÐ¶Ï } continue; } } if(isdigit(str[i])) { n1=strtoul(&str[i],&p,10); //strtoulº¯Êý´¦Àí1λÊýÒÔÉϵÄÇé¿ö push(&nums,n1); i=p-1-str; continue; } } // printf("\n%d %d\n",elemnum(&calcs),elemnum(&nums)); while(isempty(&calcs)!=1) { pop(&calcs,&c1); pop(&nums,&n2); pop(&nums,&n1); temp=calcpro(n1,n2,c1); push(&nums,temp); } pop(&nums,&val); printf("%d\n",val); } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 348KB, 提交时间: 2021-09-12
#include <stdio.h> #include <string.h> #include <ctype.h> int pos; int compute(char* data) { int len = strlen(data); int stack[1000]; int top = -1; int num = 0; char flag = '+'; while (pos < len) { if (data[pos] == '{' || data[pos] == '[' || data[pos] == '(') { pos++; num=compute(data); } while (pos < len && isdigit(data[pos])) { num = num*10 + data[pos] -'0'; pos++; } switch (flag) { case '+': stack[++top] = num; break; case '-': stack[++top] = -num; break; case '*': stack[top] *= num; break; case '/': stack[top] /= num; break; } num = 0; flag = data[pos]; if (data[pos] == '}' || data[pos] == ']'|| data[pos] == ')') { pos++; break; } pos++; } int res = 0; for (int i = 0; i <= top; i++) { res += stack[i]; } return res; } int main() { char data[1000]; while (scanf("%s", data) != EOF) { pos = 0; int res = compute(data); printf("%d\n", res); } return 0; }