zoukankan      html  css  js  c++  java
  • [HDU 1237] 简单计算器

    这题没啥好说的。就是烂模拟,我的是这样想:

    每次读三个数和两个运算符,如果第一个运算符是“+”或者“-”,后面的是“*”或者“/”那么就先将后面处理,之后将处理之后的值和还没用的数字再放回去,第一个运算符也同样放回。

    其他情况便都是先算前面之后把处理之后的值和还没用的数字放回,将第二个运算符同样放回。(可以想想为啥)

    一开始想用栈的但是用着用着发现太麻烦就换成双端队列了hhh有兴趣的可以去学一下,挺简单的。

    注意:1.因为是字符数字读入,要注意数字不止一位,这个样例跑一下就知道了

       2.题目要求精度较高需要用double,float会爆

     1 #include <bits/stdc++.h>
     2 #define maxn 200 + 10
     3 using namespace std;
     4 int main()
     5 {
     6     char s[maxn];
     7     while (gets(s) != NULL) {
     8         int len = strlen(s);
     9         if (len == 1 && s[0] == '0') break;
    10         deque<char> q1; deque<double> q2;
    11         for (int i = 0; i < len; i++)
    12         {
    13             int temp = 0;
    14             if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
    15                 q1.push_back(s[i]);
    16             else if (s[i] >= '0' && s[i] <= '9')
    17             {
    18                 while (s[i] != ' ' && i < len)
    19                     temp = temp * 10 + s[i++]-'0';
    20             //printf("%d\n", temp);
    21                 q2.push_back(temp);
    22             }
    23         }
    24         double temp;
    25         while (q2.size() > 2)
    26         {
    27             char oper1 = q1.front(); q1.pop_front();
    28             char oper2 = q1.front(); q1.pop_front();
    29             double num1 = q2.front(); q2.pop_front();
    30             double num2 = q2.front(); q2.pop_front();
    31             double num3 = q2.front(); q2.pop_front();
    32             if ((oper1 == '+' || oper1 == '-') && (oper2 == '*' || oper2 == '/'))
    33             {
    34                 if (oper2 == '*')
    35                 {
    36                     temp = num2 * num3;
    37                     //printf("%.2lf %.2lf %.2lf\n", num2, num3, temp);
    38                     q2.push_front(temp);
    39                     q2.push_front(num1);
    40                     q1.push_front(oper1);
    41 
    42                 }
    43                 else if (oper2 == '/')
    44                 {
    45                     temp = num2 / num3;
    46                     //printf("%.2lf %.2lf %.2lf\n", num2, num3, temp);
    47                     q2.push_front(temp);
    48                     q2.push_front(num1);
    49                     q1.push_front(oper1);
    50 
    51                 }
    52             }
    53             else
    54             {
    55                 if (oper1 == '+') temp = num1 + num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
    56                 else if (oper1 == '-') temp = num1 - num2,q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
    57                 else if (oper1 == '*') temp = num1 * num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
    58                 else temp = num1 / num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
    59             }
    60 
    61         }
    62         double ans;
    63         char oper = q1.front(); q1.pop_front();
    64         double num1 = q2.front(); q2.pop_front();
    65         double num2 = q2.front(); q2.pop_front();
    66         //printf("%.2lf\n", num1);printf("%.2lf\n", num2);
    67         if (oper == '+') ans = num1 + num2;
    68         else if (oper == '-') ans = num1 - num2;
    69         else if (oper == '*') ans = num1 * num2;
    70         else ans = num1 / num2;
    71         printf("%.2lf\n", ans);
    72 
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    Ubuntu系统---安装思维导图XMind
    Git---Ubuntu下的安装与使用
    YOLO---YOLOv3 with OpenCV安装与使用
    Keras---Virtualenv 下安装Keras (基于Tensorflow后端)
    Python---virtualenv + Tensorflow + 安装jupyter notebook
    Ubuntu系统---以virtualenv方式安装Tensorflow-CPU
    Caffe---Pycaffe 绘制loss和accuracy曲线
    Caffe---自带工具 绘制loss和accuracy曲线
    day91 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
    day 90 DjangoRestFramework学习二之序列化组件
  • 原文地址:https://www.cnblogs.com/Vikyanite/p/11370532.html
Copyright © 2011-2022 走看看