C 从一个字符串到堆栈的Push、pop和display函数

C 从一个字符串到堆栈的Push、pop和display函数,c,string,stack,C,String,Stack,我正试图编写一个程序,用户在其中引入一个数组数字和字母字符。然后程序读取数组,如果他看到一个数字,程序应该将该数字放入一个堆栈中。但是,如果他看到一个字母字符,最后按下的数字就会弹出 到目前为止,我有以下代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define MAX 20 int top, i; void push (do

我正试图编写一个程序,用户在其中引入一个数组数字和字母字符。然后程序读取数组,如果他看到一个数字,程序应该将该数字放入一个堆栈中。但是,如果他看到一个字母字符,最后按下的数字就会弹出

到目前为止,我有以下代码:

#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);
}
#包括
#包括
#包括
#包括
#定义最大值20
int top,i;
无效推送(双堆栈[],双x,整数顶部)
{
堆栈[顶部]=x;
}
int-pop(双堆栈[])
{
双x;
堆栈[顶部]=x;
返回x;
}
无效显示(双堆栈[],int-top)
{
int i;
printf(“\n堆栈为:”);

对于(i=0;i首先,您正在打印的变量
n
的值未初始化,并且包含创建时内存中的任何垃圾

还有,你为什么要打印它呢?我想你的意思是说
n=pop(stack);
,对吧?否则这个打印就没用了


在整个代码中,您编写循环的方式都是错误的:
for(t=0;t首先,您正在打印的变量
n
的值未初始化,并且包含创建时内存中的任何垃圾

还有,你为什么要打印它呢?我想你的意思是说
n=pop(stack);
,对吧?否则这个打印就没用了


在整个代码中,您编写循环的方式都是错误的:
for(t=0;t您正在将20个字符
#定义为最大20
到一个大小为10的数组中。此外,在您的循环中,您有
for(l=0;l更重要的是,您正在调用
pop(stack)
,而没有使用返回值(!),那么,是什么让您认为未初始化的变量
n
将包含一些有用的值呢?每当您循环时,您都会为(i=0;i弹出时,应首先递减top,否则将无法访问先前推送的元素。您正在将20个字符
#define MAX 20
读入一个大小为10的数组中。此外,在循环中,您有
for(l=0;l更重要的是,您正在调用
pop(stack)
,而不使用返回值(!),那么,是什么让您认为未初始化的变量
n
将包含一些有用的值呢?每当您循环时,您都会对(i=0;i)执行
for(当弹出时,您应该先减小top,否则您将无法访问先前推送的元素。