0

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;
 }
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by NV43
0

atof() converts a string to a double, except in your case you are giving it not a string, but a single character.

0

How would I make it read the string input? The user input to the program is going to be something like: 2.0 3.0 +, which would result in 2.0+3.0=5.0, 5.0 being output as the final answer. Thank you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.