1. 程式人生 > >C/C++經典程式訓練3---模擬計算器(類)

C/C++經典程式訓練3---模擬計算器(類)

Problem Description

簡單計算器模擬:輸入兩個整數和一個運算子,輸出運算結果。

Input

第一行輸入兩個整數,用空格分開;
第二行輸入一個運算子(+、-、*、/)。
所有運算均為整數運算,保證除數不包含0。

Output

輸出對兩個數運算後的結果。

Sample Input

30 50
*

Sample Output

1500

import java.util.Scanner; 
class compute {
	int a, b;
	char c;
	public compute(int a, int b, char c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public int jisuan(){
		if(c=='+'){
			return a+b;
		}
		else if(c=='-'){
			return a-b;
		}
		else if(c=='*'){
			return a*b;
		}
		else if(c=='/'){
			return a/b;
		}
		else{
			return 0;
		}
	}
	
}
public class Main {  
  
    public static void main(String[] args) {    
        Scanner reader = new Scanner(System.in);    
        int a = reader.nextInt();    
        int b = reader.nextInt();
        
        String s = reader.nextLine();
        s = reader.nextLine();
        char c = s.charAt(0);
        compute rect = new compute(a,b,c);
        int sum = rect.jisuan();
        System.out.println(sum);  
    }       
  
}