7 Replies - 1742 Views - Last Post: 30 April 2011 - 09:12 PM Rate Topic: -----

#1 ayush_birla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 11

using stack as ADT for expression conversion

Posted 28 April 2011 - 04:55 AM

#include<stdio.h>
#include<conio.h>
#include"D:\TC\INCLUDE\ADT.H"
#include<string.h>

void in_to_pre(char in[],char pre[]);
void in_to_post(char in[],char post[]);
//int evaluate(char post[]);
int prec(char );
void main()
{
 char in[30],pre[30],post[30];
 int op;
 char c;
	printf("\nPlease enter infix expression:");
			scanf("%s",&in);
 do
 {
	printf("\nMenu:");
	printf("\n1)Infix to prefix\n2)Infix to postfix\n3)Evaluate");
	printf("\n4)Exit");
	printf("\nPlease enter your choice:");
	scanf("%d",&op);
	switch(op)
	{
		case 1:

			in_to_pre(in,pre);
			printf("\nPrefix expression is:");
			printf("%s",pre);
			break;
		case 2:
			printf("\nPostfix expression is:");
			in_to_post(in,post);
			printf("%s",post);
			break;
		case 3:
			//eve(post);
			break;
		case 4:
			exit(0);
			break;
	}
	printf("\nDo you wish to continue(y/n)??");
	flushall();
	scanf("%c",&c);
 }while(c=='y'||c=='Y');
 getch();
}

 void in_to_pre(char in[30],char pre[30])
 {
	char k;
	int i=0;
	in=strrev(in);
		for(i=0;in[i]!='\0';i++)
		{
			k=in[i];
			if(k=='(')
				in[i]=')';
			else
				if(k==')')
					in[i]='(';
		}
	in_to_post(in,pre);
	pre=strrev(pre);
	printf("\nExpression is:%s",pre);
 }

void in_to_post(char in[30],char post[30])
{
	int i=0,j=0;
	char k,x;
	stack *s;
	init(&s);
	for(i=0;in[i]!='\0';i++)
	{
		k=in[i];
		if(isalnum(k))
		{
			post[j++]=k;
		}
		else if(k=='(')
			push(&s,'(');
		else if(k==')')
		{
			while((x=pop(&s))!='(')
				post[j++]=x;
		}
		else
		{
			while(prec(k)<=prec(top(s))&&(!empty(s)))
				{
					x=pop(&s);
					post[j++]=x;
				}
				push(&s,k);
		}
	}
	while(!empty(s))
		{
			x=pop(&s);
			post[j++]=x;
		}
	post[j]='\0';
	printf("\nPost Expression is  %s:",post);


}

int prec(char x)
{
	if(x=='('||x==')')
		return(0);
	if(x=='+'||x=='-')
		return(1);
	if(x=='*'||x=='/'||x=='%')
		return(2);
	if(x=='^')
		return(3);
	else
		return(4);
}


This is main code for expression convergance i.e from infix to postfix or prefix & ADT of stack is as follows...
#include<stdio.h>
#include<conio.h>

typedef struct stack
{
 int data;
 struct stack *next;
}stack;

void init(stack**s)
{
	*s=NULL;
}

int empty(stack *s)
{
	if(s==NULL)
		return(1);
	return 0;

}

void push(stack **s,int x)
{
	stack *p,*q;
	p=(stack*)malloc(sizeof(stack));
	p->data=x;
		p->next=*s;
		*s=p->next;
}

int pop(stack**s)
{
	stack *p;
	int x;
	p=*s;
	*s=p->next;
	x=p->data;
	free(p);
return(x);
}

int top(stack *s)
{
	return(s->data);

}





I ran the code lot of times but it is still not printing arithmetic signs such as +,-,* etc... I hand-run the code.. found no logical error; if someone have suggession ; then they are most welcome...
Thank you..

Is This A Good Question/Topic? 0
  • +

Replies To: using stack as ADT for expression conversion

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: using stack as ADT for expression conversion

Posted 28 April 2011 - 07:00 AM

My first suggestion would be to get a newer standard compliant compiler. The Turbo C compilers are at least 20 years old and use many obsolete, non-standard header files and functions.

Next because of these non-standard headers most people will not be able to compile your files.

Quote

main.c|2|error: conio.h: No such file or directory|
main.c|10|warning: no previous declaration for ‘init’|
main.c|15|warning: no previous declaration for ‘empty’|
main.c|23|warning: no previous declaration for ‘push’|
main.c||In function ‘push’:|
main.c|26|error: implicit declaration of function ‘malloc’|
main.c|26|warning: incompatible implicit declaration of built-in function ‘malloc’|
main.c|25|warning: unused variable ‘q’|
main.c|32|warning: no previous declaration for ‘pop’|
main.c||In function ‘pop’:|
main.c|39|error: implicit declaration of function ‘free’|
main.c|39|warning: incompatible implicit declaration of built-in function ‘free’|
main.c|43|warning: no previous declaration for ‘top’|
main.c|53|error: D:\TC\INCLUDE\ADT.H: No such file or directory|
main.c|60|error: return type of ‘main’ is not ‘int’|
main.c||In function ‘main’:|
main.c|66|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[30]’|
main.c|91|error: implicit declaration of function ‘exit’|
main.c|91|warning: incompatible implicit declaration of built-in function ‘exit’|
main.c|74|warning: switch missing default case|
main.c|95|error: implicit declaration of function ‘flushall’|
main.c|98|error: implicit declaration of function ‘getch’|
main.c||In function ‘in_to_pre’:|
main.c|105|error: implicit declaration of function ‘strrev’|
main.c|105|error: assignment makes pointer from integer without a cast|
main.c|116|error: assignment makes pointer from integer without a cast|
main.c||In function ‘in_to_post’:|
main.c|129|error: implicit declaration of function ‘isalnum’|
||=== Build finished: 12 errors, 11 warnings ===|


Take special note of the error:

Quote

main.c|26|error: implicit declaration of function ‘malloc’|
main.c|26|warning: incompatible implicit declaration of built-in function ‘malloc’|


To use malloc you need to include the stdlib.h header file. If you don't include this header your calls to malloc will probably fail but you may never know.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 ayush_birla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 11

Re: using stack as ADT for expression conversion

Posted 28 April 2011 - 10:53 AM

then which software should I use??
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: using stack as ADT for expression conversion

Posted 28 April 2011 - 11:22 AM

Well there are several good C/C++ compilers available free to download. There is a a listing of several good choices in the FAQ that is pinned at the top of this forum.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: using stack as ADT for expression conversion

Posted 28 April 2011 - 11:28 AM

I recommend CodeBlocks IDE. This download includes the MinGW (GCC) compiler.
Was This Post Helpful? 0
  • +
  • -

#6 ayush_birla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 11

Re: using stack as ADT for expression conversion

Posted 30 April 2011 - 08:58 PM

instead of using gcc it is still not get executed properly.... plz give some definite solution... thanx
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: using stack as ADT for expression conversion

Posted 30 April 2011 - 09:01 PM

Post your current code along with the complete error/warning messages just as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: using stack as ADT for expression conversion

Posted 30 April 2011 - 09:12 PM

To begin with, you aren't sending the infix expression to your other functions, because this is wrong
    printf("\nPlease enter infix expression:");
    scanf("%s",&in);


in is already type char* so it should be
    printf("\nPlease enter infix expression:");
    scanf("%s",in);



(Ignore the "<b></b>" nonsense -- that's just a bug in the forum.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1