设计一个保险箱用的4位数字代码锁,该锁有规定的地址代码A、B、C、D4个输入端和一个开箱钥匙孔信号E的输入端,锁的代码由实验者自编。当用钥匙开箱时,如果输入的4个代码正确,保险箱被打开;否则,电路将发出警报(可用发光二极管亮表示)。
时间: 2023-12-03 14:45:53 浏览: 40
ops.top();
ops.pop();
BigInt y = vals.top();
vals.pop();
BigInt x = vals.top();
vals.pop();
if (op == '+') {
vals.push(x + y);
} else if (op == '-') {
vals.push(x这是一个简单的保险箱代码锁的设计,以下是电路的原理图和详细 y);
} else if (op == '*') {
vals.push(x * y);
} else if (op == '/') {
vals.push(x / y);
} else if (op == '%') {
vals.push(x % y);
}
}
return vals.top();
}
bool is_valid_expr(string expr) {
stack<char> s;
for (int i = 0; i < expr.size(); i++) {
if (expr[i] == '(') {
s.push(expr[i]);
} else if (expr[i] == ')') {
if (s.empty() || s.top() != '(') {
return false;
}
s.pop();
}
}
return s.empty();
}
int main() {
map<string, int> m;
m["BIN"] = 2;
m["OCT"] = 8;
m["DEC"] = 10;
m["HEX"] = 16;
string expr;
cout << "请输入算术表达式:" << endl;
getline(cin, expr);
if (!is_valid_expr(expr)) {
cout << "表达式不合法!" << endl;
return 0;
}
BigInt res = eval(expr);
cout << "结果:" << res.str() << endl;
cout << "2进制:" << dec_to_bin(res) << endl;
cout << "8进制:" << dec_to_oct(res) << endl;
cout << "10进制:" << res.str() << endl;
cout << "16进制:" << dec_to_hex(res) << endl;
return 0;
}
```
这个程序中,我们定义了一个 eval() 函数,用来求解表达式的值。这个函数使用了两个栈,一个用来存储运算符,一个用来存储操作数。遍历表达式的每个字符,如果是数字,则将其转成 BigInt 类型的变量并压入操作数栈;如果是运算符,则将其与运算符栈
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)