help me to knpow what is wrong with this

SoccerBoss
Category:
Programming
Price: $25 USD

Question description

#include <stdio.h>

#include <string.h>

struct Node;

typedef Node *PtrToNode;

struct Node 

{

char element;

PtrToNode Next;

};

void push(PtrToNode L, char x)

{

PtrToNode p=new Node;

p->element=x;

p->Next=L->Next;

L->Next=p;

}

char pop(PtrToNode L)

{

PtrToNode q=L->Next;

int x=q->element;

L->Next=L->Next->Next;

delete q;

return x;

}

char top(PtrToNode L)

{

PtrToNode q=L->Next;

int x=q->element;

return x;

}

bool isEmpty(PtrToNode L)

{

if(L-> element== NULL)

return true;

else 

return false;

}

int Priority(char p)

{

switch(p)

{

case'+': return 1;

case'-': return 1;

case'*': return 2;

case'/': return 2;

case'%': return 2;

}

}

int EvalExpression(char c[])

{

PtrToNode s=new Node;

s->Next=NULL;

int len=strlen(c);

for(int i=0;i<len;i++)

{

if(c[i]>='1'&&c[i]<='9')

{

    push(s,c[i]);

}

else

{

char y=pop(s)-48;

char x=pop(s)-48;

int z=0;

switch(c[i])

{

case'+': z=x+y;break;

case'-': z=x-y;break;

case'*': z=x*y;break;

case'/': z=x/y;break;

case'%': z=x%y;break;

}

char l=z+48;

push(s,l);

}

}

int r=pop(s)-48;

return(r);

}

bool check(char x[])

{

PtrToNode L=new Node;

L->Next=NULL;

for(int i=0;i<strlen(x);i++)

{

if((x[i]=='(')||(x[i]=='[')||(x[i]=='{'))

push(L,x[i]);

if((x[i]==')')||(x[i]==']')||(x[i]=='}'))

{

if(L->Next==NULL)

return false;

char c=pop(L);

if((x[i]==')')&&(c!='('))

return false;

if((x[i]=='}')&&(c!='{'))

return false;

if((x[i]==']')&&(c!='['))

return false;

}

}

if(L->Next!=NULL)

return false;

else return true;

}

char* intopost(char c[])

{

int len=strlen(c); // to get the length of the string c which is written in infix notation

char *b=new char [len];

int counter=0;

PtrToNode s=new Node;

s->Next=NULL;

for(int i=0;i<len;i++)

{

if(c[i]>='1'&&c[i]<='9')

{

     b[counter++]=c[i];

}

else

{

if(isEmpty(s))

{

push(s,c[i]);

 }

else if(Priority(c[i])>Priority(top(s)))

{

 push(s,c[i]);

}

else

{

while(isEmpty(s)==false)

       b[counter++]=pop(s);

 push(s,c[i]);

}

}

}

while(isEmpty(s)==false)

b[counter++]=pop(s);

b[counter]='\0'; 

return b;

}

void main()

{

char c[20];

printf("plz enter a mathematical in infix notation:\n");

scanf("%s",c);

char *b;

b=intopost(c);

printf("%s",b);

int x=EvalExpression(b);

printf("\n%d\n",x);

}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Cornell University
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1824 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors