⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 szzh.cpp

📁 本程序适用栈来完成不同数制之间的数值转换
💻 CPP
字号:
#include"stdio.h" 
#include"stdlib.h" 
#include"assert.h"
#define null 0 
#define n 10 
struct stack{ 
int *base; 
int *top; 
int stacksize; 
}; 
void initstack(struct stack *s) { 
	s->base=(int*)malloc(20*sizeof(int)); 
	assert(s->base!=NULL); 
	s->top=s->base; 
	s->stacksize=20; 
	return; 
} 
void push(struct stack*s,int e ) { 
	if(s->top-s->base>=s->stacksize) { 
		s->base=(int*)realloc(s->base,(s->stacksize+n)*sizeof(int)); 
	    assert(s->base!=NULL);
		s->top=s->base+s->stacksize; 
		s->stacksize+=n; 
	} 
	*(s->top)++=e; 
	return; 
} 
void pop(struct stack *s) {
	int e;
	if(s->top==s->base) return; 
	e=*--s->top; printf("%d",e); 
	return; 
} 
void clearstack(struct stack*s) { 
	if(s->top==s->base) return; 
	s->top=s->base; 
	return; 
} 
void conversion(int m,int d) {
	struct stack s; 
	initstack(&s); 

	while(m) {
		push(&s,m%d); 
		m=m/d; 
	} 
    while(s.top>s.base){
		pop(&s);
	}
} 
void main() {
	int x; 
	int d;
	printf("\nplease input a num (radix 10):");
	scanf("%d",&x); 
	printf("\nplease input a num which decide the conversion result:"); 
	scanf("%d",&d); 
	printf("\n after conversion (10)%d to (%d):",x,d); 
	conversion(x,d); 
	printf("\n"); 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -