- 论坛徽章:
- 1
|
- /*
- 进制转换,支持二进制,八进制,十进制,16进制, 支持超大数(1000位),目前不支持负数、浮点数(小数)
- 这个程序还可以优化,这里是最朴素的实现。对于不是特别大的数和某些转换,可使用标准库函数,比如strtol,sprintf,atoi,printf等
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define maxhp 1000
- typedef struct _HP HP;
- struct _HP
- {
- int len;
- int s[maxhp];
- };
- void hp2str(HP x, char *s)
- {
- int i;
- for (i = 0; i < x.len; i++)
- {
- s[i] = x.s[x.len - i] + '0';
- }
- s[i] = 0;
- }
- void int2hp(int inte, HP *x)
- {
- if (inte == 0)
- {
- x->len = 1;
- x->s[1] = 0;
- return;
- }
- for (x->len = 0; inte > 0;)
- {
- x->s[++x->len] = inte % 10;
- inte /= 10;
- }
- }
- /* c=a+b */
- void add(const HP a, const HP b, HP *c)
- {
- int i;
- c->s[1] = 0;
- for (i = 1; i <= a.len || i <= b.len || c->s[i]; i++)
- {
- if (i <= a.len)
- c->s[i] += a.s[i];
- if (i <= b.len)
- c->s[i] += b.s[i];
- c->s[i + 1] = c->s[i] / 10;
- c->s[i] %= 10;
- }
- c->len = i - 1;
- if (c->len == 0)
- c->len = 1;
- }
- /* c=a*b */
- void multiply(const HP a, const HP b, HP *c)
- {
- int i, j;
- c->len = a.len + b.len;
- for (i = 1; i < c->len; i++)
- c->s[i] = 0;
- for (i = 1; i <= a.len; i++)
- {
- for (j = 1; j <= b.len; j++)
- {
- c->s[i + j - 1] += a.s[i] * b.s[j];
- }
- }
- for (i = 1; i < c->len; i++)
- {
- c->s[i + 1] += c->s[i] / 10;
- c->s[i] %= 10;
- }
- while (c->s[i])
- {
- c->s[i + 1] = c->s[i] / 10;
- c->s[i] %= 10;
- i++;
- }
- while (i > 1 && !c->s[i])
- i--;
- c->len = i;
- }
- /* 将十进制的str除以to ,dest为商,函数返回余数*/
- int divto(char *str, int to, char *dest)
- {
- int i, j, m,len,len2;
- len = strlen(str);
- m = 0;
- for (i = 0; i < len; i++)
- {
- m = m * 10 + str[i] - '0';
- dest[i] = m / to + '0';
- m %= to;
- }
- j = len;
- for (i = 0; i < len; i++)
- if (dest[i] - '0')
- {
- j = i;
- break;
- }
- if (i >= len)
- {
- dest[0] = '0';
- dest[1] = 0;
- return m;
- }
- len2 = len - j;
- for (i = 0; i < len2; i++)
- dest[i] = dest[j++];
- dest[i] = 0;
- return m;
- }
- /* 将from进制(二进制、八进制、16进制)的str转为10进制*/
- void convert2dec(char *str, int from, char *dest)
- {
- int i, j, m,len;
- len = strlen(str);
- m = 0;
- /* 二进制转10进制 1010->10
- * 八进制转10进制 031->25 0777->511
- * 16进制转10进制 0xa1->161 0x20->32
- * 输入:str为八进制时,前面的0省略,str为16进制时,0x省略
- */
- HP a, b,c, fm;
- int2hp(m, &a);
- int2hp(from, &fm);
- c = a;
- for (i = 0; i < len; i++)
- {
- multiply(c, fm, &c);/* c=c*fm*/
- if (from == 16)
- {
- if (str[i] >= 'A' && str[i] <= 'F')
- {
- int2hp(str[i] - 'A' + 10, &b);
- add(c, b, &c);
- }
- else if (str[i] >= 'a' && str[i] <= 'f')
- {
- int2hp(str[i] - 'a' + 10, &b);
- add(c, b, &c);
- }
- else
- {
- int2hp(str[i] - '0', &b);
- add(c, b, &c);
- }
- }
- else
- {
- int2hp(str[i] - '0', &b);
- add(c, b, &c);
- }
- }
- hp2str(c, dest);
- }
- /* 从from进制转为to进制 ,str为八进制时,前面的0省略,str为16进制时,0x省略 ,支持二进制,八进制,十进制,16进制*/
- void baseconvert(char *str, int from, char* dest, int to)
- {
- int m, i, j;
- char c;
- if (from == to || *str == '0'){
-
- strcpy(dest, str);
- return ;
- }
- /* 首先转为10进制*/
- if (from != 10)
- {
- convert2dec(str, from, dest);
- if(to==10)return ;
- }
- else
- {
- strcpy(dest,str);
- }
- char *res = malloc(4 * strlen(dest)+1);
- char *tmp = dest;
- i = 0;
- /*再由10进制转为to进制 */
- while (*tmp - '0')
- {
- m = divto(tmp, to, dest);
- res[i] = m + '0';
- if (m >= 10 && m <= 15)
- res[i] = m - 10 + 'a';
- i++;
- }
- for (j = 0, m = i - 1; j < i; j++, m--)
- {
- dest[j] = res[m];
- }
- dest[j] = 0;
- free(res);
- }
- int main()
- {
- char str[100];
- char dest[410];
- int from,to;
- while(scanf("%s%d%d",str,&from,&to)!=EOF)/*from进制的str转为to进制*/
- {
- /*memset(dest,0,sizeof(dest));*/
- baseconvert(str,from,dest,to);
- printf("res=%s\n",dest);
- }
- }
复制代码 相关帖子:
http://bbs.chinaunix.net/thread-3629443-1-2.html
http://bbs.chinaunix.net/thread-1342536-1-1.html
http://bbs.chinaunix.net/thread-1315250-1-1.html
http://bbs.chinaunix.net/thread-1300500-1-1.html
http://bbs.chinaunix.net/thread-1239461-1-1.html
http://bbs.chinaunix.net/thread-1229661-1-1.html
http://bbs.chinaunix.net/thread-1468845-1-1.html
http://bbs.chinaunix.net/thread-1738674-1-1.html
http://bbs.chinaunix.net/thread-1823027-1-1.html
http://bbs.chinaunix.net/thread-3622832-1-1.html |
|