M
mdh
Hi All,
I have been struggling with this for a few hours. The exercise is a
variation on the RPN calculator, asking to use scanf or sscanf for
input.
I have included the ( partially completed) code below, but this is the
puzzle. ( It's from my old standby Tondo and Gimpel)
Firstly, it compiles with out any errors!!!
If I set a breakpoint in main, and step through the code, it returns
the expected output.
If I set a breakpoint in 'getop' at the first 'while' statement, it
seems to skip the line "sscanf(lastc, "%c", &c);"
If no breakpoint is set, I get the same output as the line above, but
cannot be sure, where the error is.
ps...if the formatting below looks wrong, please tell me / please let
me know how to present it for the clc.
/********/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXOP 10
#define NUMBER '0'
double pop(void);
void push (double);
int getop(char *);
int main (int argc, const char * argv[])
{
char line[MAXOP];
int type;
while ( (type = getop(line)) != EOF)
switch (type)
{
case NUMBER:
push (atof(line));
break;
case '+':
push (pop() + pop());
break;
case '\n':
printf("\t%.5g", pop());
break;
default:
printf("Error: unknown command \'%s\'\n", line);
break;
}
return 0;
}
int getop(char line[])
{
int c, rc, i;
static char lastc[] = " ";
sscanf(lastc, "%c", &c); /** I ***think** this line is the problem?
***/
lastc[0] = ' ';
while ( (line[0]=c) == ' ' || c == '\t')
if ( scanf("%c", &c) == EOF)
c=EOF;
line[1]= '\0';
if ( !isdigit(c) && c != '.')
return c;
i = 0;
if ( isdigit(c))
do
{
rc = scanf("%c", &c);
if (!isdigit(line[++i] = c))
break;
}while (rc != EOF);
if ( c == '.')
do
{
rc = scanf("%c", &c);
if (!isdigit(line[++i]= c))
break;
}while (rc != EOF);
line= '\0';
if ( rc != EOF)
lastc[0] = c;
return NUMBER;
}
#define MAXSTACK 20
double stack[MAXSTACK];
double *dptr = stack;
double *eptr = stack + MAXSTACK;
double pop(void){
if ( dptr >= stack)
return *(--dptr) ;
else{
printf("Error: Stack Empty");
return 0.00;
}
}
void push (double d){
if ( dptr < eptr)
*dptr++ = d;
else
printf("Error: Stack Full");
}
I have been struggling with this for a few hours. The exercise is a
variation on the RPN calculator, asking to use scanf or sscanf for
input.
I have included the ( partially completed) code below, but this is the
puzzle. ( It's from my old standby Tondo and Gimpel)
Firstly, it compiles with out any errors!!!
If I set a breakpoint in main, and step through the code, it returns
the expected output.
If I set a breakpoint in 'getop' at the first 'while' statement, it
seems to skip the line "sscanf(lastc, "%c", &c);"
If no breakpoint is set, I get the same output as the line above, but
cannot be sure, where the error is.
ps...if the formatting below looks wrong, please tell me / please let
me know how to present it for the clc.
/********/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXOP 10
#define NUMBER '0'
double pop(void);
void push (double);
int getop(char *);
int main (int argc, const char * argv[])
{
char line[MAXOP];
int type;
while ( (type = getop(line)) != EOF)
switch (type)
{
case NUMBER:
push (atof(line));
break;
case '+':
push (pop() + pop());
break;
case '\n':
printf("\t%.5g", pop());
break;
default:
printf("Error: unknown command \'%s\'\n", line);
break;
}
return 0;
}
int getop(char line[])
{
int c, rc, i;
static char lastc[] = " ";
sscanf(lastc, "%c", &c); /** I ***think** this line is the problem?
***/
lastc[0] = ' ';
while ( (line[0]=c) == ' ' || c == '\t')
if ( scanf("%c", &c) == EOF)
c=EOF;
line[1]= '\0';
if ( !isdigit(c) && c != '.')
return c;
i = 0;
if ( isdigit(c))
do
{
rc = scanf("%c", &c);
if (!isdigit(line[++i] = c))
break;
}while (rc != EOF);
if ( c == '.')
do
{
rc = scanf("%c", &c);
if (!isdigit(line[++i]= c))
break;
}while (rc != EOF);
line= '\0';
if ( rc != EOF)
lastc[0] = c;
return NUMBER;
}
#define MAXSTACK 20
double stack[MAXSTACK];
double *dptr = stack;
double *eptr = stack + MAXSTACK;
double pop(void){
if ( dptr >= stack)
return *(--dptr) ;
else{
printf("Error: Stack Empty");
return 0.00;
}
}
void push (double d){
if ( dptr < eptr)
*dptr++ = d;
else
printf("Error: Stack Full");
}