数制转换.cpp

来自「数据结构学习用到的一些程序!!里面有二叉树相关的几个」· C++ 代码 · 共 81 行

CPP
81
字号
#include<stdio.h>
#include <stdlib.h>
typedef int dataType;

typedef struct stackNode
{
    dataType data;
    struct stackNode *next;
}stackNode;

typedef struct linkStack
{
    stackNode *top;
}linkStack;

void initStack(linkStack *s)
{
    s->top=NULL;
}

void push(linkStack *s,dataType x)
{
     stackNode *p = (stackNode *)malloc(sizeof(stackNode));
     p->data=x;
     p->next=s->top;
     s->top=p;  /*top is point to top struct stackNode of the stack.*/
}

dataType pop(linkStack *s)
{
     dataType tmp;
     stackNode *p=s->top;
     if(s->top==NULL)
	 {
        printf("This seqStack is empty and exit.\n");
        exit(0);
	 }
	 else
	 {
        tmp=s->top->data;
        s->top=s->top->next;
        free(p);
        return tmp;
	 }
}

int stackEmpty(linkStack *s)
{
    return s->top==NULL;
}

void multiBaseOutput(int x,int y)
{
     int i,n,b;
     linkStack s;
     n=x;
     b=y;
     initStack(&s);
     while(n)
	 {
          push(&s,n%b);
          n=n/b;
	 }
     printf("The result of %d used by %d is: ",x,y);
     while(!stackEmpty(&s))
	 {
          i=pop(&s);
          printf("%d",i);
	 }
     printf("\nThis multiBase is over!\n");
}

int main(void)
{
	int m,n;
	printf("请输入要转换的数值及进制\n");
	scanf("%d%d",&m,&n);
	multiBaseOutput(m,n);
} 
 
 

⌨️ 快捷键说明

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