亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下內容是關于數據結構問題,麻煩大佬幫忙看看!

以下內容是關于數據結構問題,麻煩大佬幫忙看看!

利用棧的“后進先出”的特點,在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);}}這個是書上的程序.本人剛開學,哪位大蝦幫幫忙,盡量將簡單點,謝謝
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

/*棧的最大容量*/
#define MAX 100

/*自定義一個簡單的棧類型*/
typedef struct _Stack
{
    int index;
    int max;
    int num[MAX];
}Stack;

/*對棧進行初始化*/
void init(Stack *s);
/*進棧*/
void push(Stack *s,int n);
/*出棧*/
void pop(Stack *s,int * e);
/*判斷棧是否為空*/
short isempty(Stack *s);

/*主函數,程序入口*/
void main()
{
    int N,i;
    Stack *s;
    printf("\nPlease input a number:",&N);
    scanf("%d",&N);
    init(s);
    do
    {   /*開始解析數字,并入棧*/
        i = 1 & N;
        push(s,i);      
        N >>= 1;
    }while(N);
    printf("\nThe result is:");
    while(!isempty(s))
    {   /*按相反的順序輸出棧的內容,就是二進制的數*/
        pop(s,&i); 
        printf("%d",i); 
    }
    getch();/*顯示完了后不立即退出程序*/
}
/*以下是對前面聲明的具體實現*/
void init(Stack *s)
{
    s->index=0;
    s->max=MAX;
}
void push(Stack *s,int n)
{
    if (s->index>=s->max)
    {
        return;
    }   
    s->num[s->index++] = n;  
}
void pop(Stack *s,int * i)
{
    *i = s->num[--s->index];     
}
short isempty(Stack *s)
{
    return (s->index<=0);
}
/*完*/



查看完整回答
反對 回復 2022-01-17
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

Option Explicit

Public Function Bin(ByVal n As Double, ByVal m As Long) As String
Dim i As Long, dot As Long, iP As Long, fP As Double
Dim prefix As String, BinInt As String, BinFloat As String
If 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 = n
Do
BinInt = (iP Mod 2) & BinInt
iP = iP \ 2
Loop Until iP = 0
BinInt = prefix & BinInt
If dot = 0 Then Bin = BinInt: Exit Function
For i = 1 To m
fP = fP * 2
fP = (fP - Int(fP)) + (Int(fP) Mod 2)
BinFloat = BinFloat & Int(fP)
If fP = 1 Then Exit For
Next
Bin = BinInt & "." & BinFloat
End Function

Public Function Dec(ByVal n As String) As Double
Dim i As Long, j As Long, dot As Long, prefix As Long
prefix = Sgn(n)
If prefix = -1 Then n = Mid(n, 2)
dot = InStr(n, ".")
If dot = 0 Then
dot = Len(n) - 1
Else
n = Left(n, dot - 1) & Mid(n, dot + 1)
dot = dot - 2
End If
For i = dot To dot - Len(n) + 1 Step -1
j = j + 1
If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ i
Next
Dec = Dec * prefix
End Function

Private Sub Command2_Click()
Dim x As Double, max As Long
x = InputBox("請輸入一個十進制數值")
'max = InputBox("請輸入最多幾個小數位")
If x > 100000000 Then
MsgBox "越位"
Exit Sub
End If
Text1.Text = "二進位 = " & Bin(x, 0)
Text2.Text = "轉回十進位 = " & Dec(Bin(x, max))
'MsgBox "二進位 = " & Bin(x, 0) ' max)
'MsgBox "轉回十進位 = " & Dec(Bin(x, max))
End Sub



查看完整回答
反對 回復 2022-01-17
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

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的參數怎么定義的??



查看完整回答
反對 回復 2022-01-17
  • 3 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號