1 solutions

  • 0
    @ 2025-11-5 19:12:13

    C :

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double a, b, s;
        char c;
        int Flag;
        while(scanf("%lf %c %lf", &a, &c, &b)!=EOF)
        {
            Flag = 0;
            switch(c)
            {
                case '+':
                {
                    s = a + b;
                    break;
                }
                case '-':
                {
                    s = a - b;
                    break;
                }
                case '*':
                {
                    s = a * b;
                    break;
                }
                case '/':
                {
                    if(fabs(b) <= 1e-7)
                    {
                        Flag= 1;
                    }
                    else
                    {
                        s = a / b;
                        break;
                    }
                }
            }
            if(!Flag)
            {
                printf("%.2f\n", s);
            }
            else
            {
                printf("Divide by zero.\n");
            }
        }
        return 0;
    }
    
    

    C++ :

    #include <iomanip>
    #include <iostream>
    using namespace std;
    int main()
    {
        float a,b;
        char t;
        cout << setiosflags(ios::fixed)<< setiosflags(ios::left)<<setprecision(2);
        while(cin >> a >> t >> b)
        {
                  switch(t)
                  {
                           case '+':cout << a+b;break;
                           case '-':cout << a-b;break;
                           case '*':cout << a*b;break;
                           case '/':if(b==0) cout << "Divide by zero."; else cout << a/b;break;
                  }
                  cout << endl;
        }
        return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    //import java.io.*;
    public class Main {
    	public static void main(String[] args) {
    		Scanner in=new Scanner(System.in);
    		Float a,b;
    		char c;
    		while(in.hasNext()){
    			a=in.nextFloat();
    		    c=in.next().charAt(0);
    			b=in.nextFloat();
    			if(c=='+'){
    				System.out.println(new java.text.DecimalFormat("0.00").format(a+b));
    			}
    			if(c=='-'){
    				System.out.println(new java.text.DecimalFormat("0.00").format(a-b));
    			}
    			if(c=='*')
    			{
    				//System.out.println(a*b);
    				System.out.println(new java.text.DecimalFormat("0.00").format(a*b));
    			}
    			if(c=='/'){
    				if(b==0){
    					System.out.println("Divide by zero.");
    				}
    				else {
    					System.out.println(new java.text.DecimalFormat("0.00").format(a/b));
    				}
    			}
    			}
    			
    		}
    		
    	}
    
    
    
    • 1

    Information

    ID
    19210
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By