Based on a simple C language to emulate win10 binary conversion calculator design

Designed to:

Each design a binary conversion calculator using C language. The figure is win10 calculator shots.
Here Insert Picture Description

Topic Requirements:

  • Write a program to achieve binary, octal, decimal, hexadecimal conversion.
    Tip: The value behind the decimal input letters, 56D is the decimal 56,72O is octal 72, ACH is the hexadecimal AC.
  • Examples
输入:56D
输出:HEX[38]
      DEC[56]
      OCT[70]
      BIN[00111000]
输入:ACH
输出:HEX[AC]
      DEC[172]
      OCT[254]
      BIN[10101100]
输入:72Q
输出:HEX[3A]
      DEC[58]
      OCT[72]
      BIN[00111010]
输入:00011010B
输出:HEX[1A]
      DEC[26]
      OCT[32]
      BIN[00011010]

programming

  • Data input here is not the actual number is hexadecimal, the input takes the form of a string. For example: a binary "11001010B", octal "26Q", a decimal "56D", hex "7CH". After the decimal adding hexadecimal identifier to distinguish.
  • By Funtion (char * array, int len) function is obtained decimal number, i.e. an int. Parameter array to incoming binary data string, which is referred to above as "11001010B" nary this style parameter len is the length of the string.
  • DecToAny (int n, int m) is a function of an arbitrary number of revolutions of the decimal binary number, n is the parameter data input, m is a parameter to be converted into m-ary meaning.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int Funtion(char *array,int len)
{
	int *d = (int*)malloc(len * sizeof(int));
	int num;//进制数
	int sum=0;
	switch (array[len-1])
	{
		case 'B'://2进制转10进制
		{
			for (int i = len - 2; i >= 0; i--)
			{
				sum += (array[i]-'0') * pow(2, len - i - 2);
			}
			break;
		}
		case 'Q'://8进制转10进制
		{
			for (int i = len - 2; i >= 0; i--)
				sum += (array[i] - '0')*pow(8, len - i - 2);
			break;
		}
		case 'D'://将字符串型的数转成int型十进制数
		{
			for (int i = 0; i < len-1; i++)
				sum += (array[i] - '0')*pow(10, len - i - 2);
			break;
		}
		case 'H'://16进制转10进制
		{
			for (int i = len - 2; i >= 0; i--)
			{
				if (array[i] > 47 && array[i] < 58)             //0~9
					sum += (array[i] - 48)*pow(16, len - i - 2);
				else if (array[i] > 64 && array[i] < 71)        //A~F
					sum += (array[i] - 55)*pow(16, len - i - 2);
				else if (array[i] > 96 && array[i] < 103)       //a~f
					sum += (array[i] - 87)*pow(16, len - i - 2);
			}
			break;
		}
		default:break;
	}
	return sum;
}
int DecToAny(int n, int m)// n表示数据,m表示要转成m进制
{
	if (n < m) return n;
	else return DecToAny(n / m, m) * 10 + n % m;
}
int main(void)
{
	int Length;//数组长度
	int s;
	char *Data = (char*)malloc(9 * sizeof(char));//数据字符串数组
	gets(Data);//输入数据
	Length = strlen(Data);
	s=Funtion(Data,Length);//功能函数
	printf("HEX[%X]\nDEC[%d]\nOCT[%o]\nBIN[%d]\n",s,s,s,DecToAny(s,2));
}

operation result

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description

Published 15 original articles · won praise 9 · views 2651

Guess you like

Origin blog.csdn.net/qq_38413498/article/details/104203607