栈,c语言问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 15:05:46
用递归函数写阶乘大家都知道吧,不用函数,只用栈,怎么写?

#include <stdio.h>

#define MAX_C 20
typedef struct stack{
int pos;
int data[20];
}Stack;

Stack st;

void main()
{
int i,n;
long res = 1;
st.pos = 0;
scanf("%d", &n);
if(n > MAX_C) return;

//push
for(i=2; i<=n; ++i)
{
st.data[st.pos++] = i;
}

//pop
while(st.pos > 0)
{
res *= st.data[--st.pos];
}

printf("%d! = %d \n", n, res);
}

什么玩意啊这是?

#include<stdio.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

#define OK 1
#define ERROR 0
#define FAILURE 0
#define FAILED NULL

typedef int ElemType;
typedef int Status;
double Result=1;

typedef struct SqStack
{
ElemType *top;
ElemType *base;
int stacksize;
}SqStack;

Status