关于栈初始化Run-Time Check Failure #3The variable 'a' is being used without being ini,该怎么解决
关于栈初始化Run-Time Check Failure #3The variable 'a' is being used without being ini
#include<stdio.h>
#include<stdlib.h>
typedef struct{
long date[100];
int top;
}Stack;
void StackInit(Stack s)
{
s.date[1000]=0;
s.top=-1;
}
bool Empty(Stack s)
{
if(s.top==(-1))
return true;
else false;
}
void Push(Stack s,int n)
{
if(s.top==999)
{
printf("栈已满了/n");
exit(0);
}
s.top++;
s.date[s.top]=n;
}
int Pop(Stack s)
{
long n;
if(Empty(s))
{
printf("栈已空,无法取出元素了/n");
exit(0);
}
n=s.date[s.top];
s.top--;
return n;
}
int StackLength(Stack s)
{
return (s.top+1);
}
int main()
{
Stack a;
long int n;
printf("要求计算多少的阶乘:");
scanf("%ld",&n);
StackInit(a);
while(n>0)
{
Push(a,n);
n--;
}
n=1;
while(StackLength(a)>0)
{
n=n*Pop(a);
}
printf("计算结果为:%ld",n);
return 0;
}
我用的VS2008 编译没错误,但是运行时出现了栈不能初始化问题,我程序中有初始化栈的函数不知道怎么不行,求大侠指点。
------解决方案--------------------
把你上面的提供操作Stack的函数改成引用
比如void StackInit(Stack s)
--》
void StackInit(Stack& s)
------解决方案--------------------
long date[100];
s.date[1000]=0;
#include<stdio.h>
#include<stdlib.h>
typedef struct{
long date[100];
int top;
}Stack;
void StackInit(Stack s)
{
s.date[1000]=0;
s.top=-1;
}
bool Empty(Stack s)
{
if(s.top==(-1))
return true;
else false;
}
void Push(Stack s,int n)
{
if(s.top==999)
{
printf("栈已满了/n");
exit(0);
}
s.top++;
s.date[s.top]=n;
}
int Pop(Stack s)
{
long n;
if(Empty(s))
{
printf("栈已空,无法取出元素了/n");
exit(0);
}
n=s.date[s.top];
s.top--;
return n;
}
int StackLength(Stack s)
{
return (s.top+1);
}
int main()
{
Stack a;
long int n;
printf("要求计算多少的阶乘:");
scanf("%ld",&n);
StackInit(a);
while(n>0)
{
Push(a,n);
n--;
}
n=1;
while(StackLength(a)>0)
{
n=n*Pop(a);
}
printf("计算结果为:%ld",n);
return 0;
}
我用的VS2008 编译没错误,但是运行时出现了栈不能初始化问题,我程序中有初始化栈的函数不知道怎么不行,求大侠指点。
------解决方案--------------------
把你上面的提供操作Stack的函数改成引用
比如void StackInit(Stack s)
--》
void StackInit(Stack& s)
------解决方案--------------------
long date[100];
s.date[1000]=0;