Solved

Problem in stack(data structure in C)

Posted on 1998-11-19
4
436 Views
Last Modified: 2010-04-15
How to write a program using stack (in C language)which functions almost like a calculator. the program will receive inputs like for example : 12 * (15 - 6) + 388  
The program will calculate +, -, *, /, (), operations.  
The priority operations are :

   1st. ( )
   2nd. *, /
   3rd. +, -

The program will print the operation for example like The question above :

15 - 6
12 * 9
108 + 388
= 496
0
Comment
Question by:tagheuer
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1254429
Easiest Answer is to write convert command strings to RPN (Reverse Polish Notation)

Basically this is a stack machine, you push values on the stack.  Operations then pull last 2 items and do the work

e.g.
15 - 6 becomes 15 6 -
15 is pushed on stack
6 is pushed on stack
- pulls last 2 items from stack, does the operation
- pushes the result back on stack

15 - 6 + 4 becomes 15 6 - 4 +
same idea
0
 
LVL 4

Accepted Solution

by:
emmons earned 10 total points
ID: 1254430
Some code from imlandris...
#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
 

Author Comment

by:tagheuer
ID: 1254431
Theres a good one!!, any way thanks to you for helping me. I'll retype your codes and give you the feedback.
Actually I'm very new in C language especially in data sturcture chapter. May be you can help me (give an example on stack, recursion, queues and list).
Thank you again!!


0
 

Author Comment

by:tagheuer
ID: 1254432
to: answer2000,
Can you explain more details on your suggestion above to convert command strings to RPN (Reverse Polish Notation). I'm really don't understand. It will be better if yu can give some example.

thanks!!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 99
libcurl - taking long time on curl_easy_perform 11 438
Problem in finding output of a program 11 106
Loading flat file data in tables 2 41
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

830 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