以下内容是关于数据结构问题,麻烦大佬帮忙看看!

利用栈的“后进先出”的特点,在TC环境下实现将任一个十进制数转换为二进制数。
我按书上的程序写入TC,但是运行的时候有很多错误.希望哪位大虾给编个程序或帮忙改一下.(C语言版的)
void conversion(){
InitStack(S);
scanf("%d",N);
while(N){
Push(S,N%2);
N=N/2;
}
while(!StackEmpty(s)){
Pop(S,e);
printf("%d",e);
}
}
这个是书上的程序.本人刚开学,哪位大虾帮帮忙,尽量将简单点,谢谢

慕婉清6462132
浏览 177回答 3
3回答

回首忆惘然

/*栈的最大容量*/#define&nbsp;MAX&nbsp;100/*自定义一个简单的栈类型*/typedef&nbsp;struct&nbsp;_Stack{&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;index;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;max;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;num[MAX];}Stack;/*对栈进行初始化*/void&nbsp;init(Stack&nbsp;*s);/*进栈*/void&nbsp;push(Stack&nbsp;*s,int&nbsp;n);/*出栈*/void&nbsp;pop(Stack&nbsp;*s,int&nbsp;*&nbsp;e);/*判断栈是否为空*/short&nbsp;isempty(Stack&nbsp;*s);/*主函数,程序入口*/void&nbsp;main(){&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;N,i;&nbsp;&nbsp;&nbsp;&nbsp;Stack&nbsp;*s;&nbsp;&nbsp;&nbsp;&nbsp;printf("\nPlease&nbsp;input&nbsp;a&nbsp;number:",&N);&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d",&N);&nbsp;&nbsp;&nbsp;&nbsp;init(s);&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;/*开始解析数字,并入栈*/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;1&nbsp;&&nbsp;N;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push(s,i);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;>>=&nbsp;1;&nbsp;&nbsp;&nbsp;&nbsp;}while(N);&nbsp;&nbsp;&nbsp;&nbsp;printf("\nThe&nbsp;result&nbsp;is:");&nbsp;&nbsp;&nbsp;&nbsp;while(!isempty(s))&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;/*按相反的顺序输出栈的内容,就是二进制的数*/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop(s,&i);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d",i);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;getch();/*显示完了后不立即退出程序*/}/*以下是对前面声明的具体实现*/void&nbsp;init(Stack&nbsp;*s){&nbsp;&nbsp;&nbsp;&nbsp;s->index=0;&nbsp;&nbsp;&nbsp;&nbsp;s->max=MAX;}void&nbsp;push(Stack&nbsp;*s,int&nbsp;n){&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s->index>=s->max)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s->num[s->index++]&nbsp;=&nbsp;n;&nbsp;&nbsp;}void&nbsp;pop(Stack&nbsp;*s,int&nbsp;*&nbsp;i){&nbsp;&nbsp;&nbsp;&nbsp;*i&nbsp;=&nbsp;s->num[--s->index];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}short&nbsp;isempty(Stack&nbsp;*s){&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(s->index<=0);}/*完*/

慕仙森

Option ExplicitPublic Function Bin(ByVal n As Double, ByVal m As Long) As StringDim i As Long, dot As Long, iP As Long, fP As DoubleDim prefix As String, BinInt As String, BinFloat As StringIf Left(n, 1) = "-" Then prefix = "-": n = Mid(n, 2)dot = InStr(n, ".")If dot <> 0 Then iP = Left(n, dot - 1): fP = Mid(n, dot) Else iP = nDoBinInt = (iP Mod 2) & BinIntiP = iP \ 2Loop Until iP = 0BinInt = prefix & BinIntIf dot = 0 Then Bin = BinInt: Exit FunctionFor i = 1 To mfP = fP * 2fP = (fP - Int(fP)) + (Int(fP) Mod 2)BinFloat = BinFloat & Int(fP)If fP = 1 Then Exit ForNextBin = BinInt & "." & BinFloatEnd FunctionPublic Function Dec(ByVal n As String) As DoubleDim i As Long, j As Long, dot As Long, prefix As Longprefix = Sgn(n)If prefix = -1 Then n = Mid(n, 2)dot = InStr(n, ".")If dot = 0 Thendot = Len(n) - 1Elsen = Left(n, dot - 1) & Mid(n, dot + 1)dot = dot - 2End IfFor i = dot To dot - Len(n) + 1 Step -1j = j + 1If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ iNextDec = Dec * prefixEnd FunctionPrivate Sub Command2_Click()Dim x As Double, max As Longx = InputBox("请输入一个十进制数值")'max = InputBox("请输入最多几个小数位")If x > 100000000 ThenMsgBox "越位"Exit SubEnd IfText1.Text = "二进位 = " & Bin(x, 0)Text2.Text = "转回十进位 = " & Dec(Bin(x, max))'MsgBox "二进位 = " & Bin(x, 0) ' max)'MsgBox "转回十进位 = " & Dec(Bin(x, max))End Sub

慕尼黑5688855

void conversion(){InitStack(S);int N;scanf("%d",&N);while(N){Push(S,N%2);N=N/2;}while(!StackEmpty(s)){Pop(S,e);printf("%d",e);}}这是改了后的,但是上面的函数不知道你都定义好了没有?在初始化栈的时候你的S的参数怎么定义的??
打开App,查看更多内容
随时随地看视频慕课网APP