计算器--可以计算合理表达是的计算器,包括+-×/()

来源:互联网 发布:protools mac破解版 编辑:程序博客网 时间:2024/04/29 16:47

参考了

1.王道的机试复试书


#include <stdio.h>#include <cstring>#include <stack>static int p[][7] = {          /*  $  +  -  *  %  (  )*/   /*$ */   { 0,-1,-1,-1,-1,-1,-2},   /*+*/    { 1, 1, 1,-1,-1,-1, 1},   /*-*/    { 1, 1, 1,-1,-1,-1, 1},   /***/    { 1, 1, 1, 1, 1,-1, 1},   /*%*/    { 1, 1, 1, 1, 1,-1, 1},   /*(*/    {-2,-1,-1,-1,-1,-1, 0},   /*)*/    { 1, 1, 1, 1, 1,-2, 1},    /* 0: =     */     /* 1: >     */     /*-1: <     */     /*-2: Error */     /*p[i][j]: i: top-stack, j = cur-opr*/};static std::stack<int> op;static std::stack<double> in;static char str[202];    void getOp(bool &retop, int &retnum, int &i){     while(str[i] == ' ' ) i++;      if((i==0 && op.empty()) || str[i] == '\0' || str[i] == '\n') {        //$        retop = true, retnum = 0;         return;     }     if('0' <= str[i] && str[i] <='9' ){         //number        retop = false;     }else{         //operator        retop = true;        if(str[i] == '+')  retnum = 1;        if(str[i] == '-')  retnum = 2;        if(str[i] == '*')  retnum = 3;        if(str[i] == '/')  retnum = 4;        if(str[i] == '(')  retnum = 5;        if(str[i] == ')')  retnum = 6;        i++;        return;      }       retnum = 0;    for(;'0' <= str[i] && str[i] <='9'; ++i){         retnum *= 10;        retnum += str[i] - '0';    }      return;}  int main(){ //    freopen("1237.in","r",stdin);    while( gets(str) && strcmp(str, "0") !=0 ){         while(!op.empty()) op.pop();        while(!in.empty()) in.pop();        int idx=0,retnum;        bool retop, error= false;        while(true){             getOp(retop, retnum, idx);            if(retop == false){                 in.push((double) retnum);            }else{                 if(op.empty() || p[op.top()][retnum] == -1){                     op.push(retnum);                  }else if(p[op.top()][retnum] == 1){                     double tmp;                    while(p[op.top()][retnum] == 1){                          int opr = op.top();                         op.pop();                         double b = in.top();                         in.pop();                         double a = in.top();                         in.pop();                         if(opr == 1) tmp = a+b;                          else if(opr == 2) tmp = a-b;                          else if(opr == 3) tmp = a*b;                          else if(opr == 4) tmp = a/b;                          in.push(tmp);                     }                    // ( <- )                      if(p[op.top()][retnum] == 0) op.pop();                    else op.push(retnum);                }else if(p[op.top()][retnum] == 0 ){                    // $ <- $                    op.pop();                }else{                   error = true;                   break;                 }                 }             if(op.size()== 0 && in.size() == 1 ) break;         }          if(error) printf("ERROR\n");        else{            printf("%.2f\n", in.top());         }    }      return 0;}  

java:

import java.util.Scanner;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class Main {public static void main(String[] args) {Scanner  sc = new Scanner(System.in);ScriptEngineManager mgr = new ScriptEngineManager();ScriptEngine engine = mgr.getEngineByName("JavaScript");while(sc.hasNext()){String expr = sc.next();try {System.out.println(engine.eval(expr));} catch (ScriptException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


0 0