zoukankan      html  css  js  c++  java
  • 简单计算器

    这道题初看感觉好简单,题意也很好理解,但是等到我真的去看的时候,就觉得还是有难度的,最开始想用数组做,一个个录入然后直接运算,但是这里还有乘除法,所以我们不能直接运算,但是我们可以用两个数组,一个录入,一个计算,只是多次循环不知道会不会超时,所以我最后还是借鉴了大神的栈的方法。这里面是边算边存数,使用两个栈,一个存数一个存符号,然后通过if这个选择计算,详情可以见代码

    #include<stdio.h>
    #include<string.h>
    #include<stack>
    using namespace std;
    int main()
    {
       int i;
       double a,b;
       char s[250],c;
       while(gets(s),strcmp(s,"0")!=0)
       {
           stack<char>s1;
           stack<double>s2;
           for(i=0;s[i];i++)
           {
               if(s[i]>='0'&&s[i]<='9')
               {
                   a=0;
                   while(s[i]>='0'&&s[i]<='9')
                   {
                       a=a*10+s[i]-'0';
                       i++;
                   }
                   i--;
                   s2.push(a);
               }
               else if(s[i]=='-'||s[i]=='+')
               {
                   if(!s1.empty())
                   {
                       c=s1.top();
                       s1.pop();
                       a=s2.top();
                       s2.pop();
                       b=s2.top();
                       s2.pop();
                       if(c=='+')
                           a+=b;
                       else
                           a=b-a;
                       s2.push(a);
                       s1.push(s[i]);
                   }
                   else
                       s1.push(s[i]);
               }
               else if(s[i]=='/')//这里就是先计算,再存数,首先找到后面的数的值,然后通过top函数引出前面一个数,然后计算,这个时候pop掉栈顶,push这个新算完的数字,存进去,这个时候就算是存完了
               {
                   b=0;
                   i+=2;
                   while(s[i]>='0'&&s[i]<='9')
                   {
                       b=b*10+s[i]-'0';
                       i++;
                   }
                   i--;
                   a=s2.top();
                   s2.pop();
                   a=a/b;
                   s2.push(a);
               }
               else if(s[i]=='*')
               {
                   b=0;
                   i+=2;
                   while(s[i]>='0'&&s[i]<='9')
                   {
                       b=b*10+s[i]-'0';
                       i++;
                   }
                   i--;
                   a=s2.top();
                   s2.pop();
                   a=a*b;
                   s2.push(a);
               }
           }
           while(!s1.empty())
           {
               c=s1.top();
               s1.pop();
               a=s2.top();
               s2.pop();
               b=s2.top();
               s2.pop();
               if(c=='+')
                   a+=b;
               else
                   a=b-a;
               s2.push(a);
           }
           printf("%.2f
    ",s2.top());
       }
       return 0;
    }
    

      

  • 相关阅读:
    (转)【web前端培训之前后端的配合(中)】继续昨日的故事
    ural(Timus) 1136. Parliament
    scau Josephus Problem
    ACMICPC Live Archive 6204 Poker End Games
    uva 10391 Compound Words
    ACMICPC Live Archive 3222 Joke with Turtles
    uva 10132 File Fragmentation
    uva 270 Lining Up
    【转】各种字符串哈希函数比较
    uva 10905 Children's Game
  • 原文地址:https://www.cnblogs.com/yintoki/p/5676637.html
Copyright © 2011-2022 走看看