• HDU 1237 简单计算器


    简单计算器

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 12832    Accepted Submission(s): 4222


    Problem Description
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
     
    Input
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
     
    Output
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
     
    Sample Input
    1 + 2
    4 + 2 * 5 - 7 / 11
    0
     
    Sample Output
    3.00
    13.36
     

    写得丑点就过不到呀~

    反正思路就是见到乘除号就马上把它搞定~再弄加减号这样。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    const int N = 3010;
    const int inf = 1e7+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    
    char str[N];
    int top1,top2,pos,len;
    
    bool is_digit( char s ) { if( s < '0' || s > '9' ) return false ; return true ; }
    
    double cal( double a , char s , double b ) {
        if( s == '+' ) return a+b ;
        else if( s=='-') return a-b;
        else if( s=='*') return a*b ;
        else if( s=='/') return a / b ;
    }
    
    double get(){
        double a , b ; char opp;
        a = 0 ; while( pos < len && is_digit( str[pos] ) ) a = a * 10 + ( str[pos] - '0' ) , pos++; pos++;
        while( pos < len && ( str[pos] == '*' || str[pos] == '/' ) ) {
            opp = str[pos] ; pos += 2 ;
            b = 0 ; while( pos < len && is_digit( str[pos] ) ) b = b * 10 + ( str[pos] - '0' ) , pos++; pos++;
            a = cal( a , opp , b );
        }
        return a ;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(false);
    
        while( gets(str) ) {
            if( str[0] == '0' && strlen(str) == 1 ) break ;
            double a , b  ; char opp ;
            pos = 0 ; len = strlen(str) ;
            a = get();
            while( pos < len ) {
                opp = str[pos] ; pos += 2 ;
                b = get() ;
                a = cal( a , opp , b );
            }
            printf("%.2lf
    ",a );
        }
        return 0;
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    Oracle11g客户端安装记要202011+Navicat Premium 连接Oracle 数据库
    设计模式之 ==> 观察者计模式
    设计模式之 ==> 责任链计模式
    一个.NET Core下的开源插件框架
    JavaScript判断文件是否为UTF-8编码
    Postgresql 设置字段搜索支持中文排序
    springboot docker 显示中文乱码问题
    linux 更新系统时间
    怎么让网易云音乐等音乐软件的歌词在MacBook Pro的 touch bar(触摸栏)上显示
    数仓:解读 NameNode 的 edits 和 fsimage 文件内容
  • 原文地址:https://www.cnblogs.com/hlmark/p/4109613.html
Copyright © 2020-2023  润新知