/*这题是用结构体顺序栈来做进制转换,将10进制转为2进制,但编译的时候老通过不了,求高手帮我看看!!!C做的*/
#include<stdio.h>
#include<stdlib.h>
#define StackSize 100
typedef char DataType;
typedef struct{
DataType data[StackSize];
int top;
}SeqStack;
//(1) 置栈空
void InitStack(SeqStack *S)
{ S->top=-1; }
//(2) 判栈空
int StackEmpty(SeqStack *S)
{ return S->top==-1; }
//(3) 判栈满
int StackFull(SeqStack *S)
{ return S->top==StackSize-1; }
//(4) 进栈
void Push(S,x)
{ if (StackFull(S))
Error("Stack overflow"); //上溢,退出运行
S->data[++S->top]=x;//栈顶指针加1后将x入栈
}
//(5) 退栈
char Pop(S)
{ if(StackEmpty(S))
Error("Stack underflow"); //下溢,退出运行
return S->data[S->top--];//栈顶元素返回后将栈顶指针减1
}
//(6) 取栈顶元素
char StackTop(S)
{
if(StackEmpty(S))
Error("Stack is empty");
return S->data[S->top];
}
int main()
{ InitStack(S);
FILE *fp1,*fp2;
fp1=fopen("stack.in","r");
fp2=fopen("stack.out","w");
int shu;
fsacf(fp1,"%d",&shu);
while(shu)
{ Push(S,shu%2);
shu=shu/2;
}
while(!StackEmpty(S))
{
Pop(S,e);
fprintf(fp2,"%d",e);
}
fclose(fp1);
fclose(fp2);
}
參考答案:按下面改
程序也帮你改了,应该运行正确了
#define StackSize 100
typedef char DataType;
typedef struct{
DataType data[StackSize];
int top;
}SeqStack;
//(1) 置栈空
void InitStack(SeqStack *S)
{ S->top=-1; }
//(2) 判栈空
int StackEmpty(SeqStack *S)
{ return S->top==-1; }
//(3) 判栈满
int StackFull(SeqStack *S)
{ return S->top==StackSize-1; }
//(4) 进栈
void Push(SeqStack *S,char x)
{ if (!StackFull(S))
//Error("Stack overflow"); //上溢,退出运行
S->data[++S->top]=x;//栈顶指针加1后将x入栈
}
//(5) 退栈
char Pop(SeqStack *S)
{ if(!StackEmpty(S))
//Error("Stack underflow"); //下溢,退出运行
return S->data[S->top--];//栈顶元素返回后将栈顶指针减1
}
//(6) 取栈顶元素
char StackTop(SeqStack *S)
{
if(StackEmpty(S))
//Error("Stack is empty");
return S->data[S->top];
}
int main()
{
SeqStack ST;
SeqStack *S = &ST;
InitStack(S);
FILE *fp1,*fp2;
fp1=fopen("stack.in","r");
fp2=fopen("stack.out","w");
int shu;
fscanf(fp1,"%d",&shu);
while(shu)
{ Push(S,shu%2);
shu=shu/2;
}
while(!StackEmpty(S))
{
char e=Pop(S);
fprintf(fp2,"%d",e);
}
fclose(fp1);
fclose(fp2);
}