欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-07 15:15:23
...

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

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

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

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

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 两个数的简单计算器

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