进制转换C语言
while(isEmpty()==0)
{
result=result+pop()*pow(jz,times);
times--;
}
printf("%d",result);
}
//填充数字字母表
void setTable(char table[]){
int i = 0;
for(;i<10;i++){
table[i]='0'+i;
int pop();
int isEmpty();
//声明全局变量
Stack *stack;
int main(){
int number,radix;//number-被转换数radix-转换进制
int n,jz;
char table[36];//定于数字字母匹配表
setTable(table);
printf("输入十进制数字:");
scanf("%d",&jz);
printf("输入要转换的数字:");
scanf("%d",&n);
int times=0,result=0;
while(true)
{
push(n%10);பைடு நூலகம்
n=n/10;
times++;
if(n==0)
{
times--;
break;
}
}
printf("结果为:");
}
for(;i<37;i++){
table[i]='A'+i-10;
}
}
//入栈
void push(int number){
stack->num[stack->tap] = number;
stack->tap = stack->tap + 1;
}
//出栈
int pop(){
stack->tap = stack->tap - 1;
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//定义栈结构
typedef struct stack{
int num[50];
int tap;
}Stack;
//声明函数
void setTable(char* table);
void push(int number);
if(number == 0)break;
}
printf("结果为:");
while(isEmpty() == 0){
printf("%c",table[pop()]);
}
putchar(10);//换行
//------------------------------------
printf("输入要转换数字是几进制的:");
scanf("%d",&number);
printf("输入要转换的进制:");
scanf("%d",&radix);
stack = (Stack*)malloc(sizeof(Stack));
stack->tap=0;
//短除法
while(true){
push( number % radix);
number = number/radix;
return stack->num[stack->tap];
}
//判断是否为空
int isEmpty(){
if(stack->tap == 0)return 1;
else return 0;
}