I'm trying to write a program where the user introduces an array numbers and alphabethic characters. Then the program reads the array, if he sees a number, the program should push that number into one stack. However, if he sees an alphabetical character, the last number pushed is popped.
So far I have this code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 20
int top, i;
void push (double stack[], double x, int top)
{
stack[top] = x;
}
int pop (double stack[])
{
double x;
stack [top]=x;
return x;
}
void display (double stack[],int top)
{
int i;
printf ("\n The stack is: ");
for (i=0; i<=top; i++)
{
printf ("%lf\n",stack[i]);
}
}
void main()
{
int r;
int stack[10];
char array[10];
printf("introduce the numbers");
fgets(array,MAX,stdin);
int l;
r=strlen(array);
top=0;
for (l=0;l<=r;l++)
{
int n;
if (isdigit(array[l]))
{
push(stack,array[l],top);
top=top+1;
}
if (islower(array[l]))
{
pop(stack);
printf("you have popped %d", n);
top=top-1;
}
}
display(stack,top);
}
For some reason the program does not work, if I introduce 22a
the output is:
you have popped 4194432
The stack is: 50.00000
50.00000
I am particularly interested in how should I write the pop,push and display to make this program work. How can I do it?