Preparing for a quick refresher --day1

Comma operator, to retain only the results of the first operator.

var = (3 * 4,4 * 5,5 * 6) 12 Results 

while (cin >> a, a! = EOF) This read a, the last part is the determination condition. In two usage seen on the book, not particularly common.

getline function: ordinary cin can only read non-space, with spaces that can be read, read one line

getline (cin, str)

#include<queue>
#include<string.h>
#include<string>
#include<iostream>
#include<stack>
#include<map>
#include<stdio.h>
using namespace std;
struct node
{
    Double NUM;
     char OP;
     BOOL In Flag; // to true representative of the operand, false behalf of the operator 
};
 String STR; // for storing input string 
Stack <Node> S; // operator stack 
Queue <Node> Q; / / postfix expression queue 
Map < char , int > OP; // for mapping priority. 
void Change () {
     Double NUM; // for storing current operand 
    Node TEMP; // for storing the current point, the auxiliary stack 
    for ( int I = 0 ; I < str.length ();)
    {
        if(str[i]>='0' && str[i]<='9')
        {
            temp.flag=true;
            temp.num=str[i++]-'0';
            while(i<str.length() && str[i]>='0' && str[i]<='9')
            {
                temp.num = temp.num * 10 + str [i] - ' 0 ' ;
                i++;
            }
            q.push(temp);
        }
        else
        {
            temp.flag=false;
            while(!s.empty() && op[str[i]]<=op[s.top().op])
            {
                q.push(s.top());
                s.pop();
            } // if the current top of the stack operation by the character and his priority equal to the current character is greater than the first portion str, continuous stack into a queue corresponding to the postfix expression. 
            = temp.op STR [I];
            s.push (TEMP); // presence of the operator after the operator to stay inside the stack are transferred to the first Node 
            I ++ ;
        }
    }
    while(!s.empty())
    {
        q.push(s.top());
        s.pop();
    }
}
double Cal()
{
    double temp1,temp2;
    node cur,temp;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        IF (== cur.flag to true ) // number of direct push, then the operator, two operators taken from the stack and push the result is then calculated. 
            s.push (CUR);
         the else
        {
            temp2=s.top().num;
            s.pop();
            temp1 = s.top () NUM;. // sequence number preceding operation is actually pushed onto the stack and an opposite 
            s.pop ();
            temp.flag=true;
            if(cur.op=='+')
                temp.num = temp1 + temp2; // note temp1 and temp2 are digital, temp for push is complete Node 
            the else  IF (cur.op == ' - ' )
                temp.num = temp1- temp2;
            else  if (cur.op == ' * ' )
                temp.num = temp1 * temp2;
            else 
                temp.num = temp1 / temp2;
            s.push(temp);
        }
    }
    return s.top () NUM;. // The end result on the stack, the stack is the unique number. 
}
 Int main ()
{
    by [ ' + ' ] = in [ " - " ] = 1 ;
    op['*']=op['/']=2;
    while(getline(cin,str), str!="0")
    {
        for(string::iterator it=str.end();it!=str.begin();it--)
        {
            if(*it==' ') str.erase(it);
        }
        while(!s.empty()) s.pop();
        Change();
        printf("%.2f\n",Cal());
    }
    return 0;
}
Infix suffix subscript turn COMPUTATIONAL

 

Guess you like

Origin www.cnblogs.com/tingxilin/p/12312070.html