首先将字符的数据使用c库函数atoi()转换为int 类型计算,在将值以字符的形式存储在数组中
适合不太大的小数
strrev() 函数反转字符串
1、使用int buff[]存储二进制数
2、使用递归
3、使用字符存储二进制数
1、使用int buff[128] 类型存储二进制数值char* num_to_io(int num){int a = 0;int buff[128] = {0};int two[128] = {0};for(int i = 0;1;i++){buf[i] = num%2;num = num/2;if(num == 1){buf[i+1] = num;a = i;break;}}for(int j =0; j <= a;j++){two[j] = buff[a-j];}for(int k = 0;k <= a;k++){printf("%d",two[k]);}return 0;}2、递归#include <stdio.h>void convert(int n){if(n > 1){convert(n / 2)}printf("%d",n%2);}int main(){int n;scanf("%d",&n);convert(n);return 0;}3、字符存储//更改为字符存储int main(){int num = 255;int a = 0;char buff[128] = {0};char two[128] = {0};for(int i = 0;1;i++){if(num%2 == 1){buff[i] = '1';}else{buff[i] = '0';}num = num/2;if(num == 1){buff[i+1] = '1';a = i+1;break;}}printf("the num : ");for(int j = a; j>=0;j--){two[a-j] = buff[j];}printf("the m is %s\n",two);return 0;}
字符串的二进制数转换为十进制的int类型
字符串中的单个字符的比较将二进制转换为十进制数#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char *num = "1111";int a = strlen(num);printf("the a is %d\n",a);int numa = 0;int numb = 0;for(int i = a-1;i>=0 ;i++){if(*num == '1'){numa = 2;for(int j = i-1;j > 0;j--){numa = numa*2;}if(i == 0){numa = 1;}numb = numa + numb;printf("the i is %d and numb us %d\n",i,numb);num++;}}printf("the numb is %d \n",numb);}
头文件:#include <math.h>
pow() 函数用来求 x 的 y 次幂(次方),其原型为:
double pow(double x, double y);
pow()用来计算以x 为底的 y 次方值,然后将结果返回。设返回值为 ret,则ret = xy。
可能导致错误的情况:
如果底数 x 为负数并且指数 y 不是整数,将会导致 domain error错误。如果底数 x 和指数 y 都是 0,可能会导致 domain error错误,也可能没有;这跟库的实现有关。如果底数 x 是 0,指数 y 是负数,可能会导致domain error 或pole error 错误,也可能没有;这跟库的实现有关。如果返回值 ret 太大或者太小,将会导致range error 错误。
错误代码:
如果发生domain error 错误,那么全局变量 errno 将被设置为 EDOM;如果发生pole error 或range error 错误,那么全局变量 errno 将被设置为 ERANGE。
注意,使用 GCC 编译时请加入-lm。