# c语言实现一个进制转换程序

#include "stdafx.h"
#include"math.h"
#include"stdlib.h"

int _tmain(int argc, _TCHAR* argv[])
{
void szq(int n);
void qzs(int m);
void szsl(int n);
for (;;){
int choose;
printf_s("                            ---------------------\n");
printf_s("                            请选择你需要的项目：\n");
printf_s("                            1.十进制转二进制\n");
printf_s("                            2.十进制转八进制\n");
printf_s("                            3.十进制转十六进制\n");
printf_s("                            4.二进制转十进制\n");
printf_s("                            5.八进制转十进制\n");
printf_s("                            6.十六进制转十进制\n");
printf_s("                            0.退出程序\n");
printf_s("                            ---------------------\n");
scanf_s("%d", &choose);
int n,m;
if (choose == 1){
n = 2;
szq(n);
}
else if (choose == 2){
n = 8;
szq(n);

}
else if (choose == 3){
n = 16;
szsl(n);

}
else if (choose == 4){
m = 2;
qzs(m);
}
else if (choose == 5){
m = 8;
qzs(m);
}
else if (choose == 6){
int p;
printf("输入一个十六进制数：");
scanf_s("%x", &p);
printf("进制转换结果：%d", p);
}
else if(choose==0){
exit(0);
}
printf("\n");
printf("\n");
printf("\n");
}

return 0;
}
void szq(int n){                    //十进制转换成二、八进制
int num, c, i = 0;
int a[100];
printf("输入一个正整数：");
scanf_s("%d", &num);
while (num>0)
{
c = (num % n);
a[i] = c;
num = num / n;
i++;
}
printf_s("进制转化结果是：");
for (i--; i >= 0; i--)           //将数组倒序输出
printf("%d", a[i]);
printf("\n");

}
void qzs(int m){                    //二、八进制转换成十进制
int i,ws=0,sum=0,e=0,js=0;
int b[100];
printf_s("输入需要转换的进制数位数：");
scanf_s("%d",&ws);
printf_s("按位输入需要转换的进制数(以空格分开）：\n");
for (i = 1; i <= ws; i++){
scanf_s("%d", &b[i]);
}
for (i = ws; i >0; i--){
sum += b[i] * _Pow_int(m, e);
e++;
}
for (i = 1; i <= ws; i++){
if (b[i] > m){
js++;
}
}
if (js == 0){
printf("进制转化结果是：%d\n", sum);
}
else{
printf_s("输入错误！");
}
}
void szsl(int n){                       //十进制转换成十六进制
int num, c, i = 0;
char a[100];
printf("输入一个正整数：");
scanf_s("%d", &num);
while (num>0)
{
c = (num % n);
if (c < 10){
a[i] = c + 48;
}
else{
if (c == 10){
c = 'A';
}
else if (c == 11){
c = 'B';
}
else if (c == 12){
c = 'C';
}
else if (c == 13){
c = 'D';
}
else if (c == 14){
c = 'E';
}
else if (c == 15){
c = 'F';
}
a[i] = c;
}
num = num / n;
i++;
}
printf_s("进制转换结果是：");
for (i--; i >= 0; i--)           //将数组倒序输出
printf("%c", a[i]);
printf("\n");
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客