Multiple Data Types in a stack in C -


so doing simple infix calculator. having trouble of storing char , double on 1 stack.

first attempted separate operands , operator seen in code below later on realize gone in big trouble.

i'm newbie in using unions should store char , double on 1 stack only?

here's code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h>   typedef union{     char c;     double d; } union;   typedef struct node{     union data;     struct node *link;  } node;   typedef struct stack{     node *top;  }stack;   void initialize_stack(stack *stack)  {     stack->top=null;  }    int is_stack_empty(stack *stack)  {     return(stack->top==null);  }   void push_c(stack *stack, char x)  {     node *node=(node*)malloc(sizeof(node));      if(node==null)     {         printf("sorry no enough memory\n");     }     else     {         node->data.c=x;         node->link=null;         if(is_stack_empty(stack))         {             stack->top=node;         }         else         {             node->link=stack->top;             stack->top=node;         }            }  }  void push_d(stack *stack, double x) {     node *node=(node*)malloc(sizeof(node));     if(node==null)    {        printf("sorry no enough memory\n");    }    else    {         node->data.d=x;         node->link=null;         if(is_stack_empty(stack))         {             stack->top=node;         }         else         {             node->link=stack->top;             stack->top=node;         }            }  }  void pop(stack *stack) {     node *runner=stack->top;     if(is_stack_empty(stack))     {          printf("stack empty.\n");     }     else     {         stack->top=stack->top->link;         free(runner);      }  }    void print_stack_c(stack *stack) {     node *runner=stack->top;     if(is_stack_empty(stack))     {         printf("stack empty.\n");     }      else     {         while(runner!=null)        {             printf("%c,",runner->data.c);             runner=runner->link;         }          printf("\n");      }  }   void print_stack_d(stack *stack)  {      node *runner=stack->top;      if(is_stack_empty(stack))      {          printf("stack empty.\n");       }      else      {          while(runner!=null)          {              printf("%.4f,",runner->data.d);              runner=runner->link;           }          printf("\n");      }  }   int main(){     char input[150],output[150];     int i;     double j;     char a[20]="";     char b[20]="";     char c[10],d[10]="",e[10]="";     struct stack operand;     struct stack operator;     struct stack tempstack;        initialize_stack(&tempstack);     initialize_stack(&operand);     initialize_stack(&operator);      scanf("%s",input); //store input string        //convert infix postfix     for(i=0;i<strlen(input)-1;i++){      if(input[i]==' '){         continue;     }     else if(input[i]=='('){          push_c(&tempstack,input[i]);     }      else if(isdigit((unsigned char)input[i]) || input[i] == '.'){     strcpy(a,b);//clear     while(isdigit((unsigned char)input[i]) || input[i] == '.'){     strncat(a,&input[i],1);     i++;             }      j=atof(a);     push_d(&operand,j);     }      else{ //operand       }      }       print_stack_c(&tempstack);     print_stack_d(&operand);     } 

here 1 method:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <errno.h>  #define success 0  typedef enum datatypes_e     {     datatype_double,     datatype_char     } datatypes_t;  typedef union data_u    {    char   c;    double d;    } data_t;  typedef struct node_s    {    data_t         data;    datatypes_t    datatype;    struct node_s *link;    } node_t;  typedef struct stack_s    {    node_t *top;    } stack_t;  int stackpushchar(stack_t *stack, char x)    {    int     rcode=success;    node_t *node;     errno=0;    node=malloc(sizeof(node_t));    if(!node)       {       rcode=errno;       goto cleanup;       }     node->datatype = datatype_char;    node->data.c=x;    node->link=null;    if(stack->top)       {       node->link=stack->top;       stack->top=node;       }    else       stack->top=node;  cleanup:     return(rcode);    }  int stackpushdouble(stack_t *stack, double x)    {    int     rcode=success;    node_t *node;     errno=0;    node=malloc(sizeof(node_t));    if(!node)       {       rcode=errno;       goto cleanup;       }     node->datatype =  datatype_double;    node->data.d=x;    node->link=null;    if(stack->top)       {       node->link=stack->top;       stack->top=node;       }    else       stack->top=node;  cleanup:     return(rcode);    }   void stackprint(stack_t *stack)    {    node_t *runner=stack->top;    if(!stack->top)       {       printf("stack empty.\n");       goto cleanup;       }     while(runner)       {       switch(runner->datatype)          {          case datatype_double:             printf("%.4f,",runner->data.d);             break;           case datatype_char:             printf("%c,", runner->data.c);             break;          }        runner=runner->link;       }     printf("\n");  cleanup:     return;    }  int main()    {    int     rcode   = success;    stack_t stack =       {       .top=null       };     rcode=stackpushdouble(&stack, 3.1415926535);    if(rcode)       {       fprintf(stderr, "stackpushdouble() reports %d %s\n", rcode, strerror(rcode));       goto cleanup;       }     rcode=stackpushchar(&stack, '+');    if(rcode)       {       fprintf(stderr, "stackpushchar() reports %d %s\n", rcode, strerror(rcode));       goto cleanup;       }     rcode=stackpushdouble(&stack, 6.02e23);    if(rcode)       {       fprintf(stderr, "stackpushdouble() reports %d %s\n", rcode, strerror(rcode));       goto cleanup;       }     rcode=stackpushchar(&stack, '=');    if(rcode)       {       fprintf(stderr, "stackpushchar() reports %d %s\n", rcode, strerror(rcode));       goto cleanup;       }      stackprint(&stack);  cleanup:      return(rcode);     } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -