Thread: pushback question (reverse polish calculator)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    EOF looks like it should already be handled correctly. getch() will return it to getop() which will return it here:
    It wont cause EOF has allready been getchared and nowhere stored! Next getch call will try reading a char after EOF signal!

    In this case only getop() needs to change to pushback c irrespective of its value.
    You mean deleting the if(c!= EOF).

    But then ill have to edit getch(). I was thinking about something like this:

    Code:
    #include "calc.h"
    
    int buf = EOF;
    
    int getch()
    {
        int temp;
        
        if(buf == 1) return EOF;
        if(buf == EOF)
        {
          temp = getchar();
        }
        else
        {
          temp = buf;
          buf = EOF;
        }
        
        return temp;
    }
    
    void ungetch(int c) //stores the temp char
    {
         if(c == EOF)
         {
           buf = 1;
           return;
         }
         
         buf = c;
    }
    Last edited by Tool; 12-29-2009 at 11:49 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Tool View Post
    You mean deleting the if(c!= EOF).
    Yep!
    Quote Originally Posted by Tool View Post
    But then ill have to edit getch().
    If you use the source given in the "Bible", array buf[MAXBUF] is of type char but you have it of type int, and that's all that's needed. But testing for EOF in ungetch() doesn't make any sense when EOF isn't even being pushed back.
    Quote Originally Posted by Tool View Post
    I was thinking about something like this:

    Code:
    #include "calc.h"
    
    int buf = EOF;
    
    int getch()
    {
        int temp;
        
        if(buf == 1) return EOF;
        if(buf == EOF)
        {
          temp = getchar();
        }
        else
        {
          temp = buf;
          buf = EOF;
        }
        
        return temp;
    }
    
    void ungetch(int c) //stores the temp char
    {
         if(c == EOF)
         {
           buf = 1;
           return;
         }
         
         buf = c;
    }
    This will work only if you're actually pushing back an EOF, but are you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  2. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 12:33 PM
  3. Reverse() question?
    By correlcj in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2002, 06:02 PM
  4. about reverse date(the 1st one is a wrong question)
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 03:51 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM