C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 01:30 PM
neuroh neuroh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 11 neuroh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Reverse polish calculator and reading from file

I have a question about reverse polish calculator, here is the K&R reverse polish calculator. I had the calculator working but now I need it to take information from a file. Here is a quick sample of the input file.
#First example from the textbook
1 2 - 4 5 + *
#Compute 5!
1 2 3 4 5 ****
#Here come binomial(14, 9) and binomial(14, 5)
1 = 6 * 1 / = 7 * 2 / = 8 * 3 / = 9 * 4 / = 10 * 5 / = 11 * 6 / = 12 * 7 / = 13 * 8 / = 14 * 9 /
1 = 10 * 1 / = 11 * 2 / = 12 * 3 / = 13 * 4 / = 14 * 5 /

My code is below I have readLine which takes a # statement and just prints it since it is a comment. I was going to have my RPN function call the readLine function until EOF and fprintf it into a file and just loop like that but I am having problem implementing it. Can anyone give me some advice? And explain how exactly RPN, getop exactly work. Thanks.
Code:
//
//  main.c
//  inputReader
//

#include <stdio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER '0'
#define MAXOP 100
#define MAXVAL 100
#define BUFSIZE 100
int sp = 0;
char out[256];
double val[MAXVAL];  /* value stack */
int buff[1024];
int i;
int run;
double pop(void);
void push(double f);
int getch(void);
void ungetch(int c);
char *RPNCalc()
{
	int type, iop2;
	double op1, op2, store_val;
	char s[MAXOP];
	char *outp = out;
	while ((type = getop(s)) != EOF) {
		switch (type) {
            case NUMBER:
                push(atof(s));
                break;
            case 'x':		//exchange top two stack items
                op1 = pop();
                op2 = pop();
                push(op1);
                push(op2);
                break;
            case 'R':
                push(store_val);
                break;
            case 'S':
                store_val = pop();
                break;
            case 0xd:		//skip cr
                break;
            case '+':
                push(pop() + pop());
                break;
            case '*':
                push(pop() * pop());
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case '/':
                op2 = pop();
                if (op2 != 0.0)
                    push(pop() / op2);
                else
                    outp += sprintf(outp, "error: zero divisor\n");
                break;
            case '=':	
                outp += sprintf(outp, "\t%.16g\n", op2 = pop());
                push(op2);
                break;
            case '\n':
                outp += sprintf(outp, "\t%.16g\n", op2 = pop());
                push(op2);
                return out;
                break;
            default:
                outp += sprintf(outp, "error: unknown command %s\n", s);
                break;
		}
	}
	return out;
}

int getop(char s[])
{
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.')
        return c;
    i = 0;
    if (isdigit(c))
        while (isdigit(s[++i] = c = getch()))
            ;
    if (c == '.')
        while (isdigit(s[++i] = c = getch())); s[i] = '\0';
    if (c != EOF)
        ungetch(c);
    return NUMBER;
}
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}
void push(double f)
{
    if (sp < MAXVAL)
        val[sp++] = f;
    else
        printf("error: stack full, can't push %g\n", f);
}
double pop(void)
{
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n");
        return 0.0;
    }
}
int readLine(FILE *fp)
{
    i = 0;
    char c;
    while( (c = fgetc(fp)) != EOF && (c ) != '\n')
    {
        if ( c == '#' )
        {
            printf("%c", c);
            while( (c = fgetc(fp)) != EOF && c != '\n')
            {
                printf("%c", c);
            }
            printf("\n");
            return 0;
        }
        buff[i] = c;
        i++;
        run = 1;
        printf("%c", c);
    }
    printf("\n");
    return 0;
}
int main(int argc, const char * argv[])
{

    FILE *fp;
    if ((fp = fopen ("/Documents/buffthat/buffthat/input.txt", "rb")) == NULL)
        return -1;
    readLine(fp);
    readLine(fp);
    readLine(fp);
    return 0;
}

Reply With Quote
  #2  
Old November 15th, 2012, 02:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
Yes, my first piece of advice for you is that you use code tags! Only then might we have some semblence of a chance of deciphering the code. As it is now, it's a huge unreadable mess.

Reply With Quote
  #3  
Old November 15th, 2012, 02:47 PM
neuroh neuroh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 11 neuroh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Apologies

I am sorry, new to posting on this forum, was not sure on how to do it. Should of looked more into before posting. I am sorry for that.

Reply With Quote
  #4  
Old November 15th, 2012, 05:19 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
Using blank lines to separate functions from each other, globals from functions, and variable declarations from code would also help. But the most essential is the code tags.

readLine reads a line from the file and puts it into buf.
RPNCalc reads from buf and outputs to out.

So then after a call to readLine, call RPNCalc and read the output from out.

Reply With Quote
  #5  
Old November 15th, 2012, 07:57 PM
neuroh neuroh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 11 neuroh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Question

Thank you for the advice, I had a follow up question. First would I just pass the buffer that I made over to rpncalc? Second should that buffer be an buffer of integers or characters? Also how would I change the while loop in RPNCalc to work with my readline? Should I have readLine return 1 for true if it read a line and 2 for if there is no more lines? Or do you have another suggestion on how to handle that. Sorry, I have a strong JAVA back ground but trying to jump into c and learn it.

Reply With Quote
  #6  
Old November 16th, 2012, 01:23 AM
neuroh neuroh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 11 neuroh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Help

Here what I modded to try and get input into rpn calculator. This does the job of reading the buffer backwards from getChar after using readLine. I tried it with a data line but it ran into some major errors. Was wondering if someone could explain to me the getop method in there. Thanks, and what I am doing wrong too please.

Code:

int readLine(FILE *fp)
{
    readVal = 0;
    i = 0;
    char c;
    while( (c = fgetc(fp)) != EOF && (c ) != '\n')
    {
        if ( c == '#' )
        {
            printf("%c", c);
            while( (c = fgetc(fp)) != EOF && c != '\n')
            {
                printf("%c", c);
            }
            printf("\n");
            return readVal;
        }
        readVal = 1;
        buff[i] = c;
        i++;
        run = 1;
        printf("%c", c);
    }
    printf("\n");
    return readVal;
}
int getChar(void)
{
    char b;
    if ( i > 0)
    {
        b = buff[--i];
    }
    return b;
}
int getop(char s[], FILE *fp)
{
    int i, c;
    while ((s[0] = c = getChar() ) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.')
        return c;
    i = 0;
    if (isdigit(c))
        while (isdigit(s[++i] = c = getChar()))
            ;
    if (c == '.')
        while (isdigit(s[++i] = c = getChar())); s[i] = '\0';
    if (c != EOF)
        ungetch(c);
    return NUMBER;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reverse polish calculator and reading from file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap