Hello all. I'm having problems figuring out how to get atof working in my program. I'm not sure exactly what I'm missing, and I was hoping someone could point me in the right direction. Thanks.
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
static int top = -1;
static double stack[50];
void push(double d)
{
top++;
stack[top]=d;
}
double pop()
{
double p;
p = stack[top];
top--;
return p;
}
int main(int argc, char *argv[])
{
int i;
double a, b, r;
static double stack[50];
static int top = -1;
for(i=1; i<argc; i++)
{
if(argv[i][0]=='+')
{
b = pop();
a = pop();
r = a + b;
push(r);
}
else if(argv[i][0]=='-')
{
b = pop();
a = pop();
r = a - b;
push(r);
}
else if(argv[i][0]=='^')
{
b = pop();
a = pop();
r = pow(a,b);
}
else if(argv[i][0]=='*')
{
b = pop();
a = pop();
r = a * b;
}
else if(argv[i][0]=='/')
{
b = pop();
a = pop();
r = a / b;
}
else if(atof(argv[i][0]))
{
double num;
num = atof(argv[i][0]);
push(num);
}
}
printf("%f\n",r);
return 0;
}