Not so much hardware related but related to an AVR project that I am working on.
I have been in the process of using an ATMega2560 to make a physically functioning programmable scientific calculator. Last semester in my programming class we built an RPN calculator in java and my professor made mention that an algebraic calculator would be difficult to make, I scoffed at that until just yesterday.
The basic operation of a scientific calculator is extremely complicated...and as I have discovered very convoluted.
There are a couple of rules I have to abide by:
- I am using a stack to hold my numbers and math functions
- The stack should be kept as small as possible and only parenthesis require more than two items on the stack
- math functions such as + - * / should act as an = but remembers to do math to the next new number
The last one is what makes things confusing, because I am trying to make a calculator that operates like a standard scientific calculator or just a standard calculator when you type 1 + 2 and then press + again, it computes the previous equation to 3 the instant you hit the second +, but remembers that it is adding the following number about to be input. Keep in mind, the input is being done with a command terminal style setup where the code tests for the most recent input.
The problem is because I am using a command line based system, I need to know what has happened previously, and I am in a rut that looks like in order to do so I have to use a whole gambit of flags and complicated logic that seems unnecessary.
this is what I have so far, minus the functions in other files that should be self explanatory:
int main(){
char userInput[8];
double data = 0;
while(1){
// TODO Initialize LCD
// TODO while loop scans for button pressed
// sets port value to variable
// get initial user input
printf(">: ");
scanf("%s", userInput);
// First itteration, first input must be a number
// Scans until a valid number is given
while ((double)userInput[0] < 48 || (double)userInput[0] > 57){
printf(">: ");
scanf("%s", userInput);
}
// Enters 1 number into the data variable
numberEntry(userInput, &data);
printf("\n %f", data);
// TODO while loop that polls until button depress
// scan for number or command
printf("\n>: ");
scanf("%s", userInput);
// continue loop as long as userInput is a number
while((double)userInput[0] >= 48 && (double)userInput[0] <= 57){
numberEntry(userInput, &data);
printf(" %f", data);
printf("\n>: ");
scanf("%s", userInput);
}
push(data);
data = 0;
// ---- Break out of inputing numbers to do math
// add
if(strcmp(userInput, "+") == 0){
push(pop() + data);
data = 0;
printf(" %f\n", peek());
}
// subtract
else if(strcmp(userInput, "-") == 0){
push(pop() - data);
data = 0;
printf(" %f\n", peek());
}
// multiply
else if(strcmp(userInput, "*") == 0){
push(pop() * data);
data = 0;
printf(" %f\n", peek());
}
// divide
else if(strcmp(userInput, "/") == 0){
push(pop() / data);
printf(" %f\n", peek());
}
else if(strcmp(userInput, "clr") == 0){
first = null;
data = 0;
printf(" %f\n", peek());
}
}
return 0;
}
obviously this code doesn't work, it pulls a zeros from the stack and adds that. I am at a loss here, if anyone could fill me in for some better topologies that would be great.
I am looking into making the basic calculator and the hardware open source so that people that build their own scientific calculator, getting the input to work like a classical sceintific calculator would be the biggest breakthrough o f the project.
BIG NOTE: If it hasn't seemed obvious I am trying to code in C language for PC just to get a working prototype and once I works will modify the code to work for embedded systems, hence why you see stdio.h and stdlib.h functions and console displays.