C/C++经典程序训练3---模拟计算器

来源:互联网 发布:js取消onblur事件 编辑:程序博客网 时间:2024/04/28 14:55

C/C++经典程序训练3---模拟计算器

Time Limit: 1000MS Memory Limit: 8192KB

Problem Description

简单计算器模拟:输入两个整数和一个运算符,输出运算结果。

Input

第一行输入两个整数,用空格分开;
第二行输入一个运算符(+、-、*、/)。
所有运算均为整数运算,保证除数不包含0。

Output

输出对两个数运算后的结果。

Example Input

30 50*

Example Output

1500

代码如下:

import java.util.*;class counter{private int x;private int y;counter(int a, int b){x = a;y = b;}int add(){return (x + y);}int sub(){return (x - y);}int mul(){return (x * y);}int div(){return (x / y);}}public class Main{public static void main(String args[]){Scanner input = new Scanner(System.in);int A = input.nextInt();int B = input.nextInt();char C = input.next().charAt(0);counter cou = new counter(A, B);switch(C){case '+' : System.out.println(cou.add()); break;case '-' : System.out.println(cou.sub()); break;case '*' : System.out.println(cou.mul()); break;case '/' : System.out.println(cou.div()); break;}input.close();}}


0 0