Advertisement
lodha1503

Untitled

Jan 24th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct node{
  5.     char n[500];
  6.     struct node *next;
  7. };
  8.  
  9.  
  10. struct node* push(struct node * head,char val[])
  11. {
  12.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  13.     strcpy(temp->n,val);
  14.     if(head==NULL)
  15.     {
  16.         temp->next=NULL;
  17.         return temp;
  18.     }
  19.    
  20.     temp->next=head;
  21.     return temp;
  22. }
  23.  
  24.  
  25. int operator(char ch[])
  26. {
  27.     if (*ch=='+' || *ch=='-' || *ch=='/' || *ch=='*')
  28.     {
  29.         return 1;
  30.     }
  31.     else if ( *ch=='^')
  32.     {
  33.         return 2;
  34.     }
  35.     else
  36.     {
  37.         return 0;
  38.     }
  39. }
  40. float conversion(float a,float b,char operator){
  41.     if(operator=='^')
  42.     {
  43.         float value=a*b;
  44.         return value;
  45.     }
  46.     else if(operator=='*')
  47.     {
  48.         float value=a*b;
  49.         return value;
  50.     }
  51.    
  52.     else if(operator=='-')
  53.     {
  54.         float value=a-b;
  55.         return value;
  56.     }
  57.     else if(operator=='/')
  58.     {
  59.         float value=a/b;
  60.         return value;
  61.     }
  62.     else if(operator=='+')
  63.     {
  64.         float value=a+b;
  65.         return value;
  66.     }
  67. }
  68. int main()
  69. {
  70.     struct node *exp=NULL;
  71.     struct node *head;
  72.     head=NULL;
  73.    
  74.     char Postfix[200];
  75.     scanf("%s",Postfix);
  76.    
  77.     int length=strlen(Postfix);
  78.     int i=0;
  79.     int j=0;
  80.     char str[200];
  81.     int a=0;
  82.     float b=0;
  83.    
  84.     while(Postfix[i]!='\0')
  85.     {
  86.  
  87.         if(Postfix[i]!=','  )
  88.         {
  89.            
  90.             str[j]=Postfix[i];
  91.             j++;
  92.            
  93.         }
  94.         if(Postfix[i]==',' || i==length-1)
  95.         {
  96.             str[j]='\0';
  97.             j=0;
  98.  
  99.            
  100.             if(operator(str)==0)
  101.             {
  102.                
  103.                 head=push(head,str);
  104.                
  105.             }
  106.             else if (operator(str)==1)  
  107.             {
  108.                 if(head!=NULL )
  109.                 {
  110.                    
  111.                     char val1[500];strcpy(val1,head->n);
  112.                     char val2[500];strcpy(val2,head->next->n);
  113.                     char x[500]="(";
  114.                     char y[500]=")";
  115.                    
  116.                     strcat(strcat(strcat(strcat(x,val2),str),val1),y);
  117.                     struct node* tempu=head->next->next;
  118.                     exp=push(exp,x);
  119.                     head=exp;
  120.                     head->next=tempu;
  121.  
  122.                 }
  123.                 else{
  124.                     printf("error");
  125.                     return 0;
  126.                 }
  127.                
  128.             }  
  129.             else if (operator(str)==2)  
  130.             {
  131.                 a = atoi(str);
  132.                 printf("%d",a);
  133.                 b=conversion(a,a,'^');
  134.                 if(head!=NULL )
  135.                 {
  136.                    
  137.                     char val1[500];strcpy(val1,head->n);
  138.                    
  139.                     char x[500]="(";
  140.                     char y[500]=")";
  141.                    
  142.                     strcat(strcat(strcat(x,val1),str),y);
  143.                    
  144.                     exp=push(exp,x);
  145.                     head=exp;
  146.                    
  147.  
  148.                 }
  149.                 else
  150.                 {
  151.                     printf("error");
  152.                     return 0;
  153.                 }
  154.                
  155.             }
  156.         }
  157.         i++;
  158.     }
  159.     struct node *t=exp;
  160.     while(t!=NULL)
  161.     {
  162.         printf("%s",t->n);
  163.         t=t->next;
  164.     }
  165.     printf("%f",b);
  166.    
  167.    
  168.     return 0;
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement