Help needed here please!!!!!!

This is a discussion on Help needed here please!!!!!! within the C Programming forums, part of the General Programming Boards category; Please can someone assist me, this code is meant to evaluate a postfix expression, but it not working for negative ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    49

    Help needed here please!!!!!!

    Please can someone assist me, this code is meant to evaluate a postfix expression, but it not working for negative digits. something like 2 -5 - i.e for 2-(-5)=7.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXCOLS 80
    int main()
         {
              char instring[MAXCOLS], postring[MAXCOLS];
              int position = 0;
              float eval();
              float result;
              printf("%s", "Enter postfix expression with space as delimiter: ");
              while ((postring[position++] = getchar()) != '\n');
              postring[--position] = '\0';
              printf("%s%s", "postfix expression is", postring);
              getchar();
              result=eval(postring);
              printf("%s%f\n", "value is", result);
              getchar();
    }
    struct stack{
                 int top;
                 float items[MAXCOLS];
                 };
    float eval(expr)
                   char expr[];
                  
                  {
                    int c, position;
                    float op1, op2, value;
                    float oper(), pop();
                    struct stack opndstk;
                    opndstk.top = -1;
                    for (position =0; (c=expr[position]) != '\0'; position++)
                         if(c==' ')
                                  {
                                  }
                                 else
                                     if(isdigit(c)){
                                                    int sign; 
                                                    double val, power,res;                            
                                                    sign = (expr[position] == '-') ? -1 : 1;
                         
                                                    for (val = 0.0; isdigit(expr[position]); position++)
                                                         val = 10.0 * val + (expr[position] - '0');
                                                         if (expr[position] == '.')
                                                            {
                                                               position++;
                                                            }
                                                         else
                                                             {
                                                              }
                                                     for (power = 1.0; isdigit(expr[position]); position++)
                                                         {
                                                            val = 10.0 * val + (expr[position] - '0');
                                                            power *= 10;
                                                          }
                                                    res= sign * val / power;
                                                    push(&opndstk, res);  
                                                    }       
                                       else {
                                           op2 = pop(&opndstk);
                                           op1 = pop(&opndstk);
                                           value = oper(c, op1, op2);
                                           push (&opndstk, value);
                                           }
                                           return (pop(&opndstk));
     }
    
    float oper(symb, operand1, operand2)
    int symb;
    float operand1, operand2;
             { 
              switch(symb){
                                  case '+': return (operand1 + operand2);
                                  case '-': return (operand1 - operand2);
                                  case '*': return (operand1 * operand2);
                                  case '/': return (operand1 / operand2);
                                  default: printf("%s", "illegal operation");
                                  exit(1);
                          }
             }
    
    push(ps, x)
    struct stack *ps;
    float x;
         {
          if(ps->top == MAXCOLS-1)
                {
                 printf("%s", "stack overflow");
                 exit(1);
                 }
    else
          ps->items[++(ps->top)] =x;
          return;
          }
    
    float pop(ps)
    struct stack *ps;
        {
        
    if (ps->top==-1)
              {
    printf("%s", "stack underflow");
    exit(1);
              }
    else
           return(ps->items[ps->top--]);
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    isdigit('-') does not return true. So when your program gets to that, it goes into your else clause and does some unexpected behaviour.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by Happy_Reaper View Post
    isdigit('-') does not return true. So when your program gets to that, it goes into your else clause and does some unexpected behaviour.
    You're right but how do I reframe that point. I know that's where the problem is but reframing it has been my problem. Any suggestion how this could be done?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Reading everything in as a character seems like an annoyance. I'd read in all the integers first, then read in all the characters.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21