#include <TM1637.h>
#include <Arduino.h>
const int CLK = 2;
const int DIO = 3;
TM1637 tm(CLK, DIO);
void setup() {
Serial.begin(9600);
tm.init();
tm.set(BRIGHT_TYPICAL);
Serial.println("Введите математическое выражение:");
}
void displayNumber(int number) {
tm.display(0, (number / 1000) % 10);
tm.display(1, (number / 100) % 10);
tm.display(2, (number / 10) % 10);
tm.display(3, number % 10);
}
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
void infixToRPN(const String &infix, String outputQueue[], int &queueSize) {
char operators[100];
int top = -1;
queueSize = 0;
String token;
for (int i = 0; i < infix.length(); i++) {
char c = infix[i];
if (isdigit(c)) {
token += c;
} else {
if (token.length() > 0) {
outputQueue[queueSize++] = token;
token = "";
}
if (c == '(') {
operators[++top] = c;
} else if (c == ')') {
while (top != -1 && operators[top] != '(') {
outputQueue[queueSize++] = String(operators[top]);
top--;
}
top--; // Pop '('
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (top != -1 && precedence(operators[top]) >= precedence(c)) {
outputQueue[queueSize++] = String(operators[top]);
top--;
}
operators[++top] = c;
}
}
}
if (token.length() > 0) {
outputQueue[queueSize++] = token;
}
while (top != -1) {
outputQueue[queueSize++] = String(operators[top]);
top--;
}
}
int evaluateRPN(String rpnQueue[], int queueSize) {
int values[100];
int top = -1;
for (int i = 0; i < queueSize; i++) {
String token = rpnQueue[i];
if (isdigit(token[0])) {
values[++top] = token.toInt();
} else {
int val2 = values[top--];
int val1 = values[top--];
switch (token[0]) {
case '+': values[++top] = val1 + val2; break;
case '-': values[++top] = val1 - val2; break;
case '*': values[++top] = val1 * val2; break;
case '/': values[++top] = val1 / val2; break;
}
}
}
return values[top];
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input.length() > 0) {
String rpnQueue[100];
int queueSize = 0;
infixToRPN(input, rpnQueue, queueSize);
long result = evaluateRPN(rpnQueue, queueSize);
Serial.print("Результат: ");
Serial.println(result);
displayNumber(result);
}
}
}
// 123+(32*(2+3)/4)