Sign in to follow this  
srikanthpv

How to build an Calculator

Recommended Posts

srikanthpv    100
All the remember when i was programming in my school was using a switch operator but they dint want me to use switch....And infact were asking me to build a Improved calc...Using unsigned int... #include <stdio.h> int main() { double number1 = 1.0; double number2 = 2.0; char operation = '+'; char answer = 0; switch(operation) { case '+': printf("= %lf\n", number1 + number2); break; case '-': printf("= %lf\n", number1 - number2); break; case '*': printf("= %lf\n", number1 * number2); break; case '/': if(number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %lf\n", number1 / number2); break; case '%': if((long)number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %ld\n", (long)number1 % (long)number2); break; default: printf("\n\n\aIllegal operation!\n"); } } [Edited by - srikanthpv on November 21, 2008 2:30:05 AM]

Share this post


Link to post
Share on other sites
Wyrframe    2426
What's the question?

How to make a real, usable calculator? Fire up a linux or unix shell and type `man dc`.

How to make a real, usable, GUI calculator? Start, Run, Calc.exe. Innumerable tutorials out there on how to make such an interface.

Best practices on switched logic? Beware of flamewar. But switch(){} will do for now if you don't need runtime-extendable logic.

Share this post


Link to post
Share on other sites
rip-off    10979
One alternative to switch is to use a function pointer table:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double divide(double a, double b) { return a / b; }
double multiply(double a, double b) { return a * b; }

typedef double (*operation_function_pointer)(double, double);

struct operation
{
char symbol;
operation_function_pointer function;
};

static operation operator_table[] =
{
{'+',&add},
{'-',&subtract},
{'/',&divide},
{'*',&multiply},
{'^',&pow}, // pow from <math.h>
{'\0',0} // sentinal value so we can detect the end of the list
// there are other ways to do this
};

int main()
{
double current = 0;

while(1)
{
operation *ptr = 0;
char op;

op = /* request an operator */

for(ptr = operator_table; ptr->function && ptr->symbol != op; ++ptr)
{
continue;
}

if(ptr->function)
{
double number = /* request a number */
current = ptr->function(current,number);
printf("current value is %lf\n",current);
}
else
{
printf("operator %c not found!\n",op);
}
}
}


Some terms to Google would be "function pointer", "function pointer table" or "jump table".

In C++, we could use std::map<>, which would reduce the amount of logic we need to write while being efficient if our function pointer table becomes quite large.

If the table values were tightly packed (like in some range M..N) then the logic for finding an entry is simpler. This is often the case when dealing with a enumeration.

I don't understand how "unsigned int" has anything to do with it. Unless they gave you a much more specific hint... Can you elaborate?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this