# 第二次博客作业: 函数+进制转换器v1.0beta

十六进制对二进制八进制的转换可以通过6选项后进行1.2选项实现。

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

void mune()
{
printf("----------------------------\n");
printf("进制转换\n");
printf("1、十进制转换二进制\n");
printf("2、十进制转换八进制\n");
printf("3、十进制转换十六进制\n");
printf("4、二进制转换十进制\n");
printf("5、八进制转换十进制\n");
printf("6、十六进制转换十进制\n");
printf("0、    退     出\n");
printf("----------------------------\n");
}

void TenToTwo()
{
int num, c, i = 0, g;
int n = 2;
int a[32];
printf("输入一个整数：");
scanf_s("%d", &num);
g = num;
if (num < 0)
num = -num;
while (num > 0)
{
c = (num % n);
a[i] = c;
num = num / n;
i++;
}
printf("十进制数转换成二进制数是：");
if (g == 0)
printf("0");
if (g < 0)
printf("-");
for (i--; i >= 0; i--)           //将数组倒序输出
printf("%d", a[i]);
printf("\n");
}

void TenToEight()
{
int num, c, i = 0,g;
int n = 8;
int a[32];
printf("输入一个整数：");
scanf_s("%d", &num);
g = num;
if (num < 0)
num = -num;
while (num > 0)
{
c = (num % n);
a[i] = c;
num = num / n;
i++;
}
printf("十进制数转换成八进制数是：");
if (g ==0)
printf("0");
if (g < 0)
printf("-");
for (i--; i >= 0; i--)           //将数组倒序输出
printf("%d", a[i]);
printf("\n");
}

void TenToSixteen()
{
char ai[] = "0123456789ABCDEF";
char h[16];
int i = 0;
int j = 0;
int num = 0, g;
printf("输入一个整数：");
scanf_s("%d", &num);
g = num;
if (num < 0)
num = -num;
while (num)
{
h[i++] = ai[num % 16];   //对十进制数求余并最终与h数组中的字符匹配
num = num / 16;
}
printf("十进制数转换成十六进制数是：");
if (g < 0)
printf("-");
for (j = i - 1; j >= 0; --j)
printf("%c", h[j]);
printf("\n");
}

void TwoToTen()
{
long long n;
int sum = 0, i = 0, m,count=0;
printf("输入一个二进制数: ");
scanf_s("%lld", &n);
while (n != 0)
{     m = n % 10;
if (m > 1||m<-1)
count++;
n /= 10;
sum += m * pow(2, i);
++i;
}
if (count != 0)
{

printf("输入错误！\n");
return TwoToTen();
}
else
printf("二进制数转换为十进制为 %d\n",sum);
}

void EightToTen()
{
int n,;
int sum = 0, i = 0, m,count=0;
printf("输入一个八进制数: ");
scanf_s("%d", &n);
while (n != 0)
{
m = n % 10;
if (m > 7||m<-7)
count++;
n /= 10;
sum += m * pow(8, i);
++i;
}
if (count != 0)
{
printf("输入错误！\n");
return EightToTen();
}
else
printf("八进制数转换为十进制为 %d\n", sum);
}

void SixteenToTen()
{
char s[50], a[50];
printf("输入一个十六进制数：");
scanf_s("%s", s);
int t, i, count = 0, j = 0;
long sum = 0;
if (s[0] == '-')
{
for (i = 0;s[i];i++)
{
a[i] = s[i + 1];
}
for (i = 0;s[i];i++)
{
s[i] = a[i];
}
j = 1;
}
for (i = 0;s[i];i++)
{
if (s[i] > 'F')
count++;
}
if (count == 0)
{

for (i = 0;s[i];i++)
{
if (s[i] <= '9')
t = s[i] - '0';
else
t = s[i] - 'A' + 10;
sum = sum * 16 + t;
}
printf("十六进制数转换为十进制为：");
if (j == 1)
{
printf("-");
}
printf("%d\n",sum);
}
else
{
printf("输入错误！");
return SixteenToTen();
}
printf("\n");
}

int main()
{
int n = 0;
int l = 1;
while (l)
{
mune();
printf("请选择：");
scanf_s("%d", &n);
switch (n)
{
case 1:TenToTwo();
break;
case 2:TenToEight();
break;
case 3:TenToSixteen();
break;
case 4:TwoToTen();
break;
case 5:EightToTen();
break;
case 6:SixteenToTen();
break;
case 0:
l = 0;
break;
default:
printf("您的选择错误!!!\n");
break;
}
}
}

四.主要问题

四.主要问题

scanf("%x",&num);
print("%d",&num);

    char s[50], a[50];
printf("输入一个十六进制数：");
scanf_s("%s", s);
int t, i, count = 0, j = 0;
long sum = 0;
if (s[0] == '-')    //判断输入的是不是负数
{
for (i = 0;s[i];i++)
{
a[i] = s[i + 1];
}
for (i = 0;s[i];i++)
{
s[i] = a[i];       //将字符串前移一位，去掉'-'号
}
j = 1;                 //判断条件判断决定后面是否输出'-'号
}
for (i = 0;s[i];i++)
{
if (s[i] > 'F')      //判断是否为十六进制数
count++;
}
if (count == 0)         //是十六进制数进入循环
{

for (i = 0;s[i];i++)
{
if (s[i] <= '9')
t = s[i] - '0';
else
t = s[i] - 'A' + 10;
sum = sum * 16 + t;
}
printf("十六进制数转换为十进制为：");
if (j == 1)           //是负数，输出'-'号
{
printf("-");
}
printf("%d\n",sum);
}
else                  //不是十六进制数提醒错误并重新调动该函数
{
printf("输入错误！");
return SixteenToTen();
}
printf("\n");

//裁判
bool judge(int n, char num[1000])
{
//二进制判断
if (n == 2)
{
for (int idx = 0; idx < strlen(num); idx++)
if (num[idx] != '1' && num[idx] != '0')return false;
return true;
}
//八进制判断
if (n == 8)
{
for (int idx = 0; idx < strlen(num); idx++)
if (num[idx] < 48 || num[idx] > 55)return false;
return true;
}
//十进制判断
if (n == 10)
{
for (int idx = 0; idx < strlen(num); idx++)
if (num[idx] < 48 || num[idx] > 57)return false;
return true;
}
//十六进制判断
if (n == 16)
{
for (int idx = 0; idx < strlen(num); idx++)
if (num[idx] < 48 || num[idx] > 70)return false;
return true;
}
}

posted @ 2019-11-06 17:28  烟雨萧煜  阅读(214)  评论(0编辑  收藏  举报