Before realizing the binary conversion, we should first understand the algorithm principle of the binary conversion. Take the decimal conversion to binary as an example: the method adopted is "divide two and take remainder, output in reverse order", so we should output in reverse order with the help of the stack. For rational numbers with decimals, the integer part still follows the rule of "taking the remainder in addition to two and outputting in reverse order", but the decimal part has different binary conversion algorithms. It is the rule of "multiplying two and rounding, outputting in positive order". Because it is outputting in positive order, it can also be printed out through simple circular statements without the help of stack.
When performing binary conversion on integers, the number to be converted is first judged to be positive and negative, and the conversion of positive and negative numbers (all converted to positive integers) is realized through if...else... Algorithm. The idea of conversion is realized by using the remainder and rounding operations in arithmetic operation and rolling Division with the help of stack first in and last out operation.
#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef int datatype; typedef struct node { datatype data; struct node *next; }*linkstack; /*Push */ 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; } /*Air judgment*/ int Empty(linkstack top) { if (top == NULL) return 1; return 0; } /*Out of stack*/ int Pop(linkstack*top, datatype*x) { if (top != NULL) { linkstack p = (*top); (*x) = (*top)->data; (*top) = (*top)->next; free(p); return 1; } return 0; } /*Convert decimal integers to arbitrary hexadecimal numbers*/ void Convert(int num, int mode) { int h; linkstack top = NULL; printf("The conversion result is:"); if (num>0) { while (num != 0) { h = num%mode; Push(&top, h); num = num / mode; } while (!Empty(top)) { Pop(&top, &h); printf("%d", h); } printf("\n"); } else if (num<0) { printf("-"); num = num*(-1); while (num != 0) { h = num%mode; if (h >= 10) { h = h - 10 + 'A'; } Push(&top, h); num = num / mode; } while (!Empty(top)) { Pop(&top, &h); printf("%d", h); } printf("\n"); } else printf("%d\n", 0); } void main() { int num, mode; printf("\n Enter the number of to convert:"); scanf("%d", &num); printf("Enter the hexadecimal to convert:"); scanf("%d", &mode); Convert(num, mode); }
The integer hexadecimal conversion algorithm has the following problems: first, it can only convert integers to hexadecimal without the function of converting decimals. Second, there will be output problems in the process of converting hexadecimal numbers greater than 10. For example, in the following test case, the correct hexadecimal conversion of 999 should be 3e7, but the program outputs 3147, Cannot convert numbers greater than 10 to characters. This problem will be implemented in the conversion of rational numbers to n-ary.
For the rational number to be converted, first separate the decimal and integer parts. The focus of the separation operation is that there will be precision loss during the conversion between int and double. Then judge the positive and negative of the separated decimal and integer parts respectively, and realize the conversion of positive and negative numbers (convert negative numbers to positive numbers) through the if...else... Algorithm.
The decimal part and the integer part adopt different algorithms for binary conversion. The integer part carries out the remainder and rounding operations after dividing with the binary, and the output is in reverse order; The decimal part carries out the remainder and rounding operations after multiplying with hexadecimal, and can be output in positive order.
#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef char datatype; typedef struct node { datatype data; struct node*next; }*linkstack; /*Push */ 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; } /*Air judgment*/ int Empty(linkstack top) { if (top == NULL) return 1; return 0; } /*Out of stack*/ int Pop(linkstack*top, datatype*x) { if (top != NULL) { linkstack p = (*top); (*x) = (*top)->data; (*top) = (*top)->next; free(p); return 1; } return 0; } /*Convert decimal integers to arbitrary hexadecimal numbers*/ void Convert_integer(int num, int mode) { int n; char h; linkstack top = NULL; if (num>0) { while (num != 0) { n = num%mode; h = num%mode +'0';//Converts an int number to a corresponding character if (n >= 10) { n = n - 10 ; h = n + 'A';//Converts an int number greater than 10 to the corresponding character } Push(&top, h); num = num / mode; } while (!Empty(top)) { Pop(&top, &h); printf("%c", h); } } else if (num<0) { printf("-"); num = num*(-1); while (num != 0) { n = num%mode; h = num%mode + '0';//Converts an int number to a corresponding character if (n >= 10) { n = n - 10; h = n + 'A';//Converts an int number greater than 10 to the corresponding character } Push(&top, h); num = num / mode; } while (!Empty(top)) { Pop(&top, &h); printf("%c", h); } } else printf("0"); } /*Convert decimals to arbitrary hexadecimal numbers*/ void Convert_decimal(double num, int mode) { if (num < 0) { num = num*(-1);//Turn to positive } int a; char b; double flag; while (1)//Carry out dead cycle { flag = num * mode; a = flag; if (a >= 10) { b = a - 10 + 'A'; }else{ b = a + '0';//Converts an int number to a corresponding character } printf("%c", b); num = flag - a; if (num==0) { break;//The break statement is used to jump out of the while loop } } } void main() { double num, decimal; int mode, integer; printf("Enter the number of to convert:"); scanf("%lf", &num); //Use precision loss to realize integer and decimal separation integer = num; decimal = num - integer; printf("Enter the hexadecimal to convert:"); scanf("%d", &mode); printf("Conversion results:\n"); Convert_integer(integer, mode); printf("."); Convert_decimal(decimal, mode); //Through the conversion of integer part and decimal part respectively, the final conversion result is spliced }
The problem of character conversion with base number greater than 10 is solved, and the conversion of integer and decimal of rational number is also completed. However, there is still a problem. For negative decimals such as -0.89, the negative sign will be lost after hexadecimal conversion. I hope a big man can help solve it. Please.