zoukankan      html  css  js  c++  java
  • hdu-1237简单计算器(栈的运用)

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

    简单的栈的运用。

    首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第一个运算符分别压

    如个自的栈,然后判取出两个栈头部的元素,判断符号,如果是乘除就用当前数值乘取出的数字(优先),然后将乘后的数压入栈,

    如果是加则将数和取出的数按原序入栈,如果减,就把新的数变负,将数和取出的数按原序入栈。

    最后栈中所有元素的和就是结果。

     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 namespace std;
    10 char a[300];
    11 char b[300];
    12 double c[300];
    13 int main(void)
    14 {
    15     int n,i,j,k,p,q,l;
    16     while(gets(a))
    17     {
    18         l=strlen(a);
    19         if(l==1&&a[0]=='0')
    20         {
    21             break;
    22         }
    23         double ss=1;
    24         double sum=0;
    25         int yy=0;
    26         for(i=l-1; i>=0; i--)//从后往前循环分离,这样取数比较容易。
    27         {
    28             if(a[i]<='9'&&a[i]>='0')
    29             {
    30                 sum+=ss*(a[i]-'0');
    31                 ss*=10;
    32             }
    33             else if(a[i]==' ')
    34             {
    35                 continue;
    36             }
    37             else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/')
    38             {
    39                 c[yy++]=sum;
    40                 sum=0;
    41                 ss=1;
    42                 b[yy++]=a[i];
    43             }
    44         }
    45         c[yy]=sum;
    46         stack<double>que;//数栈
    47         stack<char>que1;//字符栈
    48         if(yy==0)//特判就一个元素直接输出
    49         {
    50             printf("%.2f",c[0]);
    51         }
    52         else
    53         {
    54             que.push(c[yy]);
    55             que1.push(b[yy-1]);
    56             for(i=yy-2; i>=0; i-=2)
    57             {
    58                 char cc=que1.top();//栈顶元素出栈
    59                 que1.pop();
    60                 double m=que.top();//栈顶元素出栈
    61                 que.pop();
    62                 if(cc=='*')//判断类型
    63                 {
    64                     m*=c[i];
    65                     que.push(m);
    66                 }
    67                 else if(cc=='/')
    68                 {
    69                     m=1.0*m/c[i];
    70                     que.push(m);
    71                 }
    72                 else if(cc=='+')
    73                 {
    74                     que.push(m);//按原序入栈
    75                     que.push(c[i]);
    76                 }
    77                 else if(cc=='-')
    78                 {
    79                     que.push(m);//按原序入栈
    80                     que.push(0-c[i]);
    81                 }
    82                 que1.push(b[i-1]);
    83 
    84             }
    85             double pp=0;
    86             while(!que.empty())//最后栈中元素的和
    87             {
    88                 pp+=que.top();
    89                 que.pop();
    90             }
    91             printf("%.2f",pp);
    92         }
    93         printf("
    ");
    94     }
    95     return 0;
    96 }
    油!油!you@
  • 相关阅读:
    小总结下iphone开发环境搭建过程!
    Raspberry Pi之旅 【序】
    linux虚拟机中配置samba的实现文件共享的方法
    编写高质量的代码1尽量使用StringBuilder
    ArcEngine开发点滴1
    开源Firebird .NET Provider V0.9.0.0发布
    .NET的Ant构建系统 NAnt 0.91发布!
    iis7.5 发布rest服务,put请求返回404错误 解决方法
    COM学习笔记(1)
    2004年南京美食地图 [转载]
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5011516.html
Copyright © 2011-2022 走看看