题目1019:简单计算器(栈的使用)

Posted 伊甸一点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目1019:简单计算器(栈的使用)相关的知识,希望对你有一定的参考价值。

题目链接:http://ac.jobdu.com/problem.php?pid=1019

 

题目具体分析见:https://github.com/zpfbuaa/JobduInCPlusPlus

 

参考代码:

//
//  1019 简单计算器.cpp
//  oj
//
//  Created by PengFei_Zheng on 04/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stack>
 
#define run ;
//#define debug ;
 
using namespace std;
int num1;
double temp;
 
stack<double> myStack;
 
int main(){
 
#ifdef debug
    while(scanf("%d ",&num1)&&num1!=0){
        cout<<num1<<" ";
        char a,b;
        int num;
        while(scanf("%c %d%c",&a,&num,&b)!=EOF){
            if(b!= ){
                cout<<a<<" "<<num<<endl;
                break;
            }
            else{
                cout<<a<<" "<<num<<b;
            }
        }
    }
#endif
     
#ifdef run
    char space0;
    while(scanf("%d%c",&num1,&space0)&&num1!=0&&space0== ){//ignore the space and using space0!=‘ ‘ and num1==0 the jump out of loop
        while(!myStack.empty()){
            myStack.pop();
        }
        myStack.push(num1);
//        cout<<"myStack push: "<<num1<<endl;
        char op,space1,space2;
        int num2;
         
        while(scanf("%c%c%d%c",&op,&space1,&num2,&space2)!=EOF){//ignore the space and using space2 to jump out of this calculation
            if(op==+){
//                cout<<"myStack push: "<<num2<<endl;
                myStack.push(num2);
            }
            else if(op==-){
//                cout<<"myStack push: "<<0-num2<<endl;
                myStack.push(0-num2);
            }
            else if(op==*){
                temp = myStack.top();
                myStack.pop();
//                cout<<"myStack push: "<<temp*num2<<endl;
                myStack.push(temp*num2);
            }
            else if(op==/){
                temp = myStack.top();
                myStack.pop();
//                cout<<"myStack push: "<<temp/num2<<endl;
                myStack.push(temp/num2);
            }
            if(space2!= )//jump out of loop
                break;
        }
        while(!myStack.empty()){
            if(myStack.size()==1){
                printf("%.2lf\n",myStack.top());
                break;
            }
            double x = myStack.top();
            myStack.pop();
            double y = myStack.top();
            myStack.pop();
            myStack.push(x+y);
        }
    }
#endif
     
}
/**************************************************************
    Problem: 1019
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1524 kb
****************************************************************/

 

以上是关于题目1019:简单计算器(栈的使用)的主要内容,如果未能解决你的问题,请参考以下文章

题目1019:简单计算器-------注意此题的输入中空格和字符和数字的关系,该用数据结构的时候就得用

1019.简单计算器

题目1101:计算表达式(栈的使用)

20165333四则运算阶段性总结

牛客练习——吐泡泡(简单的栈的运用题:别被多组输入恶心到了)

Day8 栈的使用--计算面积 接雨水