just a sample of what can be done

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stack.cpp Source File

stack.cpp

00001 #include    "mbed.h"
00002 #include    "calc.h"
00003 
00004 #define     MAXVAL  100
00005 
00006 int     sp  = 0;
00007 double  val[ MAXVAL ];
00008 
00009 void push( double f )
00010 {
00011     if ( sp < MAXVAL )
00012         val[ sp++ ] = f;
00013     else
00014         printf( "error: stack full, can't push %g\n", f );
00015 }
00016 
00017 
00018 double pop( void )
00019 {
00020     if ( sp > 0 )
00021         return val[ --sp ];
00022     else {
00023         printf( "error: stack empty\n" );
00024         return 0.0;
00025     }
00026 }
00027 
00028 void show_stack( void )
00029 {
00030     for ( int i = 0; i < sp; i++ )
00031         printf( "%3d: %f\n", ((sp - 1) - i), val[ i ] );
00032 }