PAT基础编程题目-7-12 两个数的简单计算器
PAT基础编程题目-7-12 两个数的简单计算器
题目详情
解答
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");
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!