Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
double pop (double stack[], int *stackSize);
void push (double stack[], int *stackSize, double x);
double multiply1 (double x, double y);
double add1 (double x, double y);
double neg1 (double x);
double divide1 (double x, double y);
double square1 (double x);
double squareRoot1 (double x);
double min1 (double x, double y);
double max1 (double x, double y);
double expo1 (double x, double y);
double pi (void);
double L (double x);
double E (double x);
typedef struct Operator
{
char symbol;
int operands;
union
{
void *f;
double (*f0)(void);
double (*f1) (double x);
double (*f2) (double x, double y);
};
}Operator;
//double evaluate (const Operator op[], int opSize, const char * expression);
Operator mult2={'*',2,multiply1};
Operator add2={'+',2,add1};
Operator neg2={'-',1,neg1};
Operator divide2={'/',2,divide1};
Operator square2={'S',1,square1};
Operator sqrt2={'s',1,squareRoot1};
Operator min2={'m',2,min1};
Operator max2={'M',2,max1};
Operator expo2={'^',2,expo1};
Operator pi2={'pi',0,pi};
Operator loga2={'log',1,L};
Operator expon2={'e^',1,E};
int main (void)
{
char input[81]={'\0'};
struct op[13]={mult2, add2, neg2, divide2, square2, sqrt2, min2, max2, expo2, pi2, loga2, expon2};
printf("Enter your calculations (blank to quit): \n");
fgets(input, sizeof(input), stdin);
//printf("Answer: ", evaluate(op[], 13, input));
}
/*double evaluate (const Operator op[], int opSize, const char * expression)
{
int i;
int indexSize;
int *stackSize;
int stackSz=0;
double temp=0;
double temp2=0;
indexSize=strlen(expression);
stack
for (i=0; i<indexSize; ++i)
{
int k;
switch(expression[i])
{
case '1':
push(stack, stackSize, 1.0);
break;
case '2':
push(stack, stackSize, 2.0);
break;
case '3':
push(stack, stackSize, 3.0);
break;
case '4':
push(stack, stackSize, 4.0);
break;
case '5':
push(stack, stackSize, 5.0);
break;
case '6':
push(stack, stackSize, 6.0);
break;
case '7':
push(stack, stackSize, 7.0);
break;
case '8':
push(stack, stackSize, 8.0);
break;
case '9':
push(stack, stackSize, 9.0);
break;
default:
for (k=0; k<opSize; ++k)
{
if(expression[i]==op[k].symbol)
{
switch(op[k].operands)
{
case 0:
push (stack, stackSize, op[k].f0());
break;
case 1:
temp=pop(stack, stackSize);
push (stack, stackSize, op[k].f1(temp));
break;
case 2:
temp=pop(stack, stackSize);
temp2=pop(stack, stackSize);
push (stack, stackSize, op[k].f2(temp, temp2));
break;
}
}
}
}
}
}*/
double multiply1 (double x, double y)
{
return x*y;
}
double add1 (double x, double y)
{
return x+y;
}
double neg1 (double x)
{
return -x;
}
double divide1 (double x, double y)
{
return x/y;
}
double square1 (double x)
{
return pow(x,2);
}
double squareRoot1 (double x)
{
return sqrt(x);
}
double min1 (double x, double y)
{
return x>y?x:y;
}
double max1 (double x, double y)
{
return x>y?y:x;
}
double expo1 (double x, double y)
{
return pow(x,y);
}
double pi (void)
{
return 3.1415926535;
}
double L (double x)
{
return log(x);
}
double E (double x)
{
return exp(x);
}
double pop (double stack[], int *stackSize)
{
double value=0;
value=stack[*stackSize];
--*stackSize;
return value;
}
void push (double stack[], int *stackSize, double x)
{
++*stackSize;
stack[*stackSize]=x;
}
As you can see the evaluate bits are commented out but I've been working on them as well... Using temp variables now for the pops... Seems like an all around good idea.