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);
}
/*完*/

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

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的參數怎么定義的??
添加回答
舉報