?? 4-4.c
字號:
#include "stdio.h"
#define StackSize 100 //假定預分配的棧空間最多為100個元素
typedef int DataType;//假定棧元素的數據類型為字符
typedef struct{
DataType data[StackSize];
int top;
}SeqStack;
// 置棧空
void Initial(SeqStack *S)
{//將順序棧置空
S->top=-1;
}
//判棧空
int IsEmpty(SeqStack *S)
{
return S->top==-1;
}
//判棧滿
int IsFull(SeqStack *S)
{
return S->top==StackSize-1;
}
//進棧
void Push(SeqStack *S,DataType x)
{
if (IsFull(S))
{
printf("棧上溢"); //上溢,退出運行
exit(1);
}
S->data[++S->top]=x;//棧頂指針加1后將x入棧
}
//出棧
DataType Pop(SeqStack *S)
{
if(IsEmpty(S))
{
printf("棧為空"); //下溢,退出運行
exit(1);
}
return S->data[S->top--];//棧頂元素返回后將棧頂指針減1
}
// 取棧頂元素
DataType Top(SeqStack *S)
{
if(IsEmpty(S))
{
printf("棧為空"); //下溢,退出運行
exit(1);
}
return S->data[S->top];
}
void MultiBaseOutput (int N,int B)
{//假設N是非負的十進制整數,輸出等值的B進制數
int i;
SeqStack S;
Initial(&S);
while(N){ //從右向左產生B進制的各位數字,并將其進棧
Push(&S,N%B); //將bi進棧0<=i<=j
N=N/B;
}
while(!IsEmpty(&S)){ //棧非空時退棧輸出
i=Pop(&S);
printf("%d",i);
}
}
void main()
{
MultiBaseOutput(1023,2);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -