hdu acm-step 1.2.7 Balloon Comes!

 

      这道题的题意:给出一个运算符和2个操作数,打印运算结果,需要注意的是如果结果不是整数,那么输出2位浮点数.

      代码如下:

      

#include <cstdio>

using namespace std;

namespace IO{

    const int M = 0xcf;

    int scan()
    {

        char ch;

        int sum = 0;

        int f = 0;
    
        while((ch = getchar()) != '\n' || f == 0)
        {

            if(ch >= '0' && ch <= '9')
            {

                sum = sum * 10 + (ch & M);    

                f = 1;

            }
            else if(ch == ' '&& f != 0)
            {            

                return sum;

            }

        }

        return sum;

    }

};

void calculate(int b,int a,char op)
{
    
    switch(op)
    {

        case '+': printf("%d\n",a+b);break;

        case '-': printf("%d\n",a-b);break;

        case '*': printf("%d\n",a*b);break;

        case '/': 
        {

            if(a % b == 0)

                printf("%d\n",a/b);

            else

                printf("%.2lf\n",(double)a/b);    

            break;

        }

    }
        

}

int main()
{

    int T;

    T = IO::scan();

    while(T--)
    {

        calculate(IO::scan(),IO::scan(),getchar());

    }

    return 0;

}

这是本人第一次写IO,遇到了一个比较坑的问题。

大家要注意,C/C++的函数参数是从右到左传入的。

因此输入的时候上述三个函数的调用顺序是getchar(),IO::scan,IO::scan。

posted @ 2017-08-16 15:19  mtl6906  阅读(89)  评论(0编辑  收藏  举报