用dev c++ 代码 实现 计算器功能 要求: (1)实现大数相加,相减功能 (2)能进行2,8,10,16进制数制转换 (3)能进行判断括号是否匹配、以及对表达式进行求值。
时间: 2024-03-22 14:37:44 浏览: 101
以下是一个简单的实现示例,仅供参考:
```
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
// 大数相加函数
string add(string a, string b) {
string res = "";
int carry = 0, i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int x = i >= 0 ? a[i--] - '0' : 0;
int y = j >= 0 ? b[j--] - '0' : 0;
int sum = x + y + carry;
res = to_string(sum % 10) + res;
carry = sum / 10;
}
return res;
}
// 大数相减函数
string sub(string a, string b) {
string res = "";
int borrow = 0, i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0) {
int x = i >= 0 ? a[i--] - '0' : 0;
int y = j >= 0 ? b[j--] - '0' : 0;
int diff = x - y - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
res = to_string(diff) + res;
}
while (res.size() > 1 && res[0] == '0') res.erase(0, 1);
return res;
}
// 十进制转其他进制函数
string convert(int n, int base) {
string res = "";
while (n) {
int mod = n % base;
if (mod < 10) {
res = to_string(mod) + res;
} else {
res = (char)(mod - 10 + 'A') + res;
}
n /= base;
}
if (res.empty()) res = "0";
return res;
}
// 其他进制转十进制函数
int convert(string s, int base) {
int res = 0;
for (char c : s) {
int digit = isdigit(c) ? c - '0' : c - 'A' + 10;
res = res * base + digit;
}
return res;
}
// 判断括号是否匹配函数
bool isMatched(string s) {
stack<char> stk;
for (char c : s) {
if (c == '(') {
stk.push(c);
} else if (c == ')') {
if (stk.empty() || stk.top() != '(') {
return false;
}
stk.pop();
}
}
return stk.empty();
}
// 表达式求值函数
int evaluate(string s) {
stack<int> nums, ops;
for (int i = 0; i < s.size(); i++) {
if (isdigit(s[i])) {
int 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(s[i]);
} else if (s[i] == ')') {
while (ops.top() != '(') {
int b = nums.top(); nums.pop();
int 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() != '(') {
int b = nums.top(); nums.pop();
int 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() == '/')) {
int b = nums.top(); nums.pop();
int 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()) {
int b = nums.top(); nums.pop();
int 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);
}
return nums.top();
}
int main() {
int command;
do {
cout << "请选择功能:" << endl;
cout << "1. 大数相加" << endl;
cout << "2. 大数相减" << endl;
cout << "3. 进制转换" << endl;
cout << "4. 判断括号是否匹配" << endl;
cout << "5. 表达式求值" << endl;
cout << "0. 退出" << endl;
cin >> command;
switch (command) {
case 1: {
string a, b;
cout << "请输入两个数字(用空格隔开):" << endl;
cin >> a >> b;
cout << a << " + " << b << " = " << add(a, b) << endl;
break;
}
case 2: {
string a, b;
cout << "请输入两个数字(用空格隔开):" << endl;
cin >> a >> b;
cout << a << " - " << b << " = " << sub(a, b) << endl;
break;
}
case 3: {
int n, base;
cout << "请输入一个十进制数字和目标进制(用空格隔开):" << endl;
cin >> n >> base;
cout << "转换结果:" << convert(n, base) << endl;
break;
}
case 4: {
string s;
cout << "请输入一个字符串:" << endl;
cin >> s;
cout << "括号是否匹配:" << isMatched(s) << endl;
break;
}
case 5: {
string s;
cout << "请输入一个表达式:" << endl;
cin >> s;
cout << "表达式求值:" << evaluate(s) << endl;
break;
}
default: break;
}
} while (command != 0);
return 0;
}
```
该代码实现了一个简单的控制台菜单,可以根据用户选择调用相应的函数进行计算。需要注意的是,该代码还有很多细节问题需要处理,例如输入格式检查、错误处理、界面设计等。另外,由于代码长度限制,该示例并没有实现16进制转换功能,但实现方法与其他进制转换类似,可以自行尝试实现。
阅读全文