This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
why does this work with gcc and not borland's compiler
- To: help-gcc at gnu dot org
- Subject: why does this work with gcc and not borland's compiler
- From: "Bruce Fisher" <bfisher at tatca dot tc dot faa dot gov>
- Date: Thu, 7 Oct 1999 12:30:16 -0400
- Newsgroups: gnu.gcc.help
- Organization: FAA Technical Center, Pomona, NJ
- Xref: wodc7nx0 gnu.gcc.help:1309
my problem is this:
i have a data structures class this semester and ive written code for our
second assignment. i wrote it and compiled it in linux with gcc. ( gcc -lm
data2.c ) my professor wants to see my code compile and then run in class
which means i have to compile it in turbo c++. i thought that if it ran
perfectly with gcc why wouldnt it run with turbo c++?
it doesnt. it will compile but when it runs a window pops up that gives me a
stack error.
is there anyone who can look at this code for me and help me out.
id appreciate it.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define STACKSIZE 512
int findchar(char *, char);
char makeitwork(char *);
void split(char *, char*);
double calculate(char *);
struct stack
{
int top;
double items[STACKSIZE];
};
void stackinit(struct stack *);
int empty(struct stack *);
double pop(struct stack *);
void push(struct stack *, double);
double oper(char, double, double);
double output(double);
struct stack s;
int main(void)
{
char expr[100];
double final_answer;
printf("Enter the expression ending with a blank: ");
gets(expr); /* Get user input */
makeitwork(expr);
final_answer = pop(&s);
printf("\n\nTHE FINAL ANSWER IS: %10.2f\n\n", final_answer); /* Print
final answer */
}
/* This function finds the space between the tokens */
int findchar(char *str, char let)
{
int i = 0, found = 0;
while(*(str+i) != '\0' && !found)
if (*(str+i) == let)
found = 1;
else
i++;
if(!found)
i = -1;
return i;
}
/* This function splits the string at the spaces */
void split(char *str, char *op)
{
int pos;
pos = findchar(str, ' ');
if(pos >= 0)
{
strncpy(op, str, pos);
*(op+pos) = '\0';
strcpy(str, str+pos+1);
}
}
/* This function calls for the string to be split and the calls for the
calculation function */
char makeitwork(char *str)
{
char operand[12];
while(strlen(str) != 0)
split(str, operand);
calculate(operand);
}
}
/* Pushes tokens onto the stack */
void push(struct stack *ps, double value)
{
if(ps->top == STACKSIZE -1)
printf("\nSTACK OVERFLOW");
else
ps->items [++ps->top] = value;
return;
}
void stackinit(struct stack *ps)
{
ps->top = -1;
return;
}
/* Checks if the stack is empty */
int empty(struct stack *ps)
{
if(ps->top == -1)
return(TRUE);
else
return(FALSE);
}
/* Pops item from stack */
double pop(struct stack *ps)
{
if(empty(ps))
printf("\nSTACK UNDERFLOW");
return(ps->items[ps->top--]);
}
/* This function decides what arithmatic to do */
double oper(char symb, double op1, double op2)
{
switch(symb)
{
case '+': return(op1 + op2);
case '-': return(op1 - op2);
case '*': return(op1 * op2);
case '/': return(op1 / op2);
case '$': return(pow(op1,op2));
default: printf("\nillegal operation");
}
}
/* This function calculates the final answer */
double calculate(char *operand)
{
int i;
double amount, answer;
double opnd1, opnd2;
/* if operand is not a operator */
if(strcmp(operand, "-") && strcmp(operand, "+") && strcmp(operand, "*")
&& strcmp(operand, "/") && strcmp(operand, "$"))
{
amount = atof(operand); /* Convert operand to a float value */
push(&s, amount);
}
else
{
opnd2 = pop(&s);
opnd1 = pop(&s);
answer = oper(*operand, opnd1, opnd2);
push(&s, answer);
}
}