Submission By: Kshitij Kumar Singh Chauhan

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

SUBMISSION BY

Kshitij Kumar Singh Chauhan


CSE, 201000022

This assignment has been divided into two sections for the convenience of reader.

SECTION 1
(Implementation and Testing)

SECTION 2
(Manuscript of Working)
SECTION 1
(Implementation and Testing)

CONTENTS
A. Readme
B. Q1
a. Source Code
b. Sample Output
C. Q2
a. Source Code
b. Sample Output
D. Q3
a. Source Code
b. Sample Output
E. Q4
a. Source Code
b. Sample Output

A. README
Language : CPP
OS Used For Testing : Ubuntu 18.04.5 LTS
Compiler Used For Testing : g++

Fig (i) : Compiler info


B. Q1
a. Source Code
#include<bits/stdc++.h>
using namespace std;

int Setting_operator_precedences(char c) {
if(c == '^')
return 3;
else if(c == '/' || c=='*')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}

bool my_is_operator(char c)
{
return (!isalpha(c) && !isdigit(c));
}

int getPriority(char C)
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}

string infixToPostfix(string infix)


{
infix = '(' + infix + ')';
int l = infix.size();
stack<char> char_stack;
string output;

for (int i = 0; i < l; i++) {

if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];

else if (infix[i] == '(')


char_stack.push('(');

else if (infix[i] == ')') {


while (char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
}

else
{
if (my_is_operator(char_stack.top()))
{
if(infix[i] == '^')
{
while (getPriority(infix[i]) <=
getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}
else
{
while (getPriority(infix[i]) <
getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}
char_stack.push(infix[i]);
}
}
}
while(!char_stack.empty()){
if(char_stack.top() == '(' || char_stack.top() == ')')
{
char_stack.pop();
continue;
}
output += char_stack.top();
char_stack.pop();
}
return output;
}

string infixToPrefix(string infix)


{
int l = infix.size();
reverse(infix.begin(), infix.end());
for (int i = 0; i < l; i++) {

if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}

string prefix = infixToPostfix(infix);


reverse(prefix.begin(), prefix.end());

return prefix;
}

int main() {
string expression;
cout<< "Enter infix expression : ";
cin>>expression;
cout<< "\nPostfix : " <<infixToPostfix(expression) << std::endl;
cout << "Prefix : " <<infixToPrefix(expression) << std::endl;

b. Sample Output

Fig. 1 : Sample Output Q1


C. Q2
a. Source Code
#include<bits/stdc++.h>
using namespace std;

int Setting_operator_precedences(char c) {
if(c == '^')
return 3;
else if(c == '/' || c=='*')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}

bool my_is_operator(char c)
{
return (!isalpha(c) && !isdigit(c));
}

int getPriority(char C)
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}

string infixToPostfix(string infix)


{
infix = '(' + infix + ')';
int l = infix.size();
stack<char> char_stack;
string output;

for (int i = 0; i < l; i++) {

if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];

else if (infix[i] == '(')


char_stack.push('(');

else if (infix[i] == ')') {


while (char_stack.top() != '(') {
output += char_stack.top();
char_stack.pop();
}

else
{
if (my_is_operator(char_stack.top()))
{
if(infix[i] == '^')
{
while (getPriority(infix[i]) <=
getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}
else
{
while (getPriority(infix[i]) <
getPriority(char_stack.top()))
{
output += char_stack.top();
char_stack.pop();
}

}
char_stack.push(infix[i]);
}
}
}
while(!char_stack.empty()){
if(char_stack.top() == '(' || char_stack.top() == ')')
{
char_stack.pop();
continue;
}
output += char_stack.top();
char_stack.pop();
}
return output;
}

string infixToPrefix(string infix)


{
int l = infix.size();
reverse(infix.begin(), infix.end());
for (int i = 0; i < l; i++) {

if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}

string prefix = infixToPostfix(infix);


reverse(prefix.begin(), prefix.end());

return prefix;
}

int main() {
string expression;
cout<< "Enter infix expression : ";
cin>>expression;
cout<< "\nPostfix : " <<infixToPostfix(expression) << std::endl;
cout << "Prefix : " <<infixToPrefix(expression) << std::endl;

b. Sample Output

Fig. 2 : Sample Output Q2


D. Q3

a. Source Code

#include <iostream>
#include <string.h>

using namespace std;

struct My_Stack
{
int top;
unsigned limiting_val;
int* arr;
};

struct My_Stack* Make_My_Stack( unsigned limiting_val )


{
struct My_Stack* My_Stack = (struct My_Stack*) malloc(sizeof(struct
My_Stack));

if (!My_Stack) return NULL;

My_Stack->top = -1;
My_Stack->limiting_val = limiting_val;
My_Stack->arr = (int*) malloc(My_Stack->limiting_val * sizeof(int));

if (!My_Stack->arr) return NULL;

return My_Stack;
}

int isEmpty(struct My_Stack* My_Stack)


{
return My_Stack->top == -1 ;
}

char peek(struct My_Stack* My_Stack)


{
return My_Stack->arr[My_Stack->top];
}
char pop(struct My_Stack* My_Stack)
{
if (!isEmpty(My_Stack))
return My_Stack->arr[My_Stack->top--] ;
return '$';
}

void push(struct My_Stack* My_Stack, char op)


{
My_Stack->arr[++My_Stack->top] = op;
}

int evaluatePostfix(char* exp)


{

struct My_Stack* My_Stack = Make_My_Stack(strlen(exp));


int i;

if (!My_Stack) return -1;

for (i = 0; exp[i]; ++i)


{

if (isdigit(exp[i]))
push(My_Stack, exp[i] - '0');

else
{
int val1 = pop(My_Stack);
int val2 = pop(My_Stack);
switch (exp[i])
{
case '+': push(My_Stack, val2 + val1); break;
case '-': push(My_Stack, val2 - val1); break;
case '*': push(My_Stack, val2 * val1); break;
case '/': push(My_Stack, val2/val1); break;
}
}
}
return pop(My_Stack);
}

int main()
{

char expression[100];
cout<<"Enter postfix expression : ";
cin>>expression;
cout<<"\npostfix evaluation: "<< evaluatePostfix(expression)<<"\n";
return 0;

b. Sample Output

Fig. 3 : Sample Output Q3


E. Q4

a. Source Code

#include <bits/stdc++.h>

using namespace std;

struct Node {

char data;
Node *left, *right;

};

Node* newNode(char data);

int search(char arr[], int strt, int end, char value);

Node* buildUtil(char in[], char post[], int inStrt,


int inEnd, int* pIndex)
{

if (inStrt > inEnd)


return NULL;

Node* node = newNode(post[*pIndex]);


(*pIndex)--;

if (inStrt == inEnd)
return node;

int iIndex = search(in, inStrt, inEnd, node->data);

node->right = buildUtil(in, post, iIndex + 1, inEnd, pIndex);


node->left = buildUtil(in, post, inStrt, iIndex - 1, pIndex);
return node;
}

Node* buildTree(char in[], char post[], int n)


{

int pIndex = n - 1;
return buildUtil(in, post, 0, n - 1, &pIndex);

int search(char arr[], int strt, int end, char value)


{

int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break;
}

return i;

Node* newNode(char data)


{

Node* node = (Node*)malloc(sizeof(Node));


node->data = data;
node->left = node->right = NULL;
return (node);

void preOrder(Node* node)


{

if (node == NULL)
return;
printf("%c ", node->data);
preOrder(node->left);
preOrder(node->right);

}
int main()
{
int s;

cout<<"Enter no. of nodes : ";


cin>>s;

char in[s];
char post[s];

cout<<"\nEnter inorder : ";


for (int i = 0; i < s; i++)
{
cin>>in[i];
}
cout<<"\nEnter postorder : ";
for (int i = 0; i < s; i++)
{
cin>>post[i];
}

int n = sizeof(in) / sizeof(in[0]);


Node* root = buildTree(in, post, n);
cout << "Preorder of the constructed tree : \n";
preOrder(root);
cout<<"\n";
return 0;
}

b. Sample Output

Fig. 4 : Sample Output Q4


SECTION 2
(Manuscript of Working)

NOTE
This Section describes the working of
problems assigned and contains a
manuscript thereof.

You might also like