M
mdh
I wonder if I could ask for help. I have been struggling with this
most of the day, and cannot understand what is going on. The problems
are (1) it does not work!! (but exits normally on compilation...and
(2) when stepped through ( I use Xcode) it crashes without any error
message, at the indicated function. I have dissected it, run
individual parts on their own...which seem to work fine. Thanks in
advance.
I have supplied 3 command line arguments ( 22.3, 11.9, and '+' so the
argc is 4).
I am including the full code, as there are probably a lot I am missing
and messing up.
#include <stdio.h>
#include <math.h>
#define MAXOP 100
#define NUMBER 0
int getop(char *);
void ungets( char *);
void push(double);
double pop( void);
int main ( int argc, char *argv[]){
char s[MAXOP];
double op2;
while ( --argc > 0){
ungets(" ");
ungets(*++argv);
switch(getop(s)){
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
default:
printf("Error: Unknown input");
break;
}
}
printf("\t %.8g", pop());
return 0;
}
/*********/
#define MAXSTACK 50
double static stack[MAXSTACK];
int static stkp=0;
double pop(void){
if ( stkp > 0)
return stack[--stkp];
else{
printf("Error: Stack empty");
return 0.00;
}
}
void push(double i){
if (stkp < MAXSTACK)
stack[stkp++]=i;
else
printf("Error: Stack overflow");
}
/**********/
#include <string.h>
void ungetch( int );
void ungets( char *s){ /*** I think the problem is here ***/
int i;
i=strlen(s);
while ( i > 0)
ungetch(s[--i]);
}
/**********/
# define MAXBUF 25
char buf[MAXBUF];
static int bufp=0;
void ungetch( int c ){
if (bufp < MAXBUF)
buf[bufp++]=c;
else
printf("Error: Ungetch buffer overflow");
}
int getch(void){
return ( (bufp==0) ? getch(): buf[--bufp]);
}
#include <ctype.h>
int getch(void);
int getop(char *s){
int c;
while( (c=s[0]=getch()) == " " || c == '\t');
s[1] = '\0';
if ( !isdigit(c) && c != '.'){
return c; /** c is not a number **/
if ( isdigit(c))
while ( isdigit(*s++=c=getch()));
if ( c == '.')
while ( isdigit(*s++=c=getch()));
*s='\0';
if ( c != EOF)
ungetch(c);
return NUMBER;
}
return 0;
}
most of the day, and cannot understand what is going on. The problems
are (1) it does not work!! (but exits normally on compilation...and
(2) when stepped through ( I use Xcode) it crashes without any error
message, at the indicated function. I have dissected it, run
individual parts on their own...which seem to work fine. Thanks in
advance.
I have supplied 3 command line arguments ( 22.3, 11.9, and '+' so the
argc is 4).
I am including the full code, as there are probably a lot I am missing
and messing up.
#include <stdio.h>
#include <math.h>
#define MAXOP 100
#define NUMBER 0
int getop(char *);
void ungets( char *);
void push(double);
double pop( void);
int main ( int argc, char *argv[]){
char s[MAXOP];
double op2;
while ( --argc > 0){
ungets(" ");
ungets(*++argv);
switch(getop(s)){
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
default:
printf("Error: Unknown input");
break;
}
}
printf("\t %.8g", pop());
return 0;
}
/*********/
#define MAXSTACK 50
double static stack[MAXSTACK];
int static stkp=0;
double pop(void){
if ( stkp > 0)
return stack[--stkp];
else{
printf("Error: Stack empty");
return 0.00;
}
}
void push(double i){
if (stkp < MAXSTACK)
stack[stkp++]=i;
else
printf("Error: Stack overflow");
}
/**********/
#include <string.h>
void ungetch( int );
void ungets( char *s){ /*** I think the problem is here ***/
int i;
i=strlen(s);
while ( i > 0)
ungetch(s[--i]);
}
/**********/
# define MAXBUF 25
char buf[MAXBUF];
static int bufp=0;
void ungetch( int c ){
if (bufp < MAXBUF)
buf[bufp++]=c;
else
printf("Error: Ungetch buffer overflow");
}
int getch(void){
return ( (bufp==0) ? getch(): buf[--bufp]);
}
#include <ctype.h>
int getch(void);
int getop(char *s){
int c;
while( (c=s[0]=getch()) == " " || c == '\t');
s[1] = '\0';
if ( !isdigit(c) && c != '.'){
return c; /** c is not a number **/
if ( isdigit(c))
while ( isdigit(*s++=c=getch()));
if ( c == '.')
while ( isdigit(*s++=c=getch()));
*s='\0';
if ( c != EOF)
ungetch(c);
return NUMBER;
}
return 0;
}