利用栈实现二进制到十进制、八进制的转换

二进制----->十进制

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 1024     //初始化栈有1024个空间 
#define ADD_MAXSIZE 100  //栈内存不够时增加100个空间 

typedef char ElemType;


typedef struct
{
	ElemType *top;   //指向栈顶的指针变量 
	ElemType *base;  //指向栈底 的指针变量 
	int stackSize;   //栈可使用的最大容量 
}SeqStack;

//初始化
void InitStack(SeqStack *s)
{
	s->base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
	if(s->base==NULL)
		exit(0);
	s->top=s->base;
	s->stackSize=MAXSIZE;
} 

//入栈
void Push(SeqStack *s,ElemType e)
{
	if(s->top-s->base>=s->stackSize)
	{
		s->base=(ElemType*)realloc(s->base,(ADD_MAXSIZE+s->stackSize)*sizeof(ElemType));
		if(s->base==NULL)
			exit(0);
	}
	
	*(s->top)=e;
	s->top++;
 } 

//出栈
ElemType Pop(SeqStack *s)
{
	if(s->top==s->base)
	{
		printf("占空了");
	}
	return *--(s->top);
 } 

//计算栈当前的容量 
int StackLen(SeqStack s)
{
	return (s.top-s.base);
}


int main()
{
	ElemType c;
	SeqStack s;
	int len,i,sum=0;
	InitStack(&s);
	printf("请输入二进制数,输入#停止\n");
	scanf("%c",&c);
	while(c!='#')
	{
		Push(&s,c);
		scanf("%c",&c);
	}
	getchar();
	len=StackLen(s);
	printf("栈的容量为:%d\n",len);
	for(i=0;i<len;i++)
	{
		c=Pop(&s);
		sum+=(c-48)*pow(2,i);
	}
	printf("十进制数为:%d",sum);
	return 0;
}

二进制------>八进制

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 1024    //初始化栈有1024个空间 


typedef char ElemType;


typedef struct
{
	ElemType *top;   //指向栈顶的指针变量 
	ElemType *base;  //指向栈底 的指针变量 
	int stackSize;   //栈可使用的最大容量 
}SeqStack;

//创建栈
void InitStack(SeqStack *s)
{
	s->base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
	if(!s->base)
		exit(0);
	s->top=s->base;
	s->stackSize=MAXSIZE;
} 
 

//判断栈是否为空
int Isempty(SeqStack *s)
{
	if(s->base==s->top)
		return 0;
	return 1;
} 



//出栈 
int  SeqStack_Pop(SeqStack *s)
{
	if(Isempty(s))
	{
		return *--(s->top);
	}
	printf("栈为空返回失败\n");
	return -1;
}

//入栈
void Stack_Push(SeqStack *s,ElemType vue)
{
	if(s->top-s->base>=MAXSIZE)
	{
		s->base=(ElemType*)realloc(s->base,2*MAXSIZE*sizeof(ElemType));
		s->top=s->base+s->stackSize;
		s->stackSize=MAXSIZE*2;
		
	}
	*(s->top)=vue;	
	s->top++;
} 





int main()
{
	SeqStack s1,s2;
	InitStack(&s1);
	InitStack(&s2);
	ElemType e;
	int num=0;
	printf("请输入一个二进制数:输入#截止");
	scanf("%c",&e); 
	while(e!='#')
	{
		Stack_Push(&s1,e);
		scanf("%c",&e);
	}
	getchar();
	while(s1.base!=s1.top)
	{
		for(int i=0;i<3;i++)
		{
			e=SeqStack_Pop(&s1);
			num+=(e-48)*pow(2,i);
		}
		Stack_Push(&s2,num);
	}
	while(s2.base!=s2.top)
	{
		e=SeqStack_Pop(&s2);
		printf("%d ",e);
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言利用实现十进制转换二进制八进制、十六进制的代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int top; int capacity; char* array; } Stack; Stack* createStack(int capacity) { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->top = -1; stack->capacity = capacity; stack->array = (char*)malloc(capacity * sizeof(char)); return stack; } int isFull(Stack* stack) { return stack->top == stack->capacity - 1; } int isEmpty(Stack* stack) { return stack->top == -1; } void push(Stack* stack, char item) { if (isFull(stack)) { printf("Stack is full\n"); } else { stack->array[++stack->top] = item; } } char pop(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return 0; } else { return stack->array[stack->top--]; } } char peek(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return 0; } else { return stack->array[stack->top]; } } void convertDecimalToBinary(int decimal) { Stack* stack = createStack(100); while (decimal > 0) { int rem = decimal % 2; push(stack, rem + '0'); decimal /= 2; } printf("Binary: "); while (!isEmpty(stack)) { printf("%c", pop(stack)); } printf("\n"); free(stack->array); free(stack); } void convertDecimalToOctal(int decimal) { Stack* stack = createStack(100); while (decimal > 0) { int rem = decimal % 8; push(stack, rem + '0'); decimal /= 8; } printf("Octal: "); while (!isEmpty(stack)) { printf("%c", pop(stack)); } printf("\n"); free(stack->array); free(stack); } void convertDecimalToHexadecimal(int decimal) { Stack* stack = createStack(100); while (decimal > 0) { int rem = decimal % 16; if (rem < 10) { push(stack, rem + '0'); } else { push(stack, rem - 10 + 'A'); } decimal /= 16; } printf("Hexadecimal: "); while (!isEmpty(stack)) { printf("%c", pop(stack)); } printf("\n"); free(stack->array); free(stack); } int main() { int decimal; printf("Enter a decimal number: "); scanf("%d", &decimal); convertDecimalToBinary(decimal); convertDecimalToOctal(decimal); convertDecimalToHexadecimal(decimal); return 0; } ``` 在这个程序中,我们使用了一个`Stack`结构体,用于存储转换后的数字。`createStack`函数用于创建一个,`isFull`函数用于检查是否已满,`isEmpty`函数用于检查是否为空,`push`函数用于向中添加元素,`pop`函数用于从中弹出元素,`peek`函数用于查看顶元素。`convertDecimalToBinary`函数、`convertDecimalToOctal`函数和`convertDecimalToHexadecimal`函数分别用于将十进制数字转换二进制八进制和十六进制数字,并将结果输出到控制台上。 在`main`函数中,我们首先获取用户输入的十进制数字,然后调用上述三个函数分别将其转换二进制八进制和十六进制数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值