Application of chain stack: convert decimal rational numbers to r-ary numbers

Content: Convert decimal rational numbers to r-ary numbers, and the conversion method is the rolling and dividing method. It is required to be implemented with a chain stack.

Algorithm analysis :

The processing of integers is the same as the previous question. The main function has two inputs, that is, the number to be converted and the base to be converted. The idea of ​​​​the Convert function: first determine whether the number to be converted is positive or negative, and use the if...else statement Respectively realize the conversion of positive and negative numbers, use the remainder and rounding operations, and then use the stack operation to perform division and division.

The difference is that rational numbers need to consider the processing of the decimal part, and the decimal part can also be treated as an integer. You only need to add a decimal point in front of the output

Outline design:

Push() function

Implement push operation

Pop() function

Implement the stack operation

Empty() function

Empty stack judgment

Convert() function

Implement hexadecimal conversion operations

code:

#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);				//s 成为栈顶 
	(*top)=s;
	return 1;
}
//判空
int Empty(linkstack top){
	if(top == NULL)
		return 1;
	return 0;
} 
//出栈,逐个输出余数 
int Pop(linkstack* top,datatype* x){
	if(top != NULL){				//如果栈没空,就输出一个余数,直到空栈 
		linkstack p=(*top);
		(*x)=(*top)->data;
		(*top)=(*top)->next;
		free(p);
		return 1;
	}
	return 0; 
} 
//转换,将十进制有理数数转换为任意进制有理数
void Convert(int integer,int decimal,int mode)
{
	int h;					
	linkstack top = NULL;
	printf("转换结果为: ");
	if(integer>0)
  {
		while(integer != 0)
       {		//处理整数部分 
			h=integer%mode;			//求模得到余数 h 
			Push(&top,h);			//辗转相除,把余数压入栈 
			integer=integer/mode;
		}
		while(!Empty(top))
       {  			//如果不是空栈,出栈 
			Pop(&top,&h);
			printf("%d",h);
		}
		if(decimal != 0)
       {			//若小数部分不为零,处理小数部分 
			printf(".");			//先输出一个小数点,再通过辗转相除法将余数出栈入栈 
	 		while(decimal != 0)
          {
	 			h=decimal%mode;
	 			Push(&top,h);
	 			decimal=decimal/mode;
			 }
			 while(!Empty(top))
          {
			 	Pop(&top,&h);
			 	printf("%d",h);
			 }
		}
	 	else
	 		printf("\n"); 
	}
	else if(integer<0)
  {				//如果有理数是负数,获取到的有理数整数部分integer就是负数
		printf("-");				//将其取正数,输出时在它前面输出一个负号 
		integer=integer*(-1);
		while(integer != 0)
      {
			h=integer%mode;
			Push(&top,h);
			integer=integer/mode;
		}
		while(!Empty(top))
      {
			Pop(&top,&h); 
			printf("%d",h); 
		}
		if(decimal != 0)
        {
	 		while(decimal != 0)
           {
	 			h=decimal%mode;
	 			Push(&top,h);
	 			decimal=decimal/mode;
			 }
			 printf(".");
			 while(!Empty(top))
            {
			 	Pop(&top,&h);
			 	printf("%d",h);
			 }
		 }
	 	else
	 		printf("\n");
 
   }  
	else if(integer == 0)
   {
		if(decimal != 0)
        {
	 		while(decimal != 0)
           {
	 			h=decimal%mode;
	 			Push(&top,h);
	 			decimal=decimal/mode;
			}
			printf("0.");
			while(!Empty(top))
           {
				Pop(&top,&h);
				printf("%d",h);
			}
		}
	 	else
	 		printf("0\n");
	 }
} 
int Push(linkstack* top,datatype x);
int Empty(linkstack top);
int Pop(linkstack* top,datatype* x);
void Convert(int integer,int decimal,int mode);
int main(void)
{
	int integer,mode,decimal;				//定义整型变量有理数的整数部分 integer还有有理数的小数部分 decimal和转换的进制 mode 
	printf("\n请输入要转换的数: "); 
	scanf("%d.%d",&integer,&decimal);
	printf("\n请输入要转换的进制:");
	scanf("%d",&mode);
	Convert(integer,decimal,mode);
	return 0;
} 

operation result:

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_54474126/article/details/121732069