回首忆惘然
/*栈的最大容量*/#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);}/*完*/
慕仙森
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