A Thread for Programmers.!

hasiwebmail

Member
Feb 27, 2007
501
52
0
Sri Lanka
Sorry it was developed for numbers only
here is final code


Code:
/*\
Write a C++ program to convert any infix expression to its post-fix form....
Eg-

if user enters a+b
it should print ab+
\*/

#include <iostream>
#include <iomanip>

using std::cin;
using std::endl;
using std::cout;
using std::setw;

int main()
{
    int hold=0;
    const int max=100;

    char string[max] = {0};
    char repeat('n');

    char stringExp[max] = {0};
    char stringSymbol[max] = {0};
    int j=0;
    int i=0;
    int k=0;

    cout << "Type any Expression to proceed" << endl;
    cin.getline(string,max,'\n');

    cout << endl;

    while(string[i] != '\0') //till it reach the null character
    {
        if(('9' >= string[i] && '0' <= string[i]) || ('a'<= string[i] && 'z' >= string[i]) || ('A'<= string[i] && 'Z' >= string[i]) ) 
        {
            stringExp[k] = string[i];
            k++;
        }else 
        {
            stringSymbol[j] = string[i];
            j++;
        }
        i++;
    }

    cout << endl;
    cout << "Final Out Put " << stringExp << stringSymbol ;
    cout << endl;

    cin >> hold;
    return 0;
}

View attachment 16329

That's better :D
Nice way to check chars, and why u checking numbers?

NICE WORK........

There's a priority of operators when pushing back..

Now.........
Try with expression more than one operator

eg-
a-b/c


output

abc/- (remember B O D M A S)
 
Last edited:

JAASBandara

Well-known member
  • Sep 20, 2008
    10,564
    281
    83
    You Better To Use JAVA

    Okkkk I'm In Bro ! This is the 1st one & I'll start with a easy one . . .

    use java for solve this Prob . . .

    Q01
    There is a class & in main method have two object 0f that class with two reference. In 1st object's constructor have integer value 10 & 2nd one have 20 (as you wish) & now just print those constructor's value as output.

    After wrote code if i change those constructor's value must print that changed value . . .

    Try It !
     

    Core

    Member
    Jan 23, 2010
    3,120
    195
    0
    Milky Way/Local Cluster/Local Sol/Earth
    That's better :D
    Nice way to check chars, and why u checking numbers?

    NICE WORK........

    There's a priority of operators when pushing back..

    Now.........
    Try with expression more than one operator

    eg-
    a-b/c


    output

    abc/- (remember B O D M A S)

    dude ,first tested what I have done
    it works with everything.
    infact don't try to append new items to your questions when I have done it
    u didn't mention Bodmas early right.?
    it's distracting you can make a new questions rather than append new parts to your same question.
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    dude ,first tested what I have done
    it works with everything.
    infact don't try to append new items to your questions when I have done it
    u didn't mention Bodmas early right.?
    it's distracting you can make a new questions rather than append new parts to your same question.

    Hmm.... ok ok.... it was jst going little by little

    ur one
    input

    a-b/c

    abc-/


    According to B O D M A S

    should be

    abc/-

    sub has a lower priority

    That's all and SORRY...... :P
    Cooool Down....... Thx for rply :D:D:D:D:D:D:D
     
    Last edited:

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    //program with BODMAS in C
    #include "stdio.h"
    int top=-1;
    void main()
    {
    void push(char,char []);
    char pop(char []);
    int pref(char,char []);
    char as[20],fi[20],s[50];
    int y=0,i=0,j=0;
    printf("Enter a string infix :");
    gets(as);
    for(i=0;as!='\0';i++)
    {
    if(as>='a' && as<='z' || as>='A' && as<='Z')
    {
    fi[j]=as;
    j++;
    }
    else
    if(as=='+' || as=='-' || as=='/' || as=='*')
    {
    y=pref(as,s);
    if(y==0)
    {
    push(as,s);
    }
    else
    if (y==1)
    {
    while(y!=0)
    {
    fi[j]=pop(s);
    j++;
    y=pref(as,s);
    }
    push(as,s);
    }
    }
    }
    while(j<=i)
    {
    fi[j]=pop(s);
    j++;
    }
    puts(fi);
    }

    void push(char x,char s[])
    {
    top=top+1;
    s[top]=x;
    }

    char pop(char s[])
    {
    char x;
    x=s[top];
    top=top-1;
    return x;
    }

    int pref(char x,char s[])
    {
    if(x=='+' && s[top]=='*' || x=='+' && s[top]=='/' || x=='-' && s[top]=='+' || x=='-' && s[top]=='/' || x=='-' && s[top]=='*' || x=='*' && s[top]=='/')
    {
    return 1;
    }
    else
    return 0;
    }

    Tested using BorlandC++ 4.5v
     
    Last edited: