编写一个简单的计算器 实现两个整型数的四则运算
时间: 2023-05-14 17:05:00 浏览: 234
好的,以下是一个简单的计算器实现两个整型数的四则运算的代码:
```python
def calculator(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
return num1 / num2
else:
return "Invalid operator"
num1 = int(input("请输入第一个整数:"))
num2 = int(input("请输入第二个整数:"))
operator = input("请输入运算符(+, -, *, /):")
result = calculator(num1, num2, operator)
print("结果为:", result)
```
希望能够帮到你!
相关问题
编写控制台程序,实现两个整型数的四则运算,由用户先选择使用四则运算的哪种运算,再输入参与运算两个整数
这个控制台程序可以通过使用switch语句实现。首先,程序会提示用户选择运算类型(加、减、乘、除)。当用户做出选择后,程序将要求用户输入两个整数。接下来,根据用户选择的运算类型,程序将对两个数字进行相应的运算,并输出结果。以下是一个可能的实现:
```
#include <iostream>
using namespace std;
int main() {
// 提示用户选择运算类型
cout << "请选择运算类型:" << endl;
cout << "1. 加法" << endl;
cout << "2. 减法" << endl;
cout << "3. 乘法" << endl;
cout << "4. 除法" << endl;
// 获取用户选择
int choice;
cin >> choice;
// 获取参与运算的两个整数
int num1, num2;
cout << "请输入两个整数:" << endl;
cin >> num1 >> num2;
// 根据用户选择进行运算并输出结果
switch (choice) {
case 1:
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case 2:
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
case 3:
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
case 4:
if (num2 == 0) {
cout << "除数不能为0!" << endl;
} else {
cout << num1 << " / " << num2 << " = " << (float) num1 / num2 << endl;
}
break;
default:
cout << "无效的选择!" << endl; break;
}
return 0;
}
```
--相关问题--:
编写一个计算器程序,要求完成以下功能:1.输入任意的两个整型数,经过选择,完成加减乘除运算,并输出运算结果;(必须完成)2.用函数实现上述程序中的加减乘除运算,使用参数带回结果) 。 3.可以继续输入另外的两个数进行计算,直至结束。
好的,我可以回答这个问题。以下是计算器程序的代码:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
return a / b;
}
int main() {
int num1, num2, choice, result;
char operator;
do {
printf("请输入两个整数:\n");
scanf("%d %d", &num1, &num2);
printf("请选择要进行的运算:\n");
printf("1. 加法\n");
printf("2. 减法\n");
printf("3. 乘法\n");
printf("4. 除法\n");
scanf("%d", &choice);
switch (choice) {
case 1:
result = add(num1, num2);
operator = '+';
break;
case 2:
result = subtract(num1, num2);
operator = '-';
break;
case 3:
result = multiply(num1, num2);
operator = '*';
break;
case 4:
result = divide(num1, num2);
operator = '/';
break;
default:
printf("无效的选择!\n");
continue;
}
printf("%d %c %d = %d\n", num1, operator, num2, result);
printf("是否继续计算?(1-是,-否)\n");
scanf("%d", &choice);
} while (choice == 1);
return ;
}
在这个程序中,我们定义了四个函数,分别用于加法、减法、乘法和除法运算。在主函数中,我们使用一个 do-while 循环来实现多次计算的功能。在每次循环中,我们先输入两个整数,然后让用户选择要进行的运算。根据用户的选择,我们调用相应的函数来计算结果,并输出运算结果。最后,我们询问用户是否继续计算,如果是,则继续循环,否则退出程序。
阅读全文
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)