题目1019:简单计算器(两种方法解决,一种超简单,一种超复杂)都利用了栈

题目1019:简单计算器

时间限制:1 秒

内存限制:32 兆

特殊判题:

题目描述:
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出:
3.00
13.36
方法1简单
#include <iostream>
#include<stdio.h>
#include<stack>
using namespace std;
int main()
{
    int a;
    char b,c;
    double d;
    while(scanf("%d ",&a)&&a!=0)//读取格式很关键,注意%d后面有空格,用于读取空格
    {
        stack<double> s;
        s.push(a);
        while(scanf("%c %d%c",&b,&a,&c)!=EOF)//%c %d%c分别读取 符号空格数字及数字后的空格,如果数字后面没有空格,说明表达式结束
        {
            if(b=='+')
            {
                s.push(a);
            }
            else if(b=='-')
            {
                s.push(-1.0*a);
            }
            else if(b=='*')
            {
                d=s.top()*a;
                s.pop();
                s.push(d);
            }
            else if(b=='/')
            {
                d=s.top()/a;
                s.pop();
                s.push(d);
            }

            if(c!=' ')//数字后无空格,不再录入,退出while循环
            {
                break;
            }
        }
        while(!s.empty())
        {
            if(s.size()==1)
            {
                printf("%.2lf\n",s.top());
                break;
            }
            double d1=s.top();
            s.pop();
            double d2=s.top();
            s.pop();
            s.push(d1+d2);
        }
    }
    return 0;
}

方法2复杂

#include <iostream>
#include<stdio.h>
#include<stack>
using namespace std;
char str[220];
int mat[][5]=
{
    {1,0,0,0,0},
    {1,0,0,0,0},//+
    {1,0,0,0,0},//-
    {1,1,1,0,0},//*
    {1,1,1,0,0}// /
};
stack<int> op;//运算符栈,保存运算符编号
stack<double> in;
void getOp(bool &reto,int &retn,int &i)
{
    if(i==0&&op.empty()==true)
    {
        reto=true;
        retn=0;
        return ;
    }
    if(str[i]==0)
    {
        reto=true;
        retn=0;
        return ;
    }
    if(str[i]>='0'&&str[i]<='9')
    {
        reto =false;
    }
    else
    {
        reto=true;
        if(str[i]=='+')
        {
            retn=1;
        }
        else if(str[i]=='-')
        {
            retn =2;
        }
        else if(str[i]=='*')
        {
            retn =3;
        }
        else if(str[i]=='/')
        {
            retn =4;
        }
        i+=2;//跳过该运算符以及运算符后的空格
        return;
    }
    retn=0;// 返回结果为数字
    for(; str[i]!=' '&&str[i]!=0; i++)
    {
        retn*=10;
        retn+=str[i]-'0';
    }
    if(str[i]==' ')
        i++;
    return ;
}
int main()
{
    while(gets(str))
    {
        if(str[0]=='0'&&str[1]==0)
        {
            break;
        }
        bool retop;
        int retnum;//定义函数需要使用的引用变量
        int idx=0;//字符串下标
        while(!op.empty()) op.pop();//符号
        while(!in.empty()) in.pop();//数字
        while(true)
        {
            getOp(retop,retnum,idx);
            if(retop==false)
            {
                in.push((double)retnum);//将其压入数字堆栈
            }
            else
            {
                double tmp;
                if(op.empty()==true||mat[retnum][op.top()]==1)
                {
                    op.push(retnum);
                }
                else
                {
                    while(mat[retnum][op.top()]==0)
                    {
                        int ret=op.top();//保存符号栈顶元素
                        op.pop();
                        double b=in.top();
                        in.pop();
                        double a=in.top();
                        in.pop();
                        if(ret==1) tmp=a+b;
                        else if(ret==2) tmp=a-b;
                        else if(ret==3) tmp=a*b;
                        else tmp=a/b;
                        in.push(tmp);//将数字压入堆栈
                    }
                    op.push(retnum);
                }

            }
            if(op.size()==2&&op.top()==0) break;
        }
        printf("%.2f\n",in.top());
    }
    return 0;
}

/**************************************************************
    Problem: 1019
    User: zhuoyuezai
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1524 kb
****************************************************************/

 

posted @ 2016-07-23 23:14  多思考&&多动手  阅读(399)  评论(0编辑  收藏  举报