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

62 篇文章 0 订阅
1 篇文章 0 订阅

参考了

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 block
				e.printStackTrace();
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值