rpn calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mido elnaggar
    New Member
    • Apr 2010
    • 5

    #1

    rpn calculator

    i ve done so far this code but it deals only with 1 digit numbers i dont know how to make it work for 2 or more digited no.s using atoi or atof functions

    #include"stdio. h"
    #include"stdlib .h"
    #include"string .h"
    char postfix[100];
    int stack[100];
    int top=-1;
    void push(int x)
    {
    top++;
    stack[top]=x;
    }
    int pop()
    {
    return stack[top--];
    }
    int evalpost()
    {
    int a,b,i,temp,resu lt,len;
    len=strlen(post fix);
    for(i=0;i<len;i ++)
    {
    if(postfix[i]<='50'&&postf ix[i]>='0')
    push(postfix[i]-'0');
    else
    {
    a=pop();
    b=pop();
    switch(postfix[i])
    {
    case'+':
    temp=a+b;break;
    case'-':
    temp=b-a;break;
    case'*':
    temp=a*b;break;
    case'/':
    temp=b/a;break;
    }
    push(temp);
    }
    }
    result=pop();
    return result;
    }
    void main()
    {
    printf("enter rpn\n");
    gets(postfix);
    top=-1;
    int x=evalpost();
    printf("%d\n",x );
    }




    and thanks for ur concern
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Do you mean revs/nautical_mile.. ........and please put your code in between code tags which makes it much easier to read.
    See what you can make of the following:
    Code:
    cout<<"\ndemonstrating the atoi()function\n";
       
        const char* s="  21xy 3";
        const char*s1= " 74.7xyz ";
        cout<<"the first string is \n"<<s<<endl;
        cout<<"the second string is \n"<<s1<<endl;
        n=atoi(s);
        m=atoi(s1);
        cout<<"after converting strings to integers ";
        cout<<"s= "<<n<<" and s1="<<m<<endl;
        cout<<"the product of s and s1 is "<<n*m<<endl; 
        
        
    char q;
    cout<<"press any key to continue...";
    cin.get(q);
    return 0;    
        
    }    
    /*
    demonstrating the atoi()function
    the first string is
      21xy 3
    the second string is
     74.7xyz
    after converting strings to integers s= 21 and s1=74
    the product of s and s1 is 1554
    press any key to continue...*/

    Comment

    Working...