十进制转化二进制系列问题

//十进制转换成二进制 
#include<iostream>
using namespace std;
int main()
{
	int a[999],n;
	while(cin>>n)
	{
		int i=0,k;
		while(n!=0)
		{
			a[i++]=n%2;
			n/=2;
		}
		for(k=i-1;k>=0;k--)
		{		
			cout<<a[k];		
		}
		cout<<endl;
	}
	return 0;
}

Problem Description      hduoj:2031
输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11

#include<iostream>
using namespace std;
int main()
{
    int m,n,x,p;
    char a[1000];
    char b[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    while(cin>>m>>n)
    {
        int i;
        if(m<0)
        x=-1;
        else x=1;
        p=m*x;
        for(i=0;i>=0;i++)
        {
            a[i]=b[p%n];
            p=p/n;
            if(p==0)
            break; 
        }
        if(m<0)
        cout<<"-";
        for(;i>=0;i--)
        {
            cout<<a[i];
        }
        cout<<endl;
    } 
    return 0;
} 


十进制转换二进制顺序栈代码实现

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h> 
#define M 100
typedef int datatype;

typedef struct{
    datatype data[M];
    int top; 
}seqstack;
    
void InitStack(seqstack *s){   //初始化顺序栈 
    s->top=-1;
}

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

seqstack *push(seqstack *s,datatype x){//元素入栈函数 
   if (s->top==M-1) return NULL;
   else
    {  
		s->top++;
		s->data[s->top]=x;
	}
 	return s; 
}

datatype pop(seqstack *s){ //元素出栈函数 
 	  datatype x;
 	  x=s->data[s->top];
	  s->top--;  
	  return x;
} 

int main()
{                 // 对于输入的任意一个非负十进制整数,打印输出与其等值的二进制数
 	seqstack *s;
 	datatype n,e; 
	 int i=0,j=0;
	 scanf("%u",&n);  // 输入非负十进制整数n 
// 	s=(struct seqstack*)malloc(sizeof(int));
	s=(seqstack*)malloc(sizeof(seqstack));
 	if (s==NULL) exit(0); 
 
 	InitStack(s); // 初始化栈
 	while(n) // 当n不等于0
 	{
	 	push(s,n%2); // 入栈n除以2的余数(2进制的低位)
 		 i++;         // 统计入栈元素个数 
  		n=n/2;
 	 } 
 	 while(j<i) // 输出二进制 
 	{
		 j++; 
  		e=pop(s); // 弹出栈顶元素且赋值给e
  		printf("%d",e);
  	 }
} 

十进制转换二进制链栈代码实现

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h> 
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node *next;
}linkstack;

void push(linkstack *s,datatype x)
{//元素入栈函数 
	linkstack *p;
 	p=(linkstack *)malloc(sizeof(linkstack));
	p->data=x;
	 p->next=s->next;
	 s->next=p;
} 

int pop(linkstack *top,int *x) //将x弹出链栈top并将值送入x中
{
	linkstack *temp;
	temp=top->next;
	if(top->next==NULL)
	 return(0);
	else
	{
 		*x=temp->data;
 		top->next=temp->next;
 		free(temp);
	 	return(1);
	}
} 
int main()
{
 	datatype e,n;int i=0;int j=0; 
 	linkstack *q;
 	q=(linkstack *)malloc(sizeof(linkstack));
 	if (q==NULL) exit(0);
 	scanf("%u",&n);
 	while(n) // 当n不等于0
 	{
	 	push(q,n%2); // 入栈n除以2的余数(2进制的低位)
 		i++; // 统计入栈元素个数 
		 n=n/2;
 	}
	 while(j<i) // 输出二进制 
	 { 	
	 	j++; 
 		pop(q,&e);
	 	printf("%d",e);
 	} 
 	printf("\n");
 	system("pause");
	return 0;
}

十进制转换任意进制顺序栈代码实现

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h> 
#define M 100
typedef int datatype;
typedef struct{
    datatype data[M];
    int top; 
}seqstack;
void InitStack(seqstack *s){   //初始化顺序栈 
    s->top=-1;
  }
  
int StackEmpty(seqstack *s){   //判断栈是否为空 
     if (s->top>=0) return 1;
    else return 0;
    }
    
seqstack *push(seqstack *s,datatype x){//元素入栈函数 
 if (s->top==M-1) return NULL;
 else {  s->top++;s->data[s->top]=x;}
 return s; }
datatype pop(seqstack *s){ //元素出栈函数 
 datatype x;
 x=s->data[s->top]; s->top--; return x;} 
 
 
int main()
{                
 	seqstack *s;
 	datatype e,n,w; 
 	double t,k;
 	int i=0,j=0;
 	printf("请输入所要转换的数字:"); 
	scanf("%lf",&t);
	printf("请输入所以转换的进制:"); 
	scanf("%d",&w);
	n=(int)t;
	k=t-n;
 	s=(seqstack*)malloc(sizeof(seqstack));
 	if (s==NULL) exit(0); 
 
 	InitStack(s); // 初始化栈
 	if(n==0) printf("0");
 	while(n) 
 	{push(s,n%w); 
  	i++;          
  	n=n/w;
 	}
 	while(j<i) 
 	{j++; 
  	e=pop(s);
	if(e>=10)
	{
		if(e==10) printf("A");
		else if(e==11) printf("B");
		else if(e==12) printf("C");
		else if(e==13) printf("D");
		else if(e==14) printf("E");
		else if(e==15) printf("F");
	}
	 // 弹出栈顶元素且赋值给e
  else 	printf("%d",e); 
  	}
  	if(k)
  	{
  		printf(".");
  		int i=0;
  		while(fabs(k)>1e-6&&i<10)
  		{
  			int x;
  			k=k*w;
  			x=(int)k;
  			push(s,x);
  			pop(s);
  			if(x>=10)
  			{
  				if(x==10) printf("A");
			else if(x==11) printf("B");
			else if(x==12) printf("C");
			else if(x==13) printf("D");
			else if(x==14) printf("E");
			else if(x==15) printf("F");
  				
			  }
  			printf("%d",x);
  			k=k-x;
  			i++;
  		}
  		printf("\n");
	  }
} 

十进制转换任意进制链栈代码实现

#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
	datatype data;
	struct node *next;
}*linkstack;
 
//入栈
int Push(linkstack *top,datatype x)
{
	linkstack s=(linkstack)malloc(sizeof(struct node));
	if(s==NULL)
		return 0;
	s->data=x;
	s->next=(*top);
	(*top)=s;
	return 1;
}
 
//判空
int Empty(linkstack top)//判断栈是否为空
{
	if(top==NULL)
		return 1;
	return 0;
}
//出栈
int Out(linkstack *top,datatype *x)//出栈
{
	if(top!=NULL)
	{
		linkstack p=(*top);
		(*x)=(*top)->data;
		(*top)=(*top)->next;
		free(p);
		return 1;
	}
	return 0;
}
//十进制整数转换为其他进制数
void Transform(int num,int mode)//实现进制转换
{
	int h;
	linkstack top=NULL;
	printf("转化结果:");
	if(num>0)
	{
		while(num!=0)
		{
			h=num%mode;//取余
			Push(&top,h);
			num=num/mode;//取整
		}
		while(!Empty(top))
		{
			Out(&top,&h);
			printf("%d",h);
		}
		printf("\n");
	}
	else if(num<0)//当输入数字小于0
	{
		printf("-");
		num=num*(-1);//将负数装换为正数
		while(num!=0)
		{
			h=num%mode;
			Push(&top,h);
			num=num/mode;
		}
		while(!Empty(top))
		{
			Out(&top,&h);
			printf("%d",h);
		}
		printf("\n");
	}
	else
		printf("%d\n",0);
}
int main()
{
	int num,mode;
	printf("请输入要转化的数:");
	scanf("%d",&num);
	printf("输入要转换的进制:");
	scanf("%d",&mode);
	if(mode==0||mode==1)//判断要转换的进制是否合法
	{
		printf("转换进制无效\n\n");
		return 0;
	}
	Transform(num,mode);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流萤数点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值