• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

(进制转换)数制转换,把一个十进制的数转换为R进制

前情提要

cpp文件

对16进制转换,本文提供2种

本代码可支持,10进制转换为任意进制的数(但不支持数字转变为字母)

可以加入一些负责转换字母的函数,例如下个代码当中的mode16函数,就是负责将10、11、12、13、14、15转换为a、b、c、d、e、f的。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define maxsize 100
typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
#define ERROR -1
#define OK 1
typedef struct
{
	int *base;
	int *top;
	int stacksize;
}sqstack;
status init(sqstack *S)
{//顺序栈的初始化
	S->base=(int *)malloc(sizeof(int));
	if(!S->base) exit(OVERFLOW);
	S->top=S->base;
	S->stacksize=maxsize;
	return OK;
}

status push(sqstack *S,int e)//顺序栈入栈
{//插入元素e为新的栈顶元素
	if(S->top-S->base==S->stacksize) return ERROR;
	*S->top++=e; return OK;
}

char pop(sqstack *S)//顺序栈出栈
{//删除S的栈顶元素,用e返回其值
	int e;
	if(S->top==S->base) return ERROR;
	e=*(--S->top);
	return e;
}

char gettop(sqstack *S)//取栈顶元素
{//返回S的栈顶元素,不修改栈顶指针
	if(S->top!=S->base)
		return *(S->top-1);
}

status StackTraverse(sqstack *S)
{
// 从栈顶到栈底依次输出栈中的每个元素
	if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
	else
	{
		printf("The Stack is: ");
		S->top--;
		while(S->top>=S->base)            
		{
			printf("%d ", *S->top);
			S->top--;               
		}
	}
	printf("\n");
	return OK;
}

bool empty(sqstack *s)//判断栈是否为空
{
	return (s->top==s->base);//为空,返回1
}

void conversion(sqstack *s,int i)
{
	int e,n;
	printf("\n请输入你要转化的数据:");
    scanf("%d",&n);
    while(n)
      {
		  push(s,n%i);    
		  n=n/i;
      }
    while(!empty(s))
      { 
		  e=pop(s);
          printf("%d",e);  
	}
}


int main()
{
	sqstack Sta;int n;
	sqstack *S=&Sta;
	init(S);
	printf("这里是进制转化处理程序\n");
	printf("请输入你想将10进制转换为几进制?\n");
	scanf("%d",&n);
	printf("10进制转化为%d",n);
	conversion(S,n);
	return 0;
}

输出

输入

16
15362

这个代码额外加了个供16进制进制转换的函数

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define maxsize 100
typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
#define ERROR -1
#define OK 1
typedef struct
{
	int *base;
	int *top;
	int stacksize;
}sqstack;
status init(sqstack *S)
{//顺序栈的初始化
	S->base=(int *)malloc(sizeof(int));
	if(!S->base) exit(OVERFLOW);
	S->top=S->base;
	S->stacksize=maxsize;
	return OK;
}

status push(sqstack *S,int e)//顺序栈入栈
{//插入元素e为新的栈顶元素
	if(S->top-S->base==S->stacksize) return ERROR;
	*S->top++=e; return OK;
}

char pop(sqstack *S)//顺序栈出栈
{//删除S的栈顶元素,用e返回其值
	int e;
	if(S->top==S->base) return ERROR;
	e=*(--S->top);
	return e;
}

char gettop(sqstack *S)//取栈顶元素
{//返回S的栈顶元素,不修改栈顶指针
	if(S->top!=S->base)
		return *(S->top-1);
}

status StackTraverse(sqstack *S)
{
// 从栈顶到栈底依次输出栈中的每个元素
	if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
	else
	{
		printf("The Stack is: ");
		S->top--;
		while(S->top>=S->base)            
		{
			printf("%d ", *S->top);
			S->top--;               
		}
	}
	printf("\n");
	return OK;
}

bool empty(sqstack *s)//判断栈是否为空
{
	return (s->top==s->base);//为空,返回1
}

char made16(int e)
{
	char ch='a';
	if(e==10)return ch;
	else if(e==11) return ch+1;
	else if(e==12) return ch+2;
	else if(e==13) return ch+3;
	else if(e==14) return ch+4;
	else if(e==15) return ch+5;
}


void conversion(sqstack *s,int i)
{
	int e,n;
	printf("\n请输入你要转化的数据:");
    scanf("%d",&n);
    while(n)
      {
		  push(s,n%i);    
		  n=n/i;
      }
    while(!empty(s))
      { 
		  e=pop(s);
		  if(e<10)
          printf("%d",e);  
		  else printf("%c",made16(e));
		  
	}
}

int main()
{
	sqstack Sta;int n;
	sqstack *S=&Sta;
	init(S);
	printf("这里是进制转化处理程序\n");
	printf("支持2-16进制的转换\n");
	printf("请输入你想将10进制转换为几进制?\n");
	scanf("%d",&n);
	printf("10进制转化为%d",n);
	conversion(S,n);putchar('\n');
	return 0;
}

输出

mode16函数,转换为16进制的函数,进行了另外一种写法

char made16(int e)
{
	char ch;
	ch=(0<=e&&e<=9)?(char)(e+'0'):(char)(e-10+'A');
	return ch;
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define maxsize 100
typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
#define ERROR -1
#define OK 1
typedef struct
{
	int *base;
	int *top;
	int stacksize;
}sqstack;
status init(sqstack *S)
{//顺序栈的初始化
	S->base=(int *)malloc(sizeof(int));
	if(!S->base) exit(OVERFLOW);
	S->top=S->base;
	S->stacksize=maxsize;
	return OK;
}

status push(sqstack *S,int e)//顺序栈入栈
{//插入元素e为新的栈顶元素
	if(S->top-S->base==S->stacksize) return ERROR;
	*S->top++=e; return OK;
}

char pop(sqstack *S)//顺序栈出栈
{//删除S的栈顶元素,用e返回其值
	int e;
	if(S->top==S->base) return ERROR;
	e=*(--S->top);
	return e;
}

char gettop(sqstack *S)//取栈顶元素
{//返回S的栈顶元素,不修改栈顶指针
	if(S->top!=S->base)
		return *(S->top-1);
}

status StackTraverse(sqstack *S)
{
// 从栈顶到栈底依次输出栈中的每个元素
	if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
	else
	{
		printf("The Stack is: ");
		S->top--;
		while(S->top>=S->base)            
		{
			printf("%d ", *S->top);
			S->top--;               
		}
	}
	printf("\n");
	return OK;
}

bool empty(sqstack *s)//判断栈是否为空
{
	return (s->top==s->base);//为空,返回1
}

char made16(int e)
{
	char ch;
	ch=(0<=e&&e<=9)?(char)(e+'0'):(char)(e-10+'A');
	return ch;
}


void conversion(sqstack *s,int i)
{
	int e,n;
	printf("\n请输入你要转化的数据:");
    scanf("%d",&n);
    while(n)
      {
		  push(s,n%i);    
		  n=n/i;
      }
    while(!empty(s))
      { 
		  e=pop(s);
		  if(e<10)
          printf("%d",e);  
		  else printf("%c",made16(e));
		  
	}
}

int main()
{
	sqstack Sta;int n;
	sqstack *S=&Sta;
	init(S);
	printf("这里是进制转化处理程序\n");
	printf("支持2-16进制的转换\n");
	printf("请输入你想将10进制转换为几进制?\n");
	scanf("%d",&n);
	printf("10进制转化为%d",n);
	conversion(S,n);putchar('\n');
	return 0;
}

posted on 2022-04-12 20:02  王陈锋  阅读(408)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3