Topic 1019: Simple Calculator

Source: Internet
Author: User

Topic 1019: Simple Calculator

time limit:1 seconds

Memory limit:32 MB

Special question: No

Title Description:
reads a non-negative integer that contains only a +,-, *,/, evaluates the expression, and computes the value of the expression.
Input:
The
test input contains several test cases, one row per test case, and no more than 200 characters per line, separated by a space between integers and operators. There is no illegal expression. When only 0 o'clock input is completed in a row, the corresponding result is not output.
Output:
Output 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.
Sample input:
1 + 24 + 2 * 5-7/110
Sample output:
3.0013.36
#include <iostream>#include<stdio.h>#include<stack>using namespacestd;Charstr[ -];intmat[][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;//operator stacks, saving operator numbersstack<Double>inch;voidGetop (BOOL&reto,int&AMP;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;//Skip the operator and the space after the operator        return; } RETN=0;//returns the result as a number     for(; str[i]!=' '&&str[i]!=0; i++) {Retn*=Ten; RETN+=str[i]-'0'; }    if(str[i]==' ') I++; return ;}intMain () { while(gets (str)) {if(str[0]=='0'&&str[1]==0)        {             Break; }        BOOLRetop; intRetnum;//define the reference variables that the function needs to use        intidx=0;//string subscript         while(!op.empty ()) Op.pop ();//symbols         while(!inch. empty ())inch. Pop ();//Digital         while(true) {getop (RETOP,RETNUM,IDX); if(retop==false)            {                inch. Push (Double) retnum);//Press it into the digital stack            }            Else            {                Doubletmp; if(Op.empty () = =true|| Mat[retnum][op.top ()]==1) {Op.push (retnum); }                Else                {                     while(Mat[retnum][op.top ()]==0)                    {                        intRet=op.top ();//save symbol Stack top elementOp.pop (); Doubleb=inch. Top (); inch. Pop (); DoubleA=inch. Top (); inch. Pop (); if(ret==1) tmp=a+b; Else if(ret==2) tmp=a-b; Else if(ret==3) tmp=a*C; Elsetmp=a/b; inch. push (TMP);//press the number onto the stack} op.push (Retnum); }            }            if(op.size () = =2&&op.top () = =0) Break; } printf ("%.2f\n",inch. Top ()); }    return 0;}/************************************************************** problem:1019 User:zhuoyuezai language:c++ result:accepted time:0 Ms memory:1524 kb****************************************************************/

Topic 1019: Simple Calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.