hdu-1237 Simple Calculator (use of stacks)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1237

Simple stack usage.

First, the numbers and operators are separated, stored in two arrays, and then in the order of the original, the first number and the first operator are respectively pressed

such as a self-stack, and then sentenced to take out two stack head elements, judging the symbol, if it is multiplication the current value by the number taken out (priority), and then the number of times to press into the stack,

If it is plus, the number and the number taken out of the original sequence into the stack, if minus, the new number will be negative, the number and the number taken out of the original sequence into the stack.

All the elements in the last stack are the result.

1#include <stdio.h>2#include <algorithm>3#include <stdlib.h>4#include <string.h>5#include <math.h>6#include <iostream>7#include <stack>8#include <queue>9 using namespacestd;Ten Chara[ -]; One Charb[ -]; A Doublec[ -]; - intMainvoid) - { the     intn,i,j,k,p,q,l; -      while(Gets (a)) -     { -L=strlen (a); +         if(l==1&&a[0]=='0') -         { +              Break; A         } at         Doubless=1; -         Doublesum=0; -         intyy=0; -          for(i=l-1; i>=0; i--)//it is easier to take a number from a backward-forward loop.  -         { -             if(a[i]<='9'&&a[i]>='0') in             { -Sum+=ss* (a[i]-'0'); toss*=Ten; +             } -             Else if(a[i]==' ') the             { *                 Continue; $             }Panax Notoginseng             Else if(a[i]=='+'|| a[i]=='-'|| a[i]=='*'|| a[i]=='/') -             { thec[yy++]=sum; +sum=0; Ass=1; theb[yy++]=A[i]; +             } -         } $c[yy]=sum; $stack<Double>que;//Count Stacks -stack<Char>que1;//character Fu Yi -         if(yy==0)//It's a direct output of an element . the         { -printf"%.2f", c[0]);Wuyi         } the         Else -         { Wu Que.push (C[yy]); -Que1.push (b[yy-1]); About              for(i=yy-2; i>=0; i-=2) $             { -                 CharCc=que1.top ();//stack top element out stack - Que1.pop (); -                 DoubleM=que.top ();//stack top element out stack A Que.pop (); +                 if(cc=='*')//Judging Type the                 { -m*=C[i]; $ Que.push (m); the                 } the                 Else if(cc=='/') the                 { them=1.0*m/C[i]; - Que.push (m); in                 } the                 Else if(cc=='+') the                 { AboutQue.push (m);//Enter the stack by the original sequence the Que.push (C[i]); the                 } the                 Else if(cc=='-') +                 { -Que.push (m);//Enter the stack by the original sequence theQue.push (0-c[i]);Bayi                 } theQue1.push (b[i-1]); the  -             } -             Doublepp=0; the              while(!que.empty ())//the and of the elements in the last stack the             { thepp+=que.top (); the Que.pop (); -             } theprintf"%.2f", pp); the         } theprintf"\ n");94     } the     return 0; the}

hdu-1237 Simple Calculator (use of stacks)

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.