c语言_数制转换_任意进制间互相转换

一、任意进制间互相转换(链表结构实现)

#include "stdio.h"
#include"stdlib.h"
#include <math.h>

typedef struct node
{
	char elem;
	struct node *next;
}stackLink;

stackLink *stackPush(stackLink *top, char elem)
{
	stackLink *nodeLink = (stackLink*)malloc(sizeof(stackLink));
	nodeLink->elem = elem;
	nodeLink->next = top;
	top = nodeLink;
	return top;
}
stackLink *stackPop(stackLink *top)
{
	if (top == NULL)
	{
		printf("栈为空,无法出栈\n");
		return top;
	}
	printf("%c\n", top->elem);
	stackLink *nodefree = top;
	top = top->next;
	free(nodefree);
	return top;
}

char charToHex( char bChar){
	if ((bChar >= 0x30) && (bChar <= 0x39))
		bChar -= 0x30;
	else if ((bChar >= 0x41) && (bChar <= 0x46))//大写字母
		bChar -= 0x37;
	else if ((bChar >= 0x61) && (bChar <= 0x66))//小写字母
		bChar -= 0x57;
	else bChar = 0xff;
	return bChar;
}

 char hexToChar(char bHex){
	if ((bHex >= 0) && (bHex <= 9))
		bHex += 0x30;
	else if ((bHex >= 10) && (bHex <= 15))//大写字母
		bHex += 0x37;
	else bHex = 0xff;
	return bHex;
}

int numToDec(int numForm)
{
	char Arry[32] = {'\0',};
	printf("请输入数据:");
	scanf_s("%s",Arry,32);
	printf("输入的%d进制数据为:%s\n",numForm,Arry);
	int numDec=0;
	int i = 0;
	while (Arry[i] != '\0')
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		numDec += ((charToHex(Arry[j]) * (pow((float) numForm,  (i-j-1)))));

	}
	
	printf("十进制数为:%d\n", numDec);
	return numDec;
}

 char *numToOutputForm(int numDec, int outputNumberForm)
{
	char *numArry = ( char*)malloc(sizeof(char));
	numArry[0] = hexToChar(numDec%outputNumberForm);
	numDec = numDec / outputNumberForm;
	int numCounter = 0;
	for (int i = 1;; i++)
	{
		if (numDec!=0)
		{
			numArry = (char*)realloc(numArry, sizeof(char)*(i+1));
			numArry[i] = hexToChar(numDec%outputNumberForm);
			numDec = numDec / outputNumberForm;
		}
		else
		{
			numArry = (char*)realloc(numArry, sizeof(char)*(i + 1));
			numArry[i] = '\0';
			numCounter = i + 1;
			break;
		}

	}
	char *pArr = (char *)malloc(numCounter*sizeof(char));
	pArr[(numCounter)-1] = numArry[(numCounter)-1];
	for (int i = 0; i < (numCounter)-1; i++)
	{
		pArr[i] = numArry[(numCounter)-2 - i];
	}
	free(numArry);

	return pArr;
}

int main()
{
	stackLink *top = NULL;
	int numDec = 0;
	int inputNumForm = 10;
	int outputNumForm = 10;
	printf("请输入输入数据的进制为:");
    scanf_s("%d", &inputNumForm);
	printf("输入数据的进制为:%d\n", inputNumForm);
	numDec = numToDec(inputNumForm);
	printf("数据转换进制为:");
	scanf_s("%d", &outputNumForm);
	int numCounter = -1;
	char *num = numToOutputForm(numDec, outputNumForm);
	printf("%s", num);
	printf("\n");
	free(num);
	return 0;

}

二、任意进制间互相转换(栈结构实现)

#include "stdio.h"
#include"stdlib.h"
#include <math.h>

typedef struct node
{
	char elem;
	struct node *next;
}stackLink;

stackLink *stackPush(stackLink *top, char elem)
{
	stackLink *nodeLink = (stackLink*)malloc(sizeof(stackLink));
	nodeLink->elem = elem;
	nodeLink->next = top;
	top = nodeLink;
	return top;
}
stackLink *stackPop(stackLink *top)
{
	if (top == NULL)
	{
		printf("栈为空,无法出栈\n");
		return top;
	}
	printf("%c", top->elem);
	stackLink *nodefree = top;
	top = top->next;
	free(nodefree);
	return top;
}

char charToHex( char bChar){
	if ((bChar >= 0x30) && (bChar <= 0x39))
		bChar -= 0x30;
	else if ((bChar >= 0x41) && (bChar <= 0x46))//大写字母
		bChar -= 0x37;
	else if ((bChar >= 0x61) && (bChar <= 0x66))//小写字母
		bChar -= 0x57;
	else bChar = 0xff;
	return bChar;
}

 char hexToChar(char bHex){
	if ((bHex >= 0) && (bHex <= 9))
		bHex += 0x30;
	else if ((bHex >= 10) && (bHex <= 15))//大写字母
		bHex += 0x37;
	else bHex = 0xff;
	return bHex;
}

int numToDec(int numForm)
{
	char Arry[32] = {'\0',};
	printf("请输入数据:");
	scanf_s("%s",Arry,32);
	printf("输入的%d进制数据为:%s\n",numForm,Arry);
	int numDec=0;
	int i = 0;
	while (Arry[i] != '\0')
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		numDec += ((charToHex(Arry[j]) * (pow((float) numForm,  (i-j-1)))));

	}
	
	printf("十进制数为:%d\n", numDec);
	return numDec;
}

stackLink *numToOutputForm(int numDec, int outputNumberForm)
{
	 stackLink *topStack = NULL;
	 topStack = stackPush(topStack, hexToChar(numDec%outputNumberForm));
	 numDec = numDec / outputNumberForm;
	int numCounter = 0;
	for (int i = 1;; i++)
	{
		if (numDec!=0)
		{
			topStack = stackPush(topStack, hexToChar(numDec%outputNumberForm));
			numDec = numDec / outputNumberForm;
		}
		else
		{
			break;
		}

	}
	
	return topStack;
}

int main()
{
	stackLink *top = NULL;
	int numDec = 0;
	int inputNumForm = 10;
	int outputNumForm = 10;
	printf("请输入输入数据的进制为:");
    scanf_s("%d", &inputNumForm);
	printf("输入数据的进制为:%d\n", inputNumForm);
	numDec = numToDec(inputNumForm);
	printf("数据转换进制为:");
	scanf_s("%d", &outputNumForm);
	int numCounter = -1;
	stackLink *topStack = numToOutputForm(numDec, outputNumForm);
	while (topStack != NULL)
	{
		topStack = stackPop(topStack);
	}
	printf("\n");
	return 0;

}
发布了142 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38293453/article/details/104346009