Hi. I'm trying to make a calculator, but I ran into some problem. My code is rly bad as I'm new to this. Thanks for stopping by and helping me!
Here is my code:
// Why
// am
// I
// getting
// these
// error
// messages?
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <stdlib.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS][COLS] =
{
{'1','2','3','/'},
{'4','5','6','*'},
{'7','8','9','-'},
{'~','0','=','+'}
};
byte rowPins[ROWS] = {7,6,5,4};
byte colPins[COLS] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);
void setup()
{
lcd.begin(16,2);
}
struct Rational
{
int numerator;
int denominator;
};
typedef struct Rational rational;
rational div(rational x, int y)
{
rational z =
{x.numerator, x.denominator * y};
return z;
}
rational mul(rational x, int y)
{
rational z =
{x.numerator * y, x.denominator};
return z;
}
rational sub(rational x, rational y)
{
rational z =
{
x.numerator * y.denominator - y.numerator * x.denominator,
x.denominator * y.denominator
};
return z;
}
rational add(rational x, rational y)
{
rational z =
{
x.numerator * y.denominator + y.numerator * x.denominator,
x.denominator * y.denominator
};
return z;
}
rational calc(String num)
{
String temp = "";
String ari = "";
rational a = {0, 1};
bool cal = false;
int length = 0;
String S = num;
for (int i = 0; i < sizeof(num)/sizeof(num[0]); i++)
{
if (num[i] == '/')
{
if (ari == "/")
{
temp = S.substring(0, i - length + 1);
a = div(a, temp.toInt());
S.remove(0, i-length);
ari = "/";
cal = true;
break;
}
if (ari == "*")
{
temp = num.substring(0, i - length + 1);
a = mul(a, temp.toInt());
S.remove(0, i-length);
ari = "/";
cal = true;
break;
}
if (ari == "")
{
temp = num.substring(0, i+1);
a.numerator = temp.toInt();
S.remove(0, i-length);
length = temp.length()-'0';
ari = "/";
break;
}
}
if (num[i] == '*')
{
if (ari == "/")
{
temp = num.substring(0, i - length + 1);
a = div(a, temp.toInt());
S.remove(0, i-length);
ari = "*";
cal = true;
break;
}
if (ari == "*")
{
temp = num.substring(0, i - length + 1);
a = mul(a, temp.toInt());
S.remove(0, i-length);
ari = "*";
cal = true;
break;
}
if (ari == "")
{
temp = num.substring(0, i+1);
a.numerator = temp.toInt();
S.remove(0, i-length);
length = temp.length()-'0';
ari = "*";
break;
}
}
}
if (cal == false)
a.numerator = num.toInt();
return a;
}
int gcd(int x, int y)
{
if(y == 0)
return x;
else
return gcd(y, y % x);
}
void reduce(rational &x)
{
int GCD = gcd(x.numerator, x.denominator);
x = {x.numerator / GCD, x.denominator / GCD};
}
int x = -1;
String number = "";
String AriTemp = "";
int count = 0;
rational NumTemp = {0, 0};
rational FinResult = {0, 0};
double DecFinResult = 0;
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
x += 1;
number = number + key;
count += 1;
lcd.setCursor(x,1);
lcd.print(key);
if (x == 16)
{
lcd.clear();
x = 0;
lcd.setCursor(x,1);
}
if (key == '+')
{
AriTemp += "+";
if (AriTemp.length() == '2')
{
if(AriTemp[0] == '+')
{
NumTemp = add(NumTemp, calc(number));
AriTemp.remove(0, 0);
}
else if(AriTemp[0] == '-')
{
NumTemp = sub(NumTemp, calc(number));
AriTemp.remove(0, 0);
}
}
else
{
NumTemp = calc(number);
AriTemp += "+";
}
number = "";
}
if (key == '-')
{
AriTemp += "-";
if (AriTemp.length() == '2')
{
if(AriTemp[0] == '+')
{
NumTemp = add(NumTemp, calc(number));
AriTemp.remove(0, 0);
}
else if(AriTemp[0] == '-')
{
NumTemp = sub(NumTemp, calc(number));
AriTemp.remove(0, 0);
}
}
else
{
NumTemp = calc(number);
AriTemp += "-";
}
number = "";
}
delay(10);
}
if (key == '=')
{
lcd.clear();
lcd.setCursor(0,0);
if (NumTemp.denominator == 0)
FinResult = calc(number);
else if (AriTemp[0] == '+')
FinResult = add(NumTemp, calc(number));
else if (AriTemp[0] == '-')
FinResult = sub(NumTemp, calc(number));
reduce(FinResult);
DecFinResult = FinResult.numerator / FinResult.denominator;
lcd.print(DecFinResult);
lcd.setCursor(0,1);
lcd.print(FinResult.numerator);
lcd.print("/");
lcd.print(FinResult.denominator);
}
}
2:1: error: 'rational' does not name a type; did you mean 'atol'?
3:1: error: 'rational' does not name a type; did you mean 'atol'?
4:1: error: 'rational' does not name a type; did you mean 'atol'?
5:1: error: 'rational' does not name a type; did you mean 'atol'?
6:1: error: 'rational' does not name a type; did you mean 'atol'?
8:13: error: variable or field 'reduce' declared void
8:13: error: 'rational' was not declared in this scope
8:13: note: suggested alternative: 'atol'
8:23: error: 'x' was not declared in this scope
avre[0m 1.8.6 e[90m/home/tcad/.arduino15/packages/arduino/hardware/avr/1.8.6e[0m
exit status 1
(This part should be correct. )
