十進位制轉變為八進位制(用棧解決)
#include<stdio.h> #include<stdlib.h> #define max 100 typedef struct{ int data[max]; int top; }Stack; void CreatStack(Stack* &s)//建立棧 { s=(Stack*)malloc(sizeof(Stack)); s->top=-1; } void PushStack(Stack* &s,int n)//入棧 { int m=n; while(m!=0) { s->top++; n=n%8; s->data[s->top]=n; m=m/8; n=m; } } void PopStack(Stack* &s)//出棧 { while(s->top>=0) { printf("%d",s->data[s->top]); s->top--; } } void DestoryStack(Stack* &s) { free(s); } int main() { int num; Stack* L; printf("--------十進位制數轉變八進位制數-----------\n"); printf("請輸入所需要轉換的數字:\n"); scanf("%d",&num); CreatStack(L); PushStack(L,num); PopStack(L); DestoryStack(L); return 0; }