Thread: Please could someone assist me with double digits

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

    Please could someone assist me with double digits

    I wrote this code to evaluate a postfx expression but it works for only single digit. I need to make it work for double digit and floats. i.e making it work for something like 1315.5*8+ which is for the infix 13*15.7+8. I don't have problem converting from infix to postfix.
    Code:
    #include<stdio.h>
    #include<stdio.lib>
    #define MAXCOLS 80
    main(){
           char instring[MAXCOLS], postring[MAXCOLS];
           int position = 0;
           float eval();
           float result;
           printf("%s", "Enter postfix expression: ");
           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(isdigit(c))
                push (&opndstk, (float)(c-'0'));
                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;
    {int top;
     if (top==-1){
          printf("%s", "stack underflow");
          exit(1);
          }
          else
           return(ps->items[ps->top--]);
           }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    1315.5*8+ which is for the infix 13*15.7+8
    That looks like bad postfix to me. What's to say it isn't 1 * 315.5 + 8 or 131 * 5.5 + 5 ? You need something to separate your numbers, like a space. So:
    Code:
    131 5.5*8+
    Might be valid, but with just
    Code:
    1315.5*8+
    We have no idea where to break up the number.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is
    Code:
    #include<stdio.lib>
    I would have thought that ".lib" files are no good to include, as they are generally binary files with rather confusing result to the compiler.

    And this is definitely broken if you need to do multi-digit numbers:
    Code:
                if(isdigit(c))
                push (&opndstk, (float)(c-'0'));

    --
    Mats

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Somebody please tell me what I should do. to make it work for double digits.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I just pointed out at least part of what doesn't work with the current code. Someone else pointed out that you need to have a separator between your numerbs.

    You don't REALLY expect US to write the code FOR YOU, do you?

    --
    Mats

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ben2000 View Post
    Somebody please tell me what I should do. to make it work for double digits.
    You can start by responding to those who have already started trying to help you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    I just pointed out at least part of what doesn't work with the current code. Someone else pointed out that you need to have a separator between your numerbs.

    You don't REALLY expect US to write the code FOR YOU, do you?

    --
    Mats
    Remember what I posted is just part of my whole code, the #include<stdlib.h> was needed for some other parts of my program that's why you see it appear there.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by Cactus_Hugger View Post
    That looks like bad postfix to me. What's to say it isn't 1 * 315.5 + 8 or 131 * 5.5 + 5 ? You need something to separate your numbers, like a space. So:
    Code:
    131 5.5*8+
    Might be valid, but with just
    Code:
    1315.5*8+
    We have no idea where to break up the number.
    Should I seperate it at the infix level or after the convertion to postfix. Cos I'm wondering if I do it at the infix level whether the postfix will appear with spaces.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think you understood the original comment abotut spaces. As I understand it (and the code appears to support this) your code READS postfix expressions. So if the expression is 1213+, how do you know which of the following options:
    1 + 213
    12 + 13
    121 + 3
    1213 + <nothing> [incorrect number of arguments for add, but still possibly what the user typed]

    So the input format will have to require spaces (or some other separator, but space is the natural choice, I would think), so we type 12 13+.

    Your conversion from "char to int" is currently only written to cope with one digit, so you'll definitely need to fix that - as I pointed out in a previous post. To support floating point, you'll also have to consider the decimal point and do the conversion of anything after the decimal point correctly, which makes for a slightly more complicated function, but there are some standard functions to help you.

    Oh, and I was pointing out about "stdio.lib", not "stdlib.h" - the latter isn't in your code, but the former is. This may of course be a consequence of you only showing us part of your code and you may have edited it by hand when adding it to the forum - but as it stands, it would definitely not compile.

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    I don't think you understood the original comment abotut spaces. As I understand it (and the code appears to support this) your code READS postfix expressions. So if the expression is 1213+, how do you know which of the following options:
    1 + 213
    12 + 13
    121 + 3
    1213 + <nothing> [incorrect number of arguments for add, but still possibly what the user typed]

    So the input format will have to require spaces (or some other separator, but space is the natural choice, I would think), so we type 12 13+.

    Your conversion from "char to int" is currently only written to cope with one digit, so you'll definitely need to fix that - as I pointed out in a previous post. To support floating point, you'll also have to consider the decimal point and do the conversion of anything after the decimal point correctly, which makes for a slightly more complicated function, but there are some standard functions to help you.

    Oh, and I was pointing out about "stdio.lib", not "stdlib.h" - the latter isn't in your code, but the former is. This may of course be a consequence of you only showing us part of your code and you may have edited it by hand when adding it to the forum - but as it stands, it would definitely not compile.

    --
    Mats
    I have written a code convert from infix to postfix and when I input 12+13 it gives me the postfix as 1213+ and that's why I'm asking the particular point I need to do the tokenization so I have the postfix as 12 13+?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ben2000 View Post
    I have written a code convert from infix to postfix and when I input 12+13 it gives me the postfix as 1213+ and that's why I'm asking the particular point I need to do the tokenization so I have the postfix as 12 13+?
    But that's not the code you've presented in this thread - it reads a string containing postfix operations, so that's where the separators need to be (infix, you can use the operator as a separator, so you don't need to do anything there). If you compare it with a HP calculator, you have to hit enter between two numbers, then hit + to add them. I'm not sure if the "infix to postfix" is part of this same application (but not shown here), or if it's a separate application. Either way, your string that is input to your postfix parser is the part that needs the separation!

    --
    Mats

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    But that's not the code you've presented in this thread - it reads a string containing postfix operations, so that's where the separators need to be (infix, you can use the operator as a separator, so you don't need to do anything there). If you compare it with a HP calculator, you have to hit enter between two numbers, then hit + to add them. I'm not sure if the "infix to postfix" is part of this same application (but not shown here), or if it's a separate application. Either way, your string that is input to your postfix parser is the part that needs the separation!

    --
    Mats
    ok I have succeded in making my infix code produce postfix delimited by spaces, so 13.5+25 will be 13.5 25 + Can you guide me or someone guide me on how to construct the loop cos I need a loop to read through the string and convert it to double before pushing it. This has been difficult for, please assist me.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXCOLS 80
    main(){
              char instring[MAXCOLS], postring[MAXCOLS];
              int position = 0;
              float eval();
              float result;
              printf("%s", "Enter postfix expression: ");
              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(isdigit(c))
    /*I guess this is the point I need a loop to convert to double, if I have 44 55 +, I need to read in 44 convert it to double push it, skip the space and read 55, this code below should do the conversion but I don't know how to integrate it.
     
    double val, power,res;
    int sign;
    sign = (expr[i] == '-') ? -1 : 1;
    for (val = 0.0; isdigit(expr[i]); i++)
    val = 10.0 * val + (expr[i] - '0');
    if (expr[i] == '.')
    {
    i++;
    }
    else
    {
    }
    for (power = 1.0; isdigit(expr[i]); i++)
           {
           val = 10.0 * val + (expr[i] - '0');
           power *= 10;
           }
          res= sign * val / power;
    push(&opndstk, res)*/  
    
                push (&opndstk, (float)(c-'0'));
    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;
        {
         int top;
    if (top==-1)
              {
    printf("%s", "stack underflow");
    exit(1);
              }
    else
           return(ps->items[ps->top--]);
         }

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that looks about right.

    --
    Mats

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    I tried replacing the i's in the red loop with position and removed "push (&opndstk, (float)(c-'0'));" but it didn't run. Mat please help me and work me through. Remember the postfix was delimited by spaces.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to debug the code - try adding a few printf's in the code that deals with a number - perhaps printing the part of the string being parsed, for example.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM