0% found this document useful (0 votes)
79 views4 pages

Lab Task: Alyan Hussain UET-4 Data Algorithms and Problem Solving

The document is a lab task submission that includes code to convert: 1) Infix to postfix expressions 2) Infix to prefix expressions. The code for infix to prefix conversion reverse the infix expression, handles parentheses, and uses a stack to pop and output operators based on their precedence until the stack is empty and the prefix expression is returned.

Uploaded by

Alyan
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)
79 views4 pages

Lab Task: Alyan Hussain UET-4 Data Algorithms and Problem Solving

The document is a lab task submission that includes code to convert: 1) Infix to postfix expressions 2) Infix to prefix expressions. The code for infix to prefix conversion reverse the infix expression, handles parentheses, and uses a stack to pop and output operators based on their precedence until the stack is empty and the prefix expression is returned.

Uploaded by

Alyan
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/ 4

LAB TASK

#7

ALYAN HUSSAIN
UET-4
DATA ALGORITHMS AND
PROBLEM SOLVING
TO: MS. SOBIA YOUSAF
DATE OF SUBMISSION: 10 May 2021
LAB TASK 1
Running the given code for infix to postfix conversion:

LAB TASK 2
Infix to prefix conversion:
Code:
#include <bits/stdc++.h>
using namespace std;
int precedence(char c);
bool isOperator(char c);
string Int_to_Prefix(stack<char>, string);
int main()
{
     string infix, prefix;
     cout << "Infix Expression :/n"; cin >> infix;
     stack<char> stack;
     cout << "Prefix Expression : " << Int_to_Prefix(stack, infix);
     cout<<endl;system("pause"); return 0;
}
string Int_to_Prefix(stack<char> stack, string Inf) {
     string prefix;
     reverse(Inf.begin(), Inf.end());
     for (int i = 0; i < Inf.length(); i++)
     { if (Inf[i] == '(')
               Inf[i] = ')';
          else if (Inf[i] == ')')
               Inf[i] = '('; }
     for (int i = 0; i < Inf.length(); i++)
     {if ((Inf[i] >= 'a' && Inf[i] <= 'z') || (Inf[i] >= 'A' && Inf[
i] <= 'Z'))
          { prefix += Inf[i];}

DSA
          else if (Inf[i] == '(')
          { stack.push(Inf[i]);}
          else if (Inf[i] == ')')
          { while ((stack.top() != '(') && (!stack.empty()))
               { prefix += stack.top();
                    stack.pop();}
               if (stack.top() == '(')
               {stack.pop();}}
          else if (isOperator(Inf[i]))
          {
               if (stack.empty())
               { stack.push(Inf[i]);}
               else
               {
                  if (precedence(Inf[i]) > precedence(stack.top()))
                    { stack.push(Inf[i]);}
                    else if ((precedence(Inf[i]) == precedence(stack
.top())) && (Inf[i] == '^'))
                    {
                         while ((precedence(Inf[i]) == precedence(st
ack.top())) && (Inf[i] == '^'))
                         {prefix += stack.top();
                              stack.pop();}
                         stack.push(Inf[i]);}
                    else if (precedence(Inf[i]) == precedence(stack.
top()))
                    {stack.push(Inf[i]);}
                    else
                    {
                         while ((!stack.empty()) && 
(precedence(Inf[i]) < precedence(stack.top())))
                         { prefix += stack.top();
                              stack.pop();}
                         stack.push(Inf[i]);}}}}
     while (!stack.empty())
     {prefix += stack.top();
          stack.pop();}
     reverse(prefix.begin(), prefix.end());
     return prefix;}
//
int precedence(char c)
{if (c == '^')

DSA
          return 3;
     else if (c == '*' || c == '/')
          return 2;
     else if (c == '+' || c == '-')
          return 1;
     else
          return -1;}
bool isOperator(char c)
{
     if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
          return true;
     else
          return false;}

Output:

DSA

You might also like