把代码复制到mind+中上传模式的手动编辑里(arduino c)就能用了,有BUG欢迎评论。
使用方法:
A/B键左右切换,触摸键O选定,按等于输出结果,C清除。
#include <MPython.h>
#include <SimpleList.h>
// 变量定义
String mind_s_ShuChuXianShi;
volatile int mind_n_GeShu; // 当前选择的位置
SimpleList<String> mind_l_ShuChu; // 输入列表
// 函数声明
void onButtonBPressed();
void pin15TouchCallback();
void onButtonAPressed();
void updateDisplay();
void calculateResult();
int getPriority(char op);
void computeTop(float numbers[], int &numTop, char operators[], int &opTop);
void handleInput(int selection);
// 主程序
void setup() {
mPython.begin();
buttonB.setPressedCallback(onButtonBPressed);
touchPadO.setTouchedCallback(pin15TouchCallback);
buttonA.setPressedCallback(onButtonAPressed);
mPython.setTouchThreshold(70);
mind_s_ShuChuXianShi = "";
mind_n_GeShu = 1;
updateDisplay();
}
void loop() {
// 空循环
}
// 更新屏幕显示
void updateDisplay() {
display.fillScreen(0);
display.setCursorLine(1);
display.printLine(" 1 2 3 4 5 6 7 8 9");
display.setCursorLine(2);
display.printLine(" 0 + - * / = c");
display.rect(4, 35, 120, 23, false);
// 计算并更新选择框位置
int rectX = (mind_n_GeShu <= 9) ? ((mind_n_GeShu - 1) * 13) + 4 : ((mind_n_GeShu - 10) * 12.1) + 4;
display.rect(rectX, 1 + ((mind_n_GeShu > 9) ? 17 : 0), 14, 14, false);
// 显示输入内容
display.setCursor(9, 38);
for (int i = 0; i < mind_l_ShuChu.size(); i++) {
display.print(mind_l_ShuChu[i]);
}
}
// 按钮 B 事件(向右移动选择框)
void onButtonBPressed() {
mind_n_GeShu = (mind_n_GeShu % 16) + 1;
updateDisplay();
}
// 按钮 A 事件(向左移动选择框)
void onButtonAPressed() {
mind_n_GeShu = (mind_n_GeShu - 2 + 16) % 16 + 1;
updateDisplay();
}
// 触摸事件(确定输入)
void pin15TouchCallback() {
if (mind_l_ShuChu.size() >= 16) {
return; // 超过输入限制,直接返回
}
handleInput(mind_n_GeShu);
updateDisplay();
}
// 处理输入
void handleInput(int selection) {
// 获取输入的最后一个元素
String lastInput = (mind_l_ShuChu.size() > 0) ? mind_l_ShuChu[mind_l_ShuChu.size() - 1] : "";
// 禁止首个输入为运算符
if (mind_l_ShuChu.size() == 0 && (selection >= 11 && selection <= 14)) {
return;
}
switch (selection) {
case 1 ... 9:
mind_l_ShuChu.push_back(String(selection));
break;
case 10:
mind_l_ShuChu.push_back("0");
break;
case 11:
case 12:
case 13:
case 14:
// 防止连续输入运算符
if (lastInput != "+" && lastInput != "-" && lastInput != "*" && lastInput != "/") {
mind_l_ShuChu.push_back((selection == 11) ? "+" :
(selection == 12) ? "-" :
(selection == 13) ? "*" : "/");
}
break;
case 15:
calculateResult();
break;
case 16:
mind_l_ShuChu.clear(); // 清空输入
break;
}
}
// 计算表达式结果
void calculateResult() {
if (mind_l_ShuChu.size() == 0) {
return; // 输入为空,直接返回
}
const int MAX_SIZE = 16;
float numbers[MAX_SIZE]; // 存放数字
char operators[MAX_SIZE]; // 存放运算符
int numTop = -1, opTop = -1; // 栈指针
// 检查末尾是否为运算符,若是则删除
String lastInput = mind_l_ShuChu[mind_l_ShuChu.size() - 1];
if (lastInput == "+" || lastInput == "-" || lastInput == "*" || lastInput == "/") {
mind_l_ShuChu.pop_back();
if (mind_l_ShuChu.size() == 0) {
mind_l_ShuChu.push_back("错误"); // 只有运算符,直接报错
updateDisplay();
return;
}
}
String expression = "";
for (int i = 0; i < mind_l_ShuChu.size(); i++) {
expression += mind_l_ShuChu[i];
}
float num = 0;
bool hasNum = false;
// 解析表达式
for (int i = 0; i < expression.length(); i++) {
char c = expression[i];
if (isdigit(c)) {
num = num * 10 + (c - '0');
hasNum = true;
} else {
if (hasNum) {
numbers[++numTop] = num;
num = 0;
hasNum = false;
}
// 计算栈顶运算,并确保 `numTop` 不会小于 0
while (opTop >= 0 && numTop >= 0 && getPriority(operators[opTop]) >= getPriority(c)) {
computeTop(numbers, numTop, operators, opTop);
}
operators[++opTop] = c;
}
}
if (hasNum) {
numbers[++numTop] = num;
}
while (opTop >= 0) {
computeTop(numbers, numTop, operators, opTop);
}
// 检查 numTop 是否有效
if (numTop < 0) {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back("错误");
} else {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back(String(numbers[numTop], 2)); // 显示 2 位小数
}
updateDisplay();
}
// 获取运算符优先级
int getPriority(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
// 计算栈顶运算
void computeTop(float numbers[], int &numTop, char operators[], int &opTop) {
if (numTop < 1 || opTop < 0) return; // 确保 numTop 和 opTop 有效
float b = numbers[numTop--];
float a = numbers[numTop--];
char op = operators[opTop--];
float result = 0;
if (op == '+') result = a + b;
else if (op == '-') result = a - b;
else if (op == '*') result = a * b;
else if (op == '/') {
if (b == 0) {
mind_l_ShuChu.clear();
mind_l_ShuChu.push_back("错误");
return;
}
result = a / b;
}
numbers[++numTop] = result;
}
评论