Que tal! Quisiera saber como hacer que mi calculadora haga operaciones faltando un operador, en otras palabras asi:
5+ __=5
6 - =6
Y que la multiplicación se interprete como 1=> 7*=7
Por el momento, mi codigo es este
#include<LiquidCrystal.h>
#include<Keypad.h>
LiquidCrystal lcd=LiquidCrystal (8,9,10,11,12,13);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'X', '0', '=', '/'}
};
byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {3,2,1,0};
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
boolean valorActual = false;
boolean siguiente = false;
boolean final = false;
String num1, num2,num3;
int total;
int movimiento;
float r1,r2;
float decimal;
int contador=0;
char op;
void setup() {
lcd.begin(16,2);
}
void loop(){
char key = myKeypad.getKey();
if (key != NO_KEY && (key=='9'||key=='8'||key=='7'||key=='6'||key=='5'||key=='4'||key=='3'||key=='2'||key=='1'||key=='0'||key=='#')){
if (valorActual != true){
num1 = num1 + key;
int numLength = num1.length();
movimiento = numLength;
lcd.setCursor(0, 0);
lcd.print(num1);
}
else {
num2 = num2 + key;
int numLength = num2.length();
lcd.setCursor(movimiento+1, 0);
lcd.print(num2);
final = true;
}
}
else if (valorActual == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
if (valorActual == false){
valorActual = true;
op = key;
lcd.setCursor(movimiento,0);
lcd.print(op);
}
}
else if (final == true && key != NO_KEY && key == '='){
if (op == '+'){
total = num1.toInt() + num2.toInt();
}
else if (op == '-'){
total = num1.toInt() - num2.toInt();
}
else if (op == '*'){
total = num1.toInt() * num2.toInt();
}
else if (op == '/'){
if(num2.toInt()==0){
total = ' ';
}else{
total = num1.toInt() / num2.toInt();
r1=num1.toInt();
r2=num2.toInt();
decimal=r1/r2;
}
}
num3=String(total);
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
if(total==' '){
lcd.print("Syntax Error");
}else if(num3.length()>7){
lcd.print("No hay memoria");
}else{
if (op== '/') {lcd.print(decimal,3);}
else {lcd.print(total);};
contador=1;
}
lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'X'){
lcd.clear();
valorActual = false;
final = false;
num1 = "";
num2 = "";
total = 0;
op = ' ';
}
}