当前位置:首页 > 单片机 > 单片机
[导读]#include "stdio.h"#include "math.h"#include"string.h"int zhh(char a[32],int n){int i,j;long c=0;j=strlen(a);//测试字符串的总长度for(i=j-1,j=0;i>=0;i--,j++)c=c+(long)((a[i]-'0')*pow(n,j));//十进

#include "stdio.h"

#include "math.h"

#include"string.h"

int zhh(char a[32],int n)

{int i,j;

long c=0;

j=strlen(a);//测试字符串的总长度

for(i=j-1,j=0;i>=0;i--,j++)

c=c+(long)((a[i]-'0')*pow(n,j));//十进制10=二进制1010,即1*2^3+1*2^1

//pow(n,j)为n^j

return c;

}

int main()

{ char a[32];

int base;

long c;

char *p=a;

printf("input data and jinzhi:n ");

scanf("%s",p);

scanf("%d",&base);

c=zhh(p, base);

printf("the changed: %ld",c);

return 0;

}

代码说明:1、关于a[i]-'0',这是一个相对值。因为a[i]t和‘0’均为字符,字符‘0’ASCII中值为48,其它数字字符依次增加1。所以千万不能去掉字符0两边的‘’。

2、数组中a[0],a[1],a[2]...a[j-1],左边的数参与计算的幂次高,如十进制10等于二进制1010,即1*2^3+1*2^1,所以用“for(i=j-1,j=0;i>=0;i--,j++)...”。

3、在Visual C++ 6.0中,可以插入断点调试。


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭