作业帮做-十进制转任意进制
2018-11-09 本文已影响29人
朽木自雕也
实验目的
对于输入的任意一个非负十进制整数,输出与其等值的其他进制数(二进制、八进制或十六 进制)。
实验内容
- 建立模型,确定存储结构;
- 对任意十进制数,实现进制转换问题。
实验步骤
- 分析实验需求并简历模型,使用一个单向链表实现可扩展栈结构模型,代码如下:
typedef struct Stack {
int data;
struct Stack *next;
}DataStack, *StackLink;
- 声明进制转换对外接口,代码如下:
//十进制转2进制
StackLink transfer(int x);
//十进制转4进制
StackLink transfer4(int x);
//十进制转8进制
StackLink transfer8(int x);
//十进制转16进制
StackLink transfer16(int x);
- 实现内部进制转换,代码如下:
//十进制转2进制
StackLink transfer(int x) {
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<2) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%2;
x = x/2;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
//十进制转4进制
StackLink transfer4(int x) {
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<4) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%4;
x = x/4;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
//十进制转8进制
StackLink transfer8(int x) {
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<8) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%8;
x = x/8;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
//十进制转16进制
StackLink transfer16(int x) {
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<16) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%16;
x = x/16;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
- 代码优化阶段,声明一个枚举类型的 SystemType 作为外部传入的一个进制转换类型,代码如下:
//进制转换类型
typedef enum : NSUInteger {
//转成二进制
System_2,
//转成四进制
System_4,
//转成八进制
System_8,
//转成十六进制
System_16
} SystemType;
从新定义对外接口 transfer,调用接口需要传入两个参数,一个是十进制数值,另一个是转换成的目标类型数据,接口如下:
//进制转换方法
StackLink transfer(int x, SystemType type);
- 实现内部进制转换,代码如下:
//进制转换方法
StackLink transfer(int x, SystemType type) {
int system = 2;
switch (type) {
case System_2:
system = 2;
break;
case System_4:
system = 4;
break;
case System_8:
system = 8;
break;
case System_16:
system = 16;
break;
default:
return NULL;
break;
}
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<system) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%system;
x = x/system;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
- main 函数中调用,代码如下:
int main(int argc, const char * argv[]) {
//十进制数a
int a = 0;
//临时变量
StackLink p = NULL;
printf("请输入一个十进制数:\n");
scanf("%d",&a);
//二进制
StackLink link2 = transfer(a, System_2);
p = link2->next;
printf("对应二进制:");
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应四进制:");
//四进制
StackLink link4 = transfer(a, System_4);
p=link4->next;
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应八进制:");
//八进制
StackLink link8 = transfer(a, System_8);
p=link8->next;
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应十六进制:");
//十六进制
StackLink link16 = transfer(a, System_16);
p=link16->next;
while (p) {
int t = p->data;
switch (t) {
case 15:
printf("%c",'F');
break;
case 14:
printf("%c",'E');
break;
case 13:
printf("%c",'D');
break;
case 12:
printf("%c",'C');
break;
case 11:
printf("%c",'B');
break;
case 10:
printf("%c",'A');
break;
default:
printf("%d",t);
break;
}
p=p->next;
}
printf("\n");
return 0;
}
- 此时整体代码是这样的,方便各位读者:
//进制转换类型
typedef enum : NSUInteger {
System_2,
System_4,
System_8,
System_16
} SystemType;
//链表栈结构模型
typedef struct Stack {
int data;
struct Stack *next;
}DataStack, *StackLink;
//进制转换方法
StackLink transfer(int x, SystemType type) {
int system = 2;
switch (type) {
case System_2:
system = 2;
break;
case System_4:
system = 4;
break;
case System_8:
system = 8;
break;
case System_16:
system = 16;
break;
default:
return NULL;
break;
}
StackLink link = (StackLink)malloc(sizeof(DataStack));
link->next=NULL;
int yu = 0;
while (1) {
if (x<system) {
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = x;
pytme->next = link->next;
link->next = pytme;
break;
}
yu = x%system;
x = x/system;
StackLink pytme = (StackLink)malloc(sizeof(DataStack));
pytme->data = yu;
pytme->next = link->next;
link->next = pytme;
}
return link;
}
int main(int argc, const char * argv[]) {
//十进制数a
int a = 0;
//临时变量
StackLink p = NULL;
printf("请输入一个十进制数:\n");
scanf("%d",&a);
//二进制
StackLink link2 = transfer(a, System_2);
p = link2->next;
printf("对应二进制:");
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应四进制:");
//四进制
StackLink link4 = transfer(a, System_4);
p=link4->next;
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应八进制:");
//八进制
StackLink link8 = transfer(a, System_8);
p=link8->next;
while (p) {
printf("%d",p->data);
p=p->next;
}
printf("\n对应十六进制:");
//十六进制
StackLink link16 = transfer(a, System_16);
p=link16->next;
while (p) {
int t = p->data;
switch (t) {
case 15:
printf("%c",'F');
break;
case 14:
printf("%c",'E');
break;
case 13:
printf("%c",'D');
break;
case 12:
printf("%c",'C');
break;
case 11:
printf("%c",'B');
break;
case 10:
printf("%c",'A');
break;
default:
printf("%d",t);
break;
}
p=p->next;
}
printf("\n");
return 0;
}
- 实验输出结果如下
第一次运行:
请输入一个十进制数:
2
对应二进制:10
对应四进制:2
对应八进制:2
对应十六进制:2
第二次运行:
请输入一个十进制数:
15
对应二进制:1111
对应四进制:33
对应八进制:17
对应十六进制:F
第三次运行:
请输入一个十进制数:
17
对应二进制:10001
对应四进制:101
对应八进制:21
对应十六进制:11
第四次运行:
请输入一个十进制数:
56
对应二进制:111000
对应四进制:320
对应八进制:70
对应十六进制:38
第五次运行:
请输入一个十进制数:
999999
对应二进制:11110100001000111111
对应四进制:3310020333
对应八进制:3641077
对应十六进制:F423F