十进制转n进制
2023-03-14 09:45:50 时间
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 5
typedef struct {
ElemType* base;
int top;
int size;
int increment; // 扩容时,增加的存储容量
} SqStack;
// 构造一个空栈 S
Status InitStack(SqStack& S) {
S.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!S.base) exit(OVERFLOW);
S.top = 0;
S.size = STACK_INIT_SIZE;
S.increment = STACKINCREMENT;
return OK;
}
// 判栈 S 是否为空栈
Status StackEmpty(SqStack S) {
if (S.top == 0) return TRUE;
else return FALSE;
}
//入栈函数
Status Push(SqStack& S, ElemType e) {
ElemType* newbase;
if (S.top >= S.size) {
newbase = (ElemType*)realloc(S.base, (S.size + S.increment) * sizeof(ElemType));
if (NULL == newbase) return OVERFLOW;
S.base = newbase;
S.size += S.increment;
}
//将余数压入栈
S.base[S.top++] = e;
return OK;
}
//出栈函数
Status Pop(SqStack& S, ElemType& e) {
if (S.top == 0) return ERROR;
S.top--;
e = S.base[S.top];
return OK;
}
//十进制转n进制函数
int main() {
SqStack S;
int n, d;
ElemType e;
printf("输入要转换的数:");
scanf("%d", &n);
printf("输入要转换的进制:");
scanf("%d", &d);
InitStack(S);
while (n != 0) {
Push(S, n % d);//余数压住栈
n /= d;
}
while (FALSE == StackEmpty(S)) {
Pop(S, e);//余数出栈
printf("%d", e);
}
return 0;
}
相关文章
- 八个步骤让你的企业“数据化”
- 给你的 Linux 桌面披上 Windows 8 的外衣
- Bash脚本15分钟进阶教程
- SCL项目:在Red Hat系列系统上安装同一软件的不同版本
- 2017年即将过半 免费OA系统行业OA厂商大转变
- 敢偷用我的WiFi? 看我怎么治你
- 如何在Linux命令行下查询字典?
- Xperience UI 设计理念:优雅的 Linux 桌面设计欣赏
- 用于记录一些东西,避免遗忘
- Linux硬件信息命令大全
- 值得关注:Ubuntu 14.04服务器版提供了虚拟化、自动化、存储相关更新
- How AI can fight the phenomenon of fake news
- 如何关闭一个开放的DNS解析器
- 五个你可能不了解的killall选项
- 如何在Linux中“快速阅读”?
- 图解css3:核心技术与案例实战. 2.2 基本选择器
- rhel源更换为centos源
- 图解css3:核心技术与案例实战. 1.2 浏览器对CSS3的支持状况
- 图解css3:核心技术与案例实战. 1.3 渐进增强
- 如何给任意一款 Linux 发行版添加漂亮的字体