加减乘除基本运算

hdu Balloon Comes! (1170)

Problem Description
The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very… easy problem.
Give you an operator (+,-,*, / –denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!

Input
Input contains multiple test cases. The first line of the input is a single integer T (0

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int t;
    cin>>t;
    getchar();
    while(t--)
    {
        char s;
        cin>>s;
        int a,b,c;
        cin>>a>>b;
        float d;
        if(s=='+')
          cout<<a+b;
        if(s=='-')
          cout<<a-b;
        if(s=='*')
          cout<<a*b;
        if(s=='/')
        {
          if(a%b==0)
            cout<<a/b;       //判断如果b能整除a,则不能输出小数
          if(a%b!=0)
          {
            d=a*1.0/b;
            printf("%.2f",d);   //否则,即不能整除,就要保留两位小数
          }
        }
        cout<<endl;
    }
    return 0;
}

你可能感兴趣的:(加减乘除基本运算)