I need help to convert from infix to postfix in C - c

I was practising some data structures problems that I did previously but this time I don't know what is going wrong in my code. I looked over a long time but I did not found the mistake. When I'm printing I'm just getting the first character and it looks like e is not being updated. But I've written e++.
#include<stdio.h>
#include "ctype.h"
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int priorityof(char x)
{
if(x=='(')
return 3;
else if(x=='+'|| x=='-')
return 1;
else if(x=='*'|| x=='/')
return 2;
}
int main()
{
char exp[20];
char *e;
e=exp;char x;
scanf("%c",exp);
while(*e!='\0')
{
if(isalnum(*e))
{
printf("%c", *e);
}
else if(*e=='(')
{
push(*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else {
while (priorityof(stack[top]) >= priorityof(*e)) {
printf("%c", pop());
push(*e);
}
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
}

%c is for single character and reading your question it seems like you are giving more than one character so its a string, use %s.
#include<stdio.h>
#include "ctype.h"
int stack[20]; int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() { return stack[top--]; }
int priorityof(char x) {
if(x=='(') return 3;
else if(x=='+'|| x=='-') return 1;
else if(x=='*'|| x=='/') return 2;
}
int main() {
char exp[20];
char *e;
e=exp;char x;
scanf("%s",exp);
while(*e!='\0') { if(isalnum(*e)) { printf("%c", *e); } else if(*e=='(') { push(*e); } else if(*e==')') { while((x=pop())!='(') printf("%c",x); } else { while (priorityof(stack[top]) >= priorityof(*e)) { printf("%c", pop()); push(*e); } } e++; } while(top!=-1) { printf("%c",pop()); } }

Related

Infix to Postfix program doesn't work as intended

This is my attempt to write a program that can convert any expression in infix to postfix format. I assign -1 to the top to indicate the stack is empty. When push, the top is incremented, when pop, the top is decremented. However, when I type in a+b, the output only gives me ab without the + operator, while when I type (a+b), it says segmentation fault. I reckon there's something wrong with my stack, but couldn't figure out what went wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define SIZE 30
typedef struct Stack
{
int top;
int capacity;
char* storage;
} stack;
int isEmpty(stack* a);
char topelement(stack* a);
char pop(stack* a);
void push(stack* a,char b);
bool isOperand(char a);
int Precedence(char a);
stack* NewStack(char* a);
void InfixPostfix(char* a);
int main(void)
{
char expression[SIZE];
printf("Please enter an expression:");
scanf("%s",expression);
InfixPostfix(expression);
printf("\n");
}
int isEmpty(stack* a)
{
if(a->top==-1)
{
return 1;
}
else
return 0;
}
char topelement(stack* a)
{
return a->storage[a->top];
}
char pop(stack* a)
{
if(isEmpty(a)==1)
{
printf("Stack is Empty\n");
return '$';
}
else
return a->storage[a->top];
--(a->top);
}
void push(stack* a,char b)
{
++(a->top);
a->storage[a->top]=b;
}
bool isOperand(char a)
{
if ( (a >= 'a' && a<= 'z') ||(a>='A' && a<='Z'))
{
return 1;
}
else
return 0;
}
int Precedence(char a)
{
if(a=='+' || a=='-')
{
return 1;
}
if(a=='*' || a=='/')
{
return 2;
}
if(a=='^')
{
return 3;
}
else
return -1;
}
stack* NewStack(char* a)
{
stack* b= malloc(sizeof(stack));
if(b!=NULL)
{
b->top=-1;
b->storage=malloc((strlen(a))*sizeof(char));
return b;
}
else
return NULL;
}
void InfixPostfix(char* a)
{
int i; int j=-1;
stack* b=NewStack(a);
if(b!=NULL)
{
for(i=0; i<strlen(a) ;i++)
{
if(isOperand(a[i]))
{
a[++j]=a[i];
}
if(a[i]=='(')
{
push(b, a[i]);
}
if(a[i]==')')
{
while(isEmpty(b)==0 && topelement(b)!= '(')
{
a[++j]= pop(b);
}
}
else
{
while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
{
a[++j]=pop(b);
push(b,a[i]);
}
}
}
while(isEmpty(b)==0)
{
a[++j]=pop(b);
}
a[++j]='\0';
printf("%s",a);
}
}
Besides the already suggested malloc correction, there are a few more to make.
In pop() there's
return a->storage[a->top];
--(a->top);
where the last code line isn't reached; change that to return a->storage[a->top--];
In InfixPostfix(), else are missing before if(a[i]=='(') and if(a[i]==')').
In InfixPostfix(), after the loop
while(isEmpty(b)==0 && topelement(b)!= '(')
{
a[++j]= pop(b);
}
a pop(b); is missing - the element '(' must as well be removed from the stack.
In InfixPostfix(), the push(b,a[i]); must be removed from the loop
while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
{
a[++j]=pop(b);
push(b,a[i]);
}
and placed after that loop - the operator in a[i] has to be put onto the stack only once.

Why is the following code showing segmentation fault whenever I input parantheses in the expression?

The code:
#include<stdio.h>
#include<ctype.h>
#define MAX 100
typedef struct stack
{
char data[MAX];
int top;
}stack;
void push(stack *s, char c)
{
s->top++;
if(s->top>MAX)
{
printf("Stack Overflow\n");
return;
}
s->data[s->top]=c;
}
int isEmpty(stack *s)
{
if(s->top==-1)
return 0;
else
return 1;
}
int priority(char c)
{
if(c=='(' || c==')')
return 0;
else if(c=='+' || c=='-')
return 1;
else if(c=='*' || c=='/')
return 2;
else if(c=='^')
return 3;
}
void pop(stack *s)
{
printf("%c ", s->data[s->top]);
s->top--;
}
void infixToPostfix(stack *s)
{
int c;
printf("Enter an expression\n");
while((c=getchar()) != '\n')
{
if(isalnum(c))
printf("%c ", c);
else
{
if(c=='(')
push(s, c);
else if(c==')')
{
while(c != '(')
pop(s);
pop(s);
}
else
{
while((priority(c) <= priority(s->data[s->top])) && isEmpty(s))
{
pop(s);
}
push(s, c);
}
}
}
while(s->top)
{
pop(s);
}
pop(s);
}
int main(void)
{
stack s;
s.top=-1;
infixToPostfix(&s);
return 0;
}
For some odd reason, whenever there is a parantheses in the input expression, I get a segmentation fault.
My aim was to convert an infix expression to a postfix expression. I was trying to implement it using a stack.
Is it because I am passing the stack from a called function to other functions?
else if(c==')')
{
while(c != '(')
pop(s);
pop(s);
}
If c is a ), it can't be a ( until you change its value. Inside that while loop, you don't change its value. So that will keep calling pop forever.
void pop(stack *s)
{
printf("%c ", s->data[s->top]);
s->top--;
}
This function has no safeties. If you pop when the stack is empty, it will read outside the bounds of s->data. So calling pop in an endless loop is a disaster.

Run time error in C for prime generator

I tried to solve a prime number generator problem in spoj.com. I solved it and it runs perfectly on my machine. But after submitting the solution to online, it shows run time error. Please help me!
Could anyone take a look into my code and tell where the problem is?
#include <stdio.h>
int primeFactor[350],w=0,z,i;
main()
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
}
primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
Your main function may be returning some indeterminate value because it doesn't have any return statement and the judge may be treating it as Runtime Error.
You will have to return 0 from the main function.
Also, you shouldn't omit the return type of each functions.
Try this:
#include <stdio.h>
int primeFactor[350],w=0,z,i;
int main(void)
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
return 0;
}
int primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
int primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}

Runtime Error(SIGSEGV) in codechef

I am solving a problem https://www.codechef.com/problems/DCGAME/
Inside this, I am not able to find the reason behind getting Runtime Error(SIGSEGV). On my PC, outputs are fine. I know the meaning of this error but am still not able to find where I am making the mistake. My code is below:
#include <stdio.h>
void calc(long long int arr1[],int n,long long int arr2[][n],long long int game[],int m)
{
//printf("Pre process %d %d",n,m);
int i,j;
for(i=0;i<n;i++){game[arr1[i]]++;}
for(i=1;i<n;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(arr2[i-1][j]>arr1[i+j])
arr2[i][j]=arr2[i-1][j];
else
arr2[i][j]=arr1[i+j];
game[arr2[i][j]]++;
}
}
//for(i=0;i<=m;i++){printf(" %d ",game[i]);}
}
int main(){
int n,m,i,count=0,k;
char c,p;
scanf("%d %d",&n,&m);
long long int a[n],b[n][n],max=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>max)max=a[i];
b[0][i]=a[i];
}
long long int game[max+1];
for(i=0;i<=max;i++)
game[i]=0;
calc(a,n,b,game,max);
while(m--)
{
// printf("WHILE-M");
c=getchar();
//scanf("%c",&c);
scanf("%c %d %c",&c,&k,&p);
switch(c)
{//printf("SWITCH");
case '<':
//printf("CASE <");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=1;i<k;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '>':
//printf("CASE >");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=k+1;i<=max;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '=':
//printf("CASE =");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
count+=game[k];
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
}
}
return 0;
}
If your algorithm is otherwise correct (I didn't check) but you are running out of stack space, then a short fix is to change long long int a[n],b[n][n],max=0; to:
long long int *a, (*b)[n], max = 0;
a = malloc(n * sizeof *a);
b = malloc(n * sizeof *b);
and also change long long int game[max+1]; for(i=0;i<=max;i++) game[i]=0; to:
long long int *game = calloc(max+1, sizeof *game);
Also you should check for failure, e.g. if ( !a || !b ) exit(EXIT_FAILURE);. The rest of the code can remain unchanged.

Is this program to get the mininmum value in a stack of O(1) complexity?

I have implemented push pop and get minimum in O(1) complexity. I have seen many solutions in C++. This is an implementation in C itself. Is the following program correct?
#include <stdio.h>
#include <stdlib.h>
int stack[15],aux[15];
int top=-1,count=-1,aux_count=-1,temp_aux=-1;
void push_auxilary(int ele)
{
aux[++aux_count] = ele;
}
void push_stack(int ele)
{
stack[++top]=ele;
}
void push(int ele)
{
if(top < 0 && aux_count < 0)
{
push_auxilary(ele);
push_stack(ele);
}
else
{
if(ele > aux[aux_count])
{
push_auxilary(aux[aux_count]);
push_stack(ele);
}
else
{
push_stack(ele);
push_auxilary(ele);
}
}
}
int pop_stack()
{
return stack[top--];
}
int pop_auxilary()
{
return aux[aux_count--];
}
int pop()
{
int a = pop_stack();
pop_auxilary();
return a;
}
void display()
{
for (int i = top; i >= 0; i--)
{
printf("%d\n",stack[i]);
/* code */
}
}
int get_min()
{
return aux[aux_count];
}
int main(int argc, char const *argv[])
{
int i=0;
push(5);
push(9);
push(1);
push(6);
push(1);
push(54);
push(34);
push(9);
push(3);
push(4);
push(7);
push(12);
push(02);
printf("the %d\n",get_min() );
for (i = aux_count; i >= 0; i--)
{
printf("%d\n",aux[i]);
}
return 0;
}
Looks like it gets the job done in O(1) indeed. Algorithmic-ally correct, but terrible from code reusage point of view.

Resources