zoukankan      html  css  js  c++  java
  • 十进制转n进制

    #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;
    }
    
  • 相关阅读:
    php 获取文件的md5
    php 获取远程文件大小
    chrome 浏览器,大屏显示
    Mac 中Java项目打包上线
    如何在苹果M1芯片 (Apple Silicon) 上安装 JDK 环境
    Mysql 替换数据中的部分内容,比如迁移服务器,需要修改图片地址
    docker安装指定版本minio
    docker 查询镜像并删除
    docker 容器名称已存在
    docker 安装minio
  • 原文地址:https://www.cnblogs.com/mzdljgz/p/14105508.html
Copyright © 2011-2022 走看看