Hi ,
Now i am fully lost in this application. I am debugging it for quite some time now.
The expression i am entering is
4$2*3-3+8/4/(1+1)
and the result shoudl be
+-*$4233//84+11
and what is happening is that it works fine ( i am able to get the output till 33//84+11) but after that something is going crazy of which i have no idea (or may be after so much time my mind has gone crazy)
The code for this is
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
char * xstrrev(char *s);
void initstack(struct infix *s);
void push(struct infix *s, char data);
char pop(struct infix *s);
void convert(char *s, char *target, int len);
struct infix
{
char arr[MAX]; // stack to hold elements
int top; // store target string
};
struct infix in;
char arrrev[MAX];
int main(void)
{
char arr[MAX];
char *t, *source;
int count, i;
fputs("\nPlease enter an arith exp in infix form\n", stdout);
fflush(stdout);
if(fgets(arr, sizeof arr, stdin) != NULL)
{
char *newline = strchr(arr, '\n');
if(newline != NULL)
{
*newline = '\0';
}
}
// initialize the stack infix
initstack(&in);
count = strlen(arr);
t = arr;
printf("\n%s", t);
// make t point to the last char
for(i = 0;i<count;i++)
t++;
// Reverse the string
source = xstrrev(arr);
convert(source, t, count);
printf("\nPrefix exp %s", t);
return 0;
}
void convert(char *s, char *target, int len)
{
int i;
char a, b;
char ........;
int pria, prib;
ss = s;
for(i = 0;i< len;i++)
{
a = ........;
if(a == ' ')
{
// do nothing
}
// check if a is digit or alphabet
else if((a >= '0' && a<= '9') || (a >= '65' && a<= '90') || (a >= '97' && a<= '121'))
{
*target = a;
--target;
}
else if(a == ')')
{
push(&in, a);
}
else if(a == '(')
{
b = pop(&in);
while(b != ')')
{
*target = b;
-- target;
b = pop(&in);
}
}
else if(a == '$'||a == '/'||a == '*'||a == '+'||a == '-')
{
b = pop(&in);
// check if stack is empty
if(b == -1)
push(&in, a);
else
{
pria = getpriority(a);
prib = getpriority(b);
if(prib < pria)
{
push(&in, b);
push(&in, a);
}
else
{
while(prib > pria)
{
*target = b;
--target;
++ss;
b = pop(&in);
pria = getpriority(a);
prib = getpriority(b);
}
if(b == -1)
push (&in,a);
else
{
push(&in, b);
push (&in,a);
}
}
}
}
++ss;
}
}
int getpriority(char a)
{
int pri;
switch (a)
{
case '$':
pri = 3;
break;
case '*':
pri = 2;
break;
case '/':
pri = 2;
break;
case '+':
pri = 1;
break;
case '-':
pri = 1;
break;
default:
pri = 0;
break;
}
return pri;
}
void initstack(struct infix *s)
{
s->top = -1;
}
void push(struct infix *s, char data)
{
// check if stack is full
if(s->top == MAX -1)
{
printf("\nStack is full");
}
// adding the element for the first time
else if(s->top == -1)
{
s->top++;
s->arr[s->top] = data;
}
else
{
if(s->top < MAX -1)
{
s->top++;
s->arr[s->top] = data;
}
}
}
char pop(struct infix *s)
{
char item;
// check if elements are there
if(s->top == -1)
{
printf("No more elements");
return -1;
}
else
{
item = s->arr[s->top];
s->top--;
return item;
}
}
char * xstrrev(char *s)
{
int len, i;
len = strlen(s);
while(*s != '\0')
++s;
s--;
for(i = 0;i< len; i++)
{
arrrev[i] = *s;
s--;
}
arrrev[i++] = '\0';
printf("\n%s", arrrev);
return arrrev;
}