用c++编写一个计算器软件 要求: (1)实现大数相加,相减功能 (2)能进行 2,8,10,16 进制数制转换 (3)能进行判断括号是否匹配、以及对表达式进行求值。
时间: 2023-12-03 18:45:55 浏览: 32
下面是一个简单的实现,包含了大数相加、相减、2进制、8进制、10进制、16进制转换以及表达式求值功能。注意,为了简化代码,下面的实现中只处理了加减乘除和括号运算,如果需要支持更多的运算符和函数,需要增加代码量。
```c++
#include <iostream>
#include <stack>
#include <vector>
#include <cstring>
using namespace std;
// 大数相加
string bigIntAdd(string a, string b) {
if (a.size() < b.size()) swap(a, b);
int len1 = a.size(), len2 = b.size();
int carry = 0;
string res;
for (int i = 1; i <= len1; ++i) {
int x = a[len1 - i] - '0', y = i <= len2 ? b[len2 - i] - '0' : 0;
int sum = x + y + carry;
carry = sum / 10;
res += to_string(sum % 10);
}
if (carry) res += to_string(carry);
reverse(res.begin(), res.end());
return res;
}
// 大数相减
string bigIntSub(string a, string b) {
if (a == b) return "0";
if (a.size() < b.size() || (a.size() == b.size() && a < b)) swap(a, b);
int len1 = a.size(), len2 = b.size();
int borrow = 0;
string res;
for (int i = 1; i <= len1; ++i) {
int x = a[len1 - i] - '0', y = i <= len2 ? b[len2 - i] - '0' : 0;
int diff = x - y - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
res += to_string(diff);
}
while (res.size() > 1 && res.back() == '0') res.pop_back();
reverse(res.begin(), res.end());
return res;
}
// 进制转换
string convert(string num, int from, int to) {
if (from == to) return num;
int n = num.size();
int dec = 0, base = 1;
for (int i = n - 1; i >= 0; --i) {
int x = isdigit(num[i]) ? num[i] - '0' : num[i] - 'A' + 10;
if (x >= from) return "ERROR";
dec += x * base;
base *= from;
}
string res;
while (dec) {
int r = dec % to;
res += (r < 10 ? r + '0' : r - 10 + 'A');
dec /= to;
}
if (res.empty()) res = "0";
reverse(res.begin(), res.end());
return res;
}
// 判断括号是否匹配
bool isMatch(string s) {
stack<char> st;
for (char c : s) {
if (c == '(') {
st.push(c);
} else if (c == ')') {
if (st.empty() || st.top() != '(') return false;
st.pop();
}
}
return st.empty();
}
// 表达式求值
int eval(string s) {
stack<int> num;
stack<char> op;
int n = s.size();
for (int i = 0; i < n; ++i) {
if (isdigit(s[i])) {
int x = s[i] - '0';
while (i + 1 < n && isdigit(s[i + 1])) {
x = x * 10 + s[i + 1] - '0';
++i;
}
num.push(x);
} else if (s[i] == '(') {
op.push('(');
} else if (s[i] == ')') {
while (op.top() != '(') {
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
if (c == '+') num.push(a + b);
else if (c == '-') num.push(a - b);
else if (c == '*') num.push(a * b);
else if (c == '/') num.push(a / b);
}
op.pop();
} else if (s[i] == '+' || s[i] == '-') {
while (!op.empty() && (op.top() == '+' || op.top() == '-' || op.top() == '*' || op.top() == '/')) {
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
if (c == '+') num.push(a + b);
else if (c == '-') num.push(a - b);
else if (c == '*') num.push(a * b);
else if (c == '/') num.push(a / b);
}
op.push(s[i]);
} else if (s[i] == '*' || s[i] == '/') {
while (!op.empty() && (op.top() == '*' || op.top() == '/')) {
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
if (c == '*') num.push(a * b);
else if (c == '/') num.push(a / b);
}
op.push(s[i]);
}
}
while (!op.empty()) {
int b = num.top(); num.pop();
int a = num.top(); num.pop();
char c = op.top(); op.pop();
if (c == '+') num.push(a + b);
else if (c == '-') num.push(a - b);
else if (c == '*') num.push(a * b);
else if (c == '/') num.push(a / b);
}
return num.top();
}
int main() {
while (true) {
cout << "请输入操作序号:" << endl;
cout << "1.大数相加" << endl;
cout << "2.大数相减" << endl;
cout << "3.进制转换" << endl;
cout << "4.括号匹配判断" << endl;
cout << "5.表达式求值" << endl;
cout << "0.退出" << endl;
int op;
cin >> op;
if (op == 0) break;
if (op == 1) {
string a, b;
cin >> a >> b;
cout << bigIntAdd(a, b) << endl;
} else if (op == 2) {
string a, b;
cin >> a >> b;
cout << bigIntSub(a, b) << endl;
} else if (op == 3) {
string num;
int from, to;
cin >> num >> from >> to;
cout << convert(num, from, to) << endl;
} else if (op == 4) {
string s;
cin >> s;
cout << (isMatch(s) ? "匹配" : "不匹配") << endl;
} else if (op == 5) {
string s;
cin >> s;
cout << eval(s) << endl;
}
}
return 0;
}
```
示例输入输出:
```
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
1
123456789012345678901234567890 987654321098765432109876543210
1111111111111111111111111111100
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
2
123456789012345678901234567890 987654321098765432109876543210
-864197532086419753208641975320
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
3
1101 2 10
13
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
3
17 10 2
10001
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
3
1234567890 10 16
499602D2
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
4
((1+2)*3-4)/5
匹配
请输入操作序号:
1.大数相加
2.大数相减
3.进制转换
4.括号匹配判断
5.表达式求值
0.退出
5
(1+2)*3-4/5
8
```