Submission By: Kshitij Kumar Singh Chauhan
Submission By: Kshitij Kumar Singh Chauhan
Submission By: Kshitij Kumar Singh Chauhan
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++
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;
}
if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];
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;
}
if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}
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
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;
}
if (isalpha(infix[i]) || isdigit(infix[i]))
output += infix[i];
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;
}
if (infix[i] == '(') {
infix[i] = ')';
i++;
}
else if (infix[i] == ')') {
infix[i] = '(';
i++;
}
}
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
a. Source Code
#include <iostream>
#include <string.h>
struct My_Stack
{
int top;
unsigned limiting_val;
int* arr;
};
My_Stack->top = -1;
My_Stack->limiting_val = limiting_val;
My_Stack->arr = (int*) malloc(My_Stack->limiting_val * sizeof(int));
return My_Stack;
}
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
a. Source Code
#include <bits/stdc++.h>
struct Node {
char data;
Node *left, *right;
};
if (inStrt == inEnd)
return node;
int pIndex = n - 1;
return buildUtil(in, post, 0, n - 1, &pIndex);
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break;
}
return i;
if (node == NULL)
return;
printf("%c ", node->data);
preOrder(node->left);
preOrder(node->right);
}
int main()
{
int s;
char in[s];
char post[s];
b. Sample Output
NOTE
This Section describes the working of
problems assigned and contains a
manuscript thereof.