# include<stdio.h>
# include<stdlib.h>
# define Max_Size 50
typedef struct{//typedef是小寫開頭!
//注意此處的top是整形指針
int data[Max_Size];
int top;
}SeqStack,*PSeqStack;
void Init_SeqStack(PSeqStack S)
{
S->top=-1;//哪裡錯了?求解釋!
}
int Full_SeqStack(PSeqStack S)
{
if(S->top==Max_Size-1) return 1;
else return 0;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1) return 1;
else return 0;
}
void Push_SeqStack(PSeqStack S,int n)
{
if(Full_SeqStack(S)==1) exit(0);
else
{S->top=++S->top;//及時先加一個空間
S->data[S->top]=n;}//棧也有data域!
}
void Pop_SeqStack(PSeqStack S,int n)//*n也是指針
{
if(Empty_SeqStack(S)==1) printf("UnderFlow\n");
else
{n=S->data[S->top];
S->top=--S->top;}//及時退一個空間
}
int fact(int n,int f,PSeqStack S)//調用函數為什麼不對?求解釋!
{
Init_SeqStack(S);
while(n!=0)
{
Push_SeqStack(S,n);
n=n-1;
}
f=1;
while(Empty_SeqStack(S)!=1)
{
Pop_SeqStack(S,n);
f=f*n;
}
return f;
}
int main()
{
int n,f;SeqStack *S;
printf("input n:\n");
scanf("%d",&n);
printf("%d\n",fact(n,f,S));
getch();
}
int n,f;SeqStack *S;
->
int n,f;SeqStack *S = (SeqStack *)malloc(sizeof(SeqStack));