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

	}

}

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

全部评论

相关推荐

牛客154160166号:9月底还给我发短信,好奇怪,我24届的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务