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的参数怎么定义的??
添加回答
举报