内容摘要:二到十六进制之间的相互转换代码
此程序是用作二到十六进制的相互转换,用法如下:
gcc advanceChange.c -o advanceChange
./advanceChange 1111 2 10
意思是把二进制1111转换为十进制,结果是15
源代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define oops() {perror("malloc");exit(1);}
typedef struct ooput{
int num;
struct ooput *pre,*next;
}out;
//乘方运算,返回a的b次方的值
int power(int a,int b){
int i;
int x = a;
if (b == 0)
return 1;
else if (b == 1)
return a;
else{
for (i=1;i<b;i++){
a = a * x;
}
return a;
}
}
int translate(int time,char c[],int advance){
int i,ten = 0; //变量ten是用来得到转换成十进制后的值
int arg; //把字符转换成整型后赋给此变量
for (i=0;i<time;i++){
//为字符时如何,为数字时如何
if (isalpha(c[time-i-1])){
//按标准,进制中的字母应该是大写
if (isupper(c[time-i-1]))
arg = c[time-i-1] - 55;
else{
printf("请用大写字母
");
exit(1);
}
}
else
arg = c[time-i-1] - 48;
if (arg > (advance-1)){
printf("书写错误:%s 不是 %d进制
",c,advance);
exit(1);
}
//得到转换成十进制的数值并将其赋给变量ten
ten = ten + (arg * power(advance,i));
}
return ten;
}
//返回n进制转换后的十进制值
int first(char *parament1,char *parament2){
int replay;
//得到n进制的长度,然后将其转换成char型数组,这样才能将其一个一个的进行转换
int time = strlen(parament1);
char c[time];
sprintf(c,"%s",parament1);
//根据对应的进制数,然后将其转换成十进制
if (strcmp(parament2,"2") == 0)
replay = translate(time,c,2);
else if (strcmp(parament2,"3") == 0)
replay = translate(time,c,3);
else if (strcmp(parament2,"4") == 0)
replay = translate(time,c,4);
else if (strcmp(parament2,"5") == 0)
replay = translate(time,c,5);
else if (strcmp(parament2,"6") == 0)
replay = translate(time,c,6);
else if (strcmp(parament2,"7") == 0)
replay = translate(time,c,7);
else if (strcmp(parament2,"8") == 0)
replay = translate(time,c,8);
else if (strcmp(parament2,"9") == 0)
replay = translate(time,c,9);
else if (strcmp(parament2,"10") == 0)
replay = translate(time,c,10);
else if (strcmp(parament2,"11") == 0)
replay = translate(time,c,11);
else if (strcmp(parament2,"12") == 0)
replay = translate(time,c,12);
else if (strcmp(parament2,"13") == 0)
replay = translate(time,c,13);
else if (strcmp(parament2,"14") == 0)
replay = translate(time,c,14);
else if (strcmp(parament2,"15") == 0)
replay = translate(time,c,15);
else if (strcmp(parament2,"16") == 0)
replay = translate(time,c,16);
return replay;
}
//用一个双向链表储存转换后的值,最后返回链表的最后一个结点
out *output(int total,int advance){
//k在这个程序中并无实际意义,这的作用就是为了让程序区分中链表的第一个结点和其它的结点
int k = 1;
out *put,*pf;;
while (total){
if ((put=(out *)malloc(sizeof(out))) == NULL)
oops();
//将取得的模赋给ooput结构中的num变量
put->num = total % advance;
//将值赋给链表中第一个结点时如何,除第一个结点外其它的结点如何
if (k == 1){
put->next = NULL;
put->pre = NULL;
//只所以把put赋给两个指针变量是为了能返回链表的第一个结点
pf = put;
}else{
pf->next = put;
put->pre = pf;
put->next = NULL;
pf = put;
}
total /= advance;
k++;
}
return pf;
}
//将两个n进制相加后的值转换成对应的n进制
out *second(int total,char *parament){
out *head;
if (strcmp(parament,"2") == 0)
head = output(total,2);
else if (strcmp(parament,"3") == 0)
head = output(total,3);
else if (strcmp(parament,"4") == 0)
head = output(total,4);
else if (strcmp(parament,"5") == 0)
head = output(total,5);
else if (strcmp(parament,"6") == 0)
head = output(total,6);
else if (strcmp(parament,"7") == 0)
head = output(total,7);
else if (strcmp(parament,"8") == 0)
head = output(total,8);
else if (strcmp(parament,"9") == 0)
head = output(total,9);
else if (strcmp(parament,"10") == 0)
head = output(total,10);
else if (strcmp(parament,"11") == 0)
head = output(total,11);
else if (strcmp(parament,"12") == 0)
head = output(total,12);
else if (strcmp(parament,"13") == 0)
head = output(total,13);
else if (strcmp(parament,"14") == 0)
head = output(total,14);
else if (strcmp(parament,"15") == 0)
head = output(total,15);
else if (strcmp(parament,"16") == 0)
head = output(total,16);
return head;;
}
//得到链表的最后一个结点,从后向前输出链表内的值
void print(out *head){
while (head != NULL){
//当数值在10-15之间时将其转换成字符并打印
if (head->num > 9){
printf("%c",head->num + 55);
}else
printf("%d",head->num);
head = head->pre;
}
printf("
");
}
int main(int argc,char *argv[]){
int ten;
out *head;
if (argc != 4){
printf("错误:参数太少或太多
");
exit(1);
}
ten = first(argv[1],argv[2]);
head = second(ten,argv[3]);
print(head);
return 0;
}
责编:豆豆技术应用
正在加载评论...
- Linux/Unix新闻
- Linux/Unix入门
- Linux/Unix命令
- Linux/Unix安装
- Linux/Unix编程
- Linux/Unix管理
- Linux/Unix桌面
- Linux/Unix内核
- Linux/Unix软件
- Linux/Unix发行版
- redhat/Fedora
- Ubuntu Linux
- IBM AIX
- FreeBSD
- Solaris
- NetBSD
- SCO Unix
- find基本用法
- ldd命令原理及用法例子
- su和sudo命令的区别与使用技巧
- Linux操作系统下的dd命令技巧
- 关于Top命令的参数详解
- 关于Tar命令的使用
- SSH实用技巧及常用命令使用
- Linux后台执行命令
- VI命令使用技巧集锦
- Vmstat命令列出的属性详解
- 如何查看及修改文件读写权限
- 最大可存储的单文件容量
- ext2/ext3文件系统介绍
- 常用压缩格式的压缩解压方法
- Linux系统的引导过程详细解析
- Configure参数解释说明
- Linux下硬盘和分区的命名方法
- 硬链接与软链接的区别
- 权限和所有权模型
- 存储设备的两种表示方法