两个整数四则运算cpp
时间: 2023-12-27 17:05:13 浏览: 48
下面是两个整数四则运算的 C++ 代码示例:
```cpp
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "请输入两个整数:" << endl;
cin >> num1 >> num2;
cout << "加法结果:" << num1 + num2 << endl;
cout << "减法结果:" << num1 - num2 << endl;
cout << "乘法结果:" << num1 * num2 << endl;
if (num2 == 0) {
cout << "除数不能为0!" << endl;
} else {
cout << "除法结果:" << num1 / num2 << endl;
}
return 0;
}
```
在这个程序中,我们首先接受用户输入的两个整数,然后进行加、减、乘、除四种运算,并将结果输出到屏幕上。需要注意的是,如果除数为0,则不能进行除法运算,程序会提示用户除数不能为0。
相关问题
大数的四则运算c++
以下是C++实现大数的四则运算的两个例子:
1. 大数相加
```cpp
bign add(bign a, bign b) {
bign c;
int carry = 0; // 进位
for (int i = 0; i < a.len || i < b.len; i++) {
int temp = a.d[i] + b.d[i] + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
if (carry != 0) {
c.d[c.len++] = carry; // 如果最后一位的进位不为0,直接赋给结果的最高位
}
return c;
}
```
2. 大数乘以一个整数
```cpp
bign multi(bign a, int b) {
bign c;
int carry = 0;
for (int i = 0; i < a.len; i++) {
int temp = a.d[i] * b + carry;
c.d[c.len++] = temp % 10;
carry = temp / 10;
}
while (carry != 0) {
c.d[c.len++] = carry % 10;
carry /= 10;
}
return c;
}
```
PTA求解简单的四则运算表达式
PTA(Programming Test and Assessment)平台上有很多简单的四则运算表达式题目,可以练习基本的算术运算和表达式求值。下面是一个例子:
题目描述:
给定一个只包含加、减、乘、除四种基本运算的表达式,求表达式的值。
输入格式:
第一行包含一个整数 T,表示共有 T 组测试数据。
每组数据占一行,包含一个长度不超过 50 的只包含数字与加、减、乘、除四种运算符的表达式。
输出格式:
对于每组数据,输出表达式的值,保留两位小数。
样例输入:
2
1+2*3-4/5
3+5*8-6/2
样例输出:
6.20
43.00
代码示例(C++):
```cpp
#include <iostream>
#include <stack>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--)
{
string s;
cin >> s;
stack<double> nums;
stack<char> ops;
for (int i = 0; i < s.size(); i++)
{
if (isdigit(s[i]))
{
double num = s[i] - '0';
while (i + 1 < s.size() && isdigit(s[i + 1]))
{
num = num * 10 + s[i + 1] - '0';
i++;
}
nums.push(num);
}
else if (s[i] == '(')
{
ops.push('(');
}
else if (s[i] == ')')
{
while (ops.top() != '(')
{
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if (op == '+')
nums.push(a + b);
else if (op == '-')
nums.push(a - b);
else if (op == '*')
nums.push(a * b);
else if (op == '/')
nums.push(a / b);
}
ops.pop();
}
else if (s[i] == '+' || s[i] == '-')
{
while (!ops.empty() && (ops.top() == '+' || ops.top() == '-' || ops.top() == '*' || ops.top() == '/'))
{
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if (op == '+')
nums.push(a + b);
else if (op == '-')
nums.push(a - b);
else if (op == '*')
nums.push(a * b);
else if (op == '/')
nums.push(a / b);
}
ops.push(s[i]);
}
else if (s[i] == '*' || s[i] == '/')
{
while (!ops.empty() && (ops.top() == '*' || ops.top() == '/'))
{
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if (op == '+')
nums.push(a + b);
else if (op == '-')
nums.push(a - b);
else if (op == '*')
nums.push(a * b);
else if (op == '/')
nums.push(a / b);
}
ops.push(s[i]);
}
}
while (!ops.empty())
{
double b = nums.top();
nums.pop();
double a = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if (op == '+')
nums.push(a + b);
else if (op == '-')
nums.push(a - b);
else if (op == '*')
nums.push(a * b);
else if (op == '/')
nums.push(a / b);
}
cout << fixed << setprecision(2) << nums.top() << endl;
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)