Solved

how to make a program by using stack

Posted on 1998-11-19
3
175 Views
Last Modified: 2010-04-15
how to make c program like a culculator....using stack to solve it.
eg output:
12*(15-6) + 388=496
0
Comment
Question by:badrul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1254413
Strange some question. 2 different users. Are you both in the same school. Sorry not homework will be done in EE. Post what you have done already and we will help you the solve your problems.
0
 

Expert Comment

by:baprebap
ID: 1254414
it's quite easy.first change the expression into a prefix expression (or postfix ).you may refer to some goos data structures book for this purpose.once you have done so it is very easy to calculate it.(try reading data structures using pascal by tenebaum and augustein or data structures by tremblay sorenson)
you may try even knuth
0
 
LVL 4

Accepted Solution

by:
emmons earned 10 total points
ID: 1254415
Pretty cheap points for source code.
Some code from imladris...
#include "stdio.h"

#define Err(n)       printf(errm[n-1])
#define Guard(n)     if(sp<n-1){ Err(1); break; }

char errm[3][40] = { "Insufficient stack elements\n",
                     "Stack full\n",
                     "Invalid input\n"
                   };

char base = 'd',buf[160];
int cp=0;

main()

{       long stk[10],mem,sp;
        int i;
        char c,num[20];

        sp=-1;
        mem=0;

        do
        {       c=getnext();
                switch(c)
                {       case 'x':
                                base='x';
                                if(sp>=0)
                                {       printf("\n");
                                        show(stk[sp]);
                                }
                                break;
                        case 'd':
                                base='d';
                                if(sp>=0)
                                {       printf("\n");
                                        show(stk[sp]);
                                }
                                break;
                        case '+':
                                Guard(2);
                                stk[sp-1]+=stk[sp];
                                --sp;
                                printf("\n");
                                show(stk[sp]);
                                break;
                        case '-':
                                Guard(2);
                                stk[sp-1]-=stk[sp];
                                --sp;
                                printf("\n");
                                show(stk[sp]);
                                break;
                        case '*':
                                Guard(2);
                                stk[sp-1]*=stk[sp];
                                --sp;
                                printf("\n");
                                show(stk[sp]);
                                break;
                        case '/':
                                Guard(2);
                                stk[sp-1]/=stk[sp];
                                --sp;
                                printf("\n");
                                show(stk[sp]);
                                break;
                        case 't':
                                printf("\n");
                                if(sp>=0)show(stk[sp]);
                                break;
                        case 'a':
                                printf("\n");
                                for(i=sp; i>=0; --i)show(stk[i]);
                                break;
                        case 'p':
                                Guard(1);
                                --sp;
                                break;
                        case 's':
                                mem=stk[sp];
                                break;
                        case 'r':
                                stk[sp]=mem;
                                break;
                        case 'm':
                                printf("\n");
                                show(mem);
                                break;
                        case 'q':
                        case '\n':
                        case '\r':
                        case ' ':
                                break;
                        default:
                                if('0'<=c && c<='9')
                                {       if(sp<9)
                                        {       backup();
                                                getnum(num);
                                                ++sp;
                                                if(base=='d')sscanf(num,"%ld",stk+sp);
                                                else sscanf(num,"%lx",stk+sp);
                                        }
                                        else Err(2);
                                }
                                else Err(3);
                }
        } while(c!='q');
}


show(a)
long a;

{       if(base=='d')printf("%ld\n",a);
        else printf("%lx\n",a);
        return;
}

getnext()

{       if(buf[cp]=='\0')getline();
        return(buf[cp++]);
}


backup()

{       if(cp>0)--cp;
        return;
}


getnum(num)
char *num;

{       int dp;

        dp=0;
        while( ('0'<=buf[cp] && buf[cp]<='9') || (base=='x' && 'a'<=buf[cp] && buf[cp]<='f') )num[dp++]=buf[cp++];
        num[dp]='\0';
        return;
}


getline()

{       char *sp,*dp;

        scanf("%s",buf);
        dp=buf;
        for(sp=buf; *sp!='\0'; ++sp)
        {       if(*sp!=8)*dp++=*sp;
                else dp=dp>buf?dp-1:dp;
        }
        *dp='\0';
        cp=0;
        return;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 21
voltage to force translation ? 8 100
Compile VxWorks Toronado project under Visual Studio 11 242
Inorder binary search tree 5 178
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question