# How to build an Calculator

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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?

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×