c - calculater by using reverse polish notation and using a stack -


hello have segmentation fault ,can please? if have operater "3 5 +" mean 3+5 , "9 8 * 5 + 4 + sin", "sin(((9*8)+5)+4)" idea check if first , second numbers , push theem in stack when have operator pop numbers , make calculation push answer again. `

typedef struct st_node {     float val;     struct st_node *next; } t_node;  typedef t_node t_stack;   // function allocate memory stack , returns stack t_stack* fnewcell() {     t_stack* ret;     ret = (t_stack*) malloc(sizeof(t_stack));     return ret; } // function allocate memory stack, fills value v , pointer n ,    , returns stack t_stack* fnewcellfilled(float v, t_stack* n) {     t_stack* ret;     ret = fnewcell();     ret->val = v;     ret->next =n;     return ret; }  //function initialize stack void initstack(t_stack** stack) {     fnewcellfilled(0,null); }  // add new cell void insrthead(t_stack** head,float val) {     *head = fnewcellfilled(val,*head); }  //function push value v stack s  void push(t_stack **s, float val) {     insrthead(s,val); }  //function pop value stack , returns int pop(t_stack **s) {     t_stack* tmp;     int ret;     tmp = (*s)->next;     ret = (*s)->val;     free(*s);     (*s) = tmp;     return ret; }  int isempty (t_stack *t) {     return t == null; }   //function transfer string(str) int (value) //returns -1 when success , otherwise int str2int(char *str,int *value) {     int i;     *value = 0;     int sign=(str[0]=='-' ? -1 : 1);     for(i=(str[0]=='-' ? 1 : 0);str[i]!=0;i++) {         if(!(str[i]>=48 && str[i]<=57)) // ascii char 0 9             return i;         *value= *value*10+(str[i]-48);     }     *value = *value * sign;     return -1; }  //a function takes string, transfer integer , make operation using stack void function(t_stack *stack, char *str) {     char x[10]=" ";     int y,j,i=0,z;     printf("++\n");     if(str[i] != '\0') {         strcpy(x, strtok(str, " "));         z= str2int(x, &y);         if(z == -1)          {             push(&stack,y);             i=i+2;         }     }      while(str[i] != '\0')     {         strcpy(x, strtok(null, " "));         z= str2int(x, &y);         if(z == -1)          {             printf("yes %d",y);             push(&stack,y);             i=i+2;           }         else          {              y=pop(&stack);             j=pop(&stack);              if(x[0] == '+' ) push(&stack,y+j);                 else if (x[0] == '-' ) push(&stack,j-y);                 else if(x[0] == '*' ) push(&stack,j*y);                 else if(x[0] == '/') push (&stack ,j/y);         }      } }   int main() {     t_stack *s;     initstack(&s);     char *str="3 5 +";         function(s,str);     return 0; } 

`

where noticed:

1)

void initstack(t_stack** stack) {     fnewcellfilled(0,null); } 

probably

void initstack(t_stack** stack) {     *stack=fnewcellfilled(0,null); } 

2)

string literal changed strtok

char *str="3 5 +"; 

should be

char str[]="3 5 +";     

3)

while(str[i] != '\0') {     strcpy(x, strtok(null, " "));     z= str2int(x, &y);     if(z == -1)      {         printf("yes %d",y);         push(&stack,y);         i=i+2;       }     else      {          y=pop(&stack);         j=pop(&stack);         if(x[0] == '+' ) push(&stack,y+j);         else if(x[0] == '-' ) push(&stack,j-y);         else if(x[0] == '*' ) push(&stack,j*y);         else if(x[0] == '/') push (&stack ,j/y);     }  

probably

while(str[i] != '\0') {     strcpy(x, strtok(null, " "));     z= str2int(x, &y);     if(z == -1)      {         printf("yes %d",y);         push(&stack,y);         i=i+2;       }     else      {          y=pop(&stack);         j=pop(&stack);         if(x[0] == '+' ) push(&stack,y+j);         else if(x[0] == '-' ) push(&stack,j-y);         else if(x[0] == '*' ) push(&stack,j*y);         else if(x[0] == '/') push (&stack ,j/y);         += 1;//need increment     }  

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -