简易计算器

简易计算器

一 问题

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出

12178.21

二 问题分析

 这是很常见的问题,对应于数据结构的栈与队列。  首先应当将中序表达式,转化为后缀表达式。然后利用站计算。

三 具体代码

#include<iostream>
#include<string>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node
{
    double num;
    char op;
    bool flag;
};
string str;
stack<node> s;
queue<node> q;
map<char,int> op;
vector<double> result;
void change(); 
void cal();
void display_queue();
void display_stack();
int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    op['+']=op['-']=1;
    op['*']=op['/']=2;
    while(1)
    {	
        getline(cin,str);
        if(str=="0")
        {
            break; 
        } 
        for(string::iterator it=str.end(); it!=str.begin();it--)
        {
            if((*it)==' ')
            {
                str.erase(it);
            }
        }
        cout<<str<<endl;
        change();
        cal();
        while(!s.empty()) s.pop();
        while(!q.empty()) q.pop();
    }
    for(vector <double>::iterator it=result.begin(); it!=result.end(); it++)
    {
        cout<<*it<<endl;	
    }
    return 0;
}
void change()
{
    double num;
    node temp;
    string tp="";
    string::iterator it=str.begin();
    while(it!=str.end())
    {
        double num;
        if(*it>='0'&&*it<='9')
        {
            while(it!=str.end()&&*it>='0'&&*it<='9')
            {
                tp=tp+*it;
                it++;
            }
            sscanf(tp.data(),"%lf",&num);
            cout<<"num="<<num<<endl;
            temp.flag=true;
            temp.num=num;
            q.push(temp);
            tp="";	
        } 
        else
        {
        
            
            if(s.empty()||op[*it]>op[s.top().op])
            {
                temp.flag=false;
                temp.op=*it;
                s.push(temp);
            }else
            {
                while(!s.empty()&&op[*it]<=op[s.top().op])
                {
                    q.push(s.top());
                    s.pop(); 
                }
                temp.flag=false;
                temp.op=*it;
                s.push(temp);
            }
    
            it++;
        }	 
     }
    while(!s.empty())
    {
        q.push(s.top());
        s.pop(); 
    }
     display_queue();
     display_stack();
} 
void display_queue()
{
    queue<node> temp=q;
    cout<<"+++++++++++++q"<<endl;
    while(!temp.empty())
    {
        cout<<temp.front().flag<<endl;
        if(temp.front().flag)
        {
                cout<<temp.front().num<<endl;
        }else
        {
                cout<<temp.front().op<<endl;
        }
        cout<<"--------------"<<endl;
        temp.pop();
    }
}
void display_stack()
{
    stack<node> temp=s;
    cout<<"+++++++++++++s"<<endl;
    while(!temp.empty())
    {
        cout<<temp.top().flag<<endl;
        if(temp.top().flag)
        {
                cout<<temp.top().num<<endl;
        }else
        {
                cout<<temp.top().op<<endl;
        }
        cout<<"--------------"<<endl;
        temp.pop();
    }
}
void cal()
{
    while(!q.empty())
    {
        if(q.front().flag)
        {
            if(!q.empty())
            {
                s.push(q.front());
                q.pop();
            }
                                	
        }
        else
        {
            node temp;
            char ch=q.front().op;
            if(!q.empty())
            {
                q.pop();
            }
            double temp1,temp2;
            if(!s.empty())
            {
                temp1=s.top().num;
                s.pop();
            }
            if(!s.empty())
            {
                temp2=s.top().num;
                s.pop();;
            }
            temp.flag=true;
            if(ch=='+')
            {
                temp.num=temp2+temp1;
            }
            if(ch=='-')
            {
                temp.num=temp2-temp1;
            }
            if(ch=='*')
            {
                temp.num=temp2*temp1;
            }
            if(ch=='/')
            {
                temp.num=temp2/temp1;
            }
            s.push(temp);
        }
        display_queue();
        display_stack();
    }
    result.push_back(s.top().num);
}

四 运行结果

测试用例

 

int.txt
int.txt

 

结果

 

enter description here
enter description here

 

原文地址:https://www.cnblogs.com/Howbin/p/10584981.html