PAT基礎編程題目-7-12 兩個數的簡單計算器

PAT基礎編程題目-7-12 兩個數的簡單計算器

題目詳情

在這裏插入圖片描述

題目地址:https://pintia.cn/problem-sets/14/problems/792

解答

C語言版

#include<stdio.h>
int main() {
	int operand1, operand2;
	char operatorC;
	scanf("%d %c %d", &operand1, &operatorC, &operand2);
	if (operatorC == '+')
		printf("%d", operand1 + operand2);
	else if (operatorC == '-')
		printf("%d", operand1 - operand2);
	else if (operatorC == '*')
		printf("%d", operand1 * operand2);
	else if (operatorC == '/')
		printf("%d", operand1 / operand2);
	else if (operatorC == '%')
		printf("%d", operand1 % operand2);
	else
		printf("ERROR");
	return 0;
}

在這裏插入圖片描述

C++版

#include<iostream>
using namespace std;
int main() {
	int operand1, operand2;
	char operatorC;
	cin >> operand1 >> operatorC >> operand2;
	if (operatorC == '+')
		printf("%d", operand1 + operand2);
	else if (operatorC == '-')
		printf("%d", operand1 - operand2);
	else if (operatorC == '*')
		printf("%d", operand1 * operand2);
	else if (operatorC == '/')
		printf("%d", operand1 / operand2);
	else if (operatorC == '%')
		printf("%d", operand1 % operand2);
	else
		printf("ERROR");
	return 0;
}

在這裏插入圖片描述

Java版

import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		int operand1 = 0, operand2 = 0;
		char operatorC = 0;
		Scanner scanner = new Scanner(System.in);
		if (scanner.hasNext()) {
			operand1 = scanner.nextInt();
			operatorC = scanner.next().charAt(0);
			operand2 = scanner.nextInt();
		}
		scanner.close();
		if (operatorC == '+')
			System.out.println(operand1 + operand2);
		else if (operatorC == '-')
			System.out.println(operand1 - operand2);
		else if (operatorC == '*')
			System.out.println(operand1 * operand2);
		else if (operatorC == '/')
			System.out.println(operand1 / operand2);
		else if (operatorC == '%')
			System.out.println(operand1 % operand2);
		else
			System.out.println("ERROR");

	}

}

在這裏插入圖片描述

創作不易,喜歡的話加個關注點個贊,謝謝謝謝謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章