0% found this document useful (0 votes)
82 views9 pages

Practical No: 5: Rajdeep Shivarkar BE19F06F060

The document describes 3 C programs: 1) A program that prints a star pattern. 2) A program that converts an infix expression to postfix notation. 3) A program that converts an infix expression to prefix notation using a stack. The programs are explained and code snippets are provided along with sample outputs. At the end, the document concludes that it has successfully demonstrated drawing patterns and converting expression notations in C programming on Linux.

Uploaded by

Vishal Birajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views9 pages

Practical No: 5: Rajdeep Shivarkar BE19F06F060

The document describes 3 C programs: 1) A program that prints a star pattern. 2) A program that converts an infix expression to postfix notation. 3) A program that converts an infix expression to prefix notation using a stack. The programs are explained and code snippets are provided along with sample outputs. At the end, the document concludes that it has successfully demonstrated drawing patterns and converting expression notations in C programming on Linux.

Uploaded by

Vishal Birajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Rajdeep Shivarkar BE19F06F060

PRACTICAL NO : 5

Aim : Write a c program to display pattern

1. C program to print Star Pattern


#include <stdio.h> int main() { for(int i=0;i<9;i++)
{
for(int j=0;j<13;j++)
{
if(i==2 || i==6|| i+j ==6 || j-i==6 || i-j==2 || i+j ==14)
{
printf("*");
} else{
printf(" ");
}
}
printf("\n");
}
return 0;
}

OUTPUT :

2.C program to convert infix expression to postfix expression


Rajdeep Shivarkar BE19F06F060

#include<stdio.h>
#include<ctype.h> char
stack[100]; int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{ if(top == -1) return -
1; else return
stack[top--];
}
int priority(char x)
{ if(x == '(') return 0;
if(x == '+' || x == '-')
return 1; if(x == '*' || x ==
'/') return 2; return 0;
}
Rajdeep Shivarkar BE19F06F060

int main()
{
char exp[100]; char *e, x;
printf("Enter the expression : ");
scanf("%s",exp); printf("\n"); e =
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(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
} e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}
Rajdeep Shivarkar BE19F06F060

OUTPUT :

3.C program to convert infix expression to prefix expression

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct
Stack { int top; int
maxSize; int* array;
};
struct Stack* create(int max)
Rajdeep Shivarkar BE19F06F060

{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct
Stack)); stack->maxSize = max; stack->top = -1;
stack->array = (int*)malloc(stack->maxSize * sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
if(stack->top == stack->maxSize - 1){
printf("Will not be able to push maxSize reached\n");
}
// Since array starts from 0, and maxSize starts from 1
return stack->top == stack->maxSize - 1;
}

int isEmpty(struct Stack* stack)


{
return stack->top == -1;
}
void push(struct Stack* stack, char item)
{
if (isFull(stack)) return;
stack->array[++stack->top] = item;
}
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
int peek(struct Stack* stack)
{
Rajdeep Shivarkar BE19F06F060

if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int checkIfOperand(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

int precedence(char ch)


{
switch (ch)
{
case '+': case '-':
return 1;

case '*': case '/':


return 2;

case '^': return 3;


}
return -1;
}
int getPostfix(char* expression)
{
int i, j;
struct Stack* stack = create(strlen(expression)); if(!
stack)
return -1 ;

for (i = 0, j = -1; expression[i]; ++i)


{
Rajdeep Shivarkar BE19F06F060

if (checkIfOperand(expression[i]))
expression[++j] = expression[i]; else if
(expression[i] == '(') push(stack,
expression[i]);
else if (expression[i] == ')')
{
while (!isEmpty(stack) && peek(stack) !=
'(') expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression else
pop(stack);
}
else // if an opertor
{
while (!isEmpty(stack) && precedence(expression[i]) <= precedence(peek(stack)))
expression[++j] = pop(stack);
push(stack, expression[i]);
}

}
while (!isEmpty(stack))
expression[++j] = pop(stack);

expression[++j] = '\0';

void reverse(char *exp){

int size = strlen(exp);


int j = size, i=0;
char temp[size];

temp[j--]='\0';
while(exp[i]!='\0')
Rajdeep Shivarkar BE19F06F060

{
temp[j] = exp[i];
j--; i++;
}
strcpy(exp,temp);
}
void brackets(char* exp){
int i = 0; while(exp[i]!='\0')
{
if(exp[i]=='(')
exp[i]=')'; else
if(exp[i]==')')
exp[i]='('; i++;
}
}
void InfixtoPrefix(char *exp)
{ int size = strlen(exp);
reverse(exp); brackets(exp);
getPostfix(exp); reverse(exp);
}

int main()
{
printf("The infix is: "); char expression[]
= "((a/b)+c)-(d+(e*f))"; printf("%s\
n",expression); InfixtoPrefix(expression);
printf("The prefix is: "); printf("%s\
n",expression);
return 0;
}

OUTPUT :
Rajdeep Shivarkar BE19F06F060

CONCLUSION : In this practical, I have successfully performed the practical of


how to draw patterns in c programing in Linux Operating System.

You might also like