Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node{
- char n[500];
- struct node *next;
- };
- struct node* push(struct node * head,char val[])
- {
- struct node* temp=(struct node*)malloc(sizeof(struct node));
- strcpy(temp->n,val);
- if(head==NULL)
- {
- temp->next=NULL;
- return temp;
- }
- temp->next=head;
- return temp;
- }
- int operator(char ch[])
- {
- if (*ch=='+' || *ch=='-' || *ch=='/' || *ch=='*')
- {
- return 1;
- }
- else if ( *ch=='^')
- {
- return 2;
- }
- else
- {
- return 0;
- }
- }
- float conversion(float a,float b,char operator){
- if(operator=='^')
- {
- float value=a*b;
- return value;
- }
- else if(operator=='*')
- {
- float value=a*b;
- return value;
- }
- else if(operator=='-')
- {
- float value=a-b;
- return value;
- }
- else if(operator=='/')
- {
- float value=a/b;
- return value;
- }
- else if(operator=='+')
- {
- float value=a+b;
- return value;
- }
- }
- int main()
- {
- struct node *exp=NULL;
- struct node *head;
- head=NULL;
- char Postfix[200];
- scanf("%s",Postfix);
- int length=strlen(Postfix);
- int i=0;
- int j=0;
- char str[200];
- int a=0;
- float b=0;
- while(Postfix[i]!='\0')
- {
- if(Postfix[i]!=',' )
- {
- str[j]=Postfix[i];
- j++;
- }
- if(Postfix[i]==',' || i==length-1)
- {
- str[j]='\0';
- j=0;
- if(operator(str)==0)
- {
- head=push(head,str);
- }
- else if (operator(str)==1)
- {
- if(head!=NULL )
- {
- char val1[500];strcpy(val1,head->n);
- char val2[500];strcpy(val2,head->next->n);
- char x[500]="(";
- char y[500]=")";
- strcat(strcat(strcat(strcat(x,val2),str),val1),y);
- struct node* tempu=head->next->next;
- exp=push(exp,x);
- head=exp;
- head->next=tempu;
- }
- else{
- printf("error");
- return 0;
- }
- }
- else if (operator(str)==2)
- {
- a = atoi(str);
- printf("%d",a);
- b=conversion(a,a,'^');
- if(head!=NULL )
- {
- char val1[500];strcpy(val1,head->n);
- char x[500]="(";
- char y[500]=")";
- strcat(strcat(strcat(x,val1),str),y);
- exp=push(exp,x);
- head=exp;
- }
- else
- {
- printf("error");
- return 0;
- }
- }
- }
- i++;
- }
- struct node *t=exp;
- while(t!=NULL)
- {
- printf("%s",t->n);
- t=t->next;
- }
- printf("%f",b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement