PAT基础编程题目-7-12 两个数的简单计算器

PAT基础编程题目-7-12 两个数的简单计算器

题目详情

PAT基础编程题目-7-12 两个数的简单计算器_第1张图片

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

解答

C语言版

#include
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;
}

PAT基础编程题目-7-12 两个数的简单计算器_第2张图片

C++版

#include
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;
}

PAT基础编程题目-7-12 两个数的简单计算器_第3张图片

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");

	}

}

PAT基础编程题目-7-12 两个数的简单计算器_第4张图片

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!

你可能感兴趣的:(C,C++,Java,PAT,基础编程题)