float addition 2.5 + 2.5 = 4.0? RPN

Posted by AJ Clou on Stack Overflow See other posts from Stack Overflow or by AJ Clou
Published on 2012-09-18T02:46:28Z Indexed on 2012/09/18 3:37 UTC
Read the original article Hit count: 151

Filed under:
|
|

The code below is my subprogram to do reverse polish notation calculations... basically +, -, *, and /. Everything works in the program except when I try to add 2.5 and 2.5 the program gives me 4.0... I think I have an idea why, but I'm not sure how to fix it... Right now I am reading all the numbers and operators in from command line as required by this assignment, then taking that string and using sscanf to get the numbers out of it... I am thinking that somehow the array that contains the three characters '2', '.', and '5', is not being totally converted to a float... instead i think just the '2' is. Could someone please take a look at my code and either confirm or deny this, and possibly tell me how to fix it so that i get the proper answer? Thank you in advance for any help!

float
fsm (char mystring[])
{
  int i = -1, j, k = 0, state = 0;
  float num1, num2, ans;
  char temp[10];
  c_stack top;
  c_init_stack (&top);
  while (1)
    {
      switch (state)
    {
    case 0:
      i++;
      if ((mystring[i]) == ' ')
        {
          state = 0;
        }
      else if ((isdigit (mystring[i])) || (mystring[i] == '.'))
        {
          state = 1;
        }
      else if ((mystring[i]) == '\0')
        {
          state = 3;
        }
  else
    {
      state = 4;
    }
  break;
case 1:
  temp[k] = mystring[i];
  k++;
  i++;
  if ((isdigit (mystring[i])) || (mystring[i] == '.'))
    {
      state = 1;
    }
  else
    {
      state = 2;
    }
  break;
case 2:
  temp[k] = '\0';
  sscanf (temp, "%f", &num1);
  c_push (&top, num1);
  i--;
  k = 0;
  state = 0;
  break;
case 3:
  ans = c_pop (&top);
  if (c_is_empty (top))
    return ans;
  else
    {
      printf ("There are still items on the stack\n");
      exit (0);
case 4:
      num2 = c_pop (&top);
      num1 = c_pop (&top);
      if (mystring[i] == '+'){
          ans = num1 + num2;
        return ans;
    }
      else if (mystring[i] == '-'){
    ans = num1 - num2;
    return ans;
    }
      else if (mystring[i] == '*'){
    ans = num1 * num2;
    return ans;
    }
      else if (mystring[i] == '/'){
          if (num2){
            ans = num1 / num2;
            return ans;
        }
      else{
        printf ("Error: cannot divide by 0\n");
        exit (0);
          }
    }
      c_push (&top, ans);
      state = 0;
      break;
    }
}
}
}

Here is my main program:

#include <stdio.h>
#include <stdlib.h>
#include "boolean.h"
#include "c_stack.h"
#include <string.h>

int main(int argc, char *argv[])
{
  char mystring[100];
  int i;
  sscanf("", "%s", mystring);
  for (i=1; i<argc; i++){
   strcat(mystring, argv[i]);
   strcat(mystring, " ");
   }
  printf("%.2f\n", fsm(mystring));
  }

and here is the header file with prototypes and the definition for c_stack:

#include "boolean.h"

#ifndef CSTACK_H
#define CSTACK_H

  typedef struct c_stacknode{
  char data;
  struct c_stacknode *next;
  } *c_stack;

#endif

void c_init_stack(c_stack *);
boolean c_is_full(void);
boolean c_is_empty(c_stack);
void c_push(c_stack *,char);
char c_pop(c_stack *);
void print_c_stack(c_stack);
boolean is_open(char);
boolean is_brother(char, char);
float fsm(char[]);

© Stack Overflow or respective owner

Related posts about c

    Related posts about calculator