📄 1-2-2.c
字号:
/*中国系统分析员顾问团,http://www.csai.cn*/
/*程序员下午考试指南书籍源码*/
#include <malloc.h>
#include <stdio.h>
#define MAXSIZE 32
typedef struct {
int *elem; /*栈的存储区*/
int max; /*栈的容量,即栈中最多能存放的元素个数*/
int top; /*栈顶指针*/
} Stack;
int InitStack(Stack *S, int n) /*创建容量为n的空栈*/
{
S->elem = (int *)malloc(n * sizeof(int));
if(S->elem == NULL) return -1;
S->max = n;
S->top = 0;
return 0;
}
int Push(Stack *S, int item) /*将整数item压入栈顶*/
{
if(S->top == S->max) {
printf("Stack is full! \n");
return -1;
}
S->elem[S->top++] = item;
return 0;
}
int StackEmpty(Stack *S)
{
return (!S->top)?1 : 0; /*判断栈是否为空*/
}
int Pop(Stack *S) /*栈顶元素出栈*/
{
if(!S->top) {
printf("Pop an empty stack!\n");
return -1;
}
return S->elem[--S->top];
}
void MultibaseOutput(long n,int B)
{
int m; Stack S;
if(InitStack(&S,MAXSIZE)){
printf("Failure!\n");
return;
}
do {
if (Push(&S,n%B)) {
printf("Failure!\n");
return;
}
n= n/B ;
}while(n!=0);
while(!StackEmpty(&S)) { /*输出B进制的数*/
m=Pop(&S);
if(m<10) printf("%d",m); /*小于10,输出数字*/
else printf("%c", m+55); /*大于或等于10,输出相应的字符*/
}
printf("\n");
}
main()
{
MultibaseOutput(12,8);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -