Spread the word.

Share the link on social media.

Share
  • Facebook
Have an account? Sign In Now

Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

SIKSHAPATH

SIKSHAPATH Navigation

  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Questions
  • Blog
    • Computer Science(CSE)
    • NPTEL
    • Startup
  • Shop
    • Internshala Answers
Home/ Questions/Q 234
Next
In Process

SIKSHAPATH Latest Questions

I'M ADMIN
  • 0
  • 0
I'M ADMIN
Asked: October 14, 20212021-10-14T05:58:06+05:30 2021-10-14T05:58:06+05:30In: Data Structure

Write a program to convert the expression “a+b” into “ab+”.

  • 0
  • 0

Q.Write a program to convert the expression “a+b” into “ab+”.

infixpostfixpostfix to infix
  • 2 2 Answers
  • 684 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. Professor
      Professor
      2021-10-14T05:58:41+05:30Added an answer on October 14, 2021 at 5:58 am
      This answer was edited.
      /* C++ implementation to convert
      infix expression to postfix*/

      #include<bits/stdc++.h>
      using namespace std;

      //Function to return precedence of operators
      int prec(char c) {
      if(c == '^')
      return 3;
      else if(c == '/' || c=='*')
      return 2;
      else if(c == '+' || c == '-')
      return 1;
      else
      return -1;
      }

      // The main function to convert infix expression
      //to postfix expression
      void infixToPostfix(string s) {

      stack<char> st; //For stack operations, we are using C++ built in stack
      string result;

      for(int i = 0; i < s.length(); i++) {
      char c = s[i];

      // If the scanned character is
      // an operand, add it to output string.
      if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
      result += c;

      // If the scanned character is an
      // ‘(‘, push it to the stack.
      else if(c == '(')
      st.push('(');

      // If the scanned character is an ‘)’,
      // pop and to output string from the stack
      // until an ‘(‘ is encountered.
      else if(c == ')') {
      while(st.top() != '(')
      {
      result += st.top();
      st.pop();
      }
      st.pop();
      }

      //If an operator is scanned
      else {
      while(!st.empty() && prec(s[i]) <= prec(st.top())) {
      result += st.top();
      st.pop();
      }
      st.push(c);
      }
      }

      // Pop all the remaining elements from the stack
      while(!st.empty()) {
      result += st.top();
      st.pop();
      }

      cout << result << endl;
      }

      //Driver program to test above functions
      int main() {
      string exp = "a+b";
      infixToPostfix(exp);
      return 0;
      }
        • 0
      • Reply
      • Share
        Share
        • Share on WhatsApp
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
      • Bluedragon
        Bluedragon
        2021-10-20T22:29:08+05:30Replied to answer on October 20, 2021 at 10:29 pm

        Tnq so much

          • 2
        • Reply
        • Share
          Share
          • Share on WhatsApp
          • Share on Facebook
          • Share on Twitter
          • Share on LinkedIn

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    store ads

    Stats

    • Questions 1k
    • Answers 1k
    • Posts 149
    • Best Answers 89
    • This Free AI Tool Translates Entire Books in Minute !
    • AI News: 🎬 Hollywood’s AI Studios, 🎓 OpenAI’s Latest Gift to Educators, 🚚 Class8 Bags $22M, 🧠 Google Gemini’s Memory Upgrade
    • AI NEWS: Legal Action Against OpenAI, $16M Paid, & Elon Musk’s Praise from Investor 🤖💰📑 | AI Boosts Cloud Seeding for Water Security 🌱💧
    • AI News: 🎬AI Video Tool Scam Exposed🤯, 🛰️ AI-Powered Drones to Ukraine 😱, Google’s $20M AI Push, Sam Altman Joins SF’s Leadership Team
    • AI News: 🤝 Biden Meets Xi on AI Talks, 💡 Xavier Niel’s Advice for Europe, ♻️ Hong Kong’s Smart Bin Revolution, 🚀 AI x Huawei

    Explore

    • Recent Questions
    • Questions For You
    • Answers With Time
    • Most Visited
    • New Questions
    • Recent Questions With Time

    Footer

    SIKSHAPATH

    Helpful Links

    • Contact
    • Disclaimer
    • Privacy Policy Notice
    • TERMS OF USE
    • FAQs
    • Refund/Cancellation Policy
    • Delivery Policy for Sikshapath

    Follow Us

    © 2021-24 Sikshapath. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.