字符串转数字,数字转字符串

来源:互联网 发布:淘宝俄罗斯博士代购 编辑:程序博客网 时间:2024/04/30 00:57

1.字符串转数字

例如将字符串”-1024“转为数字-1024,”1024“转化为1024,”+1024“转换为1024

<span style="font-size:10px;">#include<stdio.h>#include<stdlib.h>#define MAX 10int main(void){int n=0;int flag=1;//标识符,以判断字符串的开头是'+'还是'-'还是直接以数字开头;0代表以'-'开头,1代表以'+'开头或直接以数字开头char *p=(char *)malloc(sizeof(char)*MAX);char *str;if(NULL==p){printf("insufficient memory!\n");exit(0);}str=p;printf("enter the string:\n");gets(str);while(*str!='\0'){  if(*str=='-')  flag=0;  else if(*str=='+')  flag=1;  else  {      n=n*10 + *str-'0';  }  str++;}if(0==flag)   printf("now the interger that convert from the string is %d\n",-n);else   printf("now the interger that convert from the string is %d\n",n);    free(p);p=NULL;return 0;}</span>
在写这个小程序的过程中还是出现了一个之前忽视的一个小问题,即利用free释放为指针分配的内存空间的问题,,例如下面的代码(这个代码是有bug的)
<span style="font-size:10px;">#include<stdio.h>#include<stdlib.h>#define MAX 10int main(void){int n=0;int flag=1;char *str=(char *)malloc(sizeof(char)*MAX);if(NULL==str){printf("insufficient memory!\n");exit(0);}printf("enter the string:\n");gets(str);while(*str!='\0'){  if(*str=='-')  flag=0;  else if(*str=='+')  flag=1;  else  {      n=n*10 + *str-'0';  }  str++;}if(0==flag)   printf("now the interger that convert from the string is %d\n",-n);else   printf("now the interger that convert from the string is %d\n",n);    free(str);//这里会出现bugstr=NULL;return 0;}</span>
这里free(str)语句会出现错误,利用malloc 动态分配内存的指针str的指向到程序结束时已经发生了改变,此时再用free释放原来申请的内存空间会发生崩溃,最好的解决办法时,当用到该指针时,最好再定义一个与该指针同类型的指针作为临时指针来完成程序中的操作。

2.数字转换为字符串,例如-1024转化为“-1024”,1024转化为"1024"

  2.1递归算法(2014.8.3,之前的程序没有判断整数0转化为字符串的情形)

#include<stdio.h>#include<stdlib.h>   #include<assert.h>      #define MAX 20        int i=0;    void Int2Str(const int a,char *str)    {      if(a==0)          return ;      if(a>0)      {          Int2Str(a/10,str);//递归            str[i]=a%10+'0'-0;            i++;        }  }    int main(void)    {        long int a;        char *str=(char *)malloc(sizeof(char )*MAX);       assert(str);  //if the malloc failed(str==NULL) ,then abort the program       printf("please enter the interger a:\n");        scanf("%ld",&a);        if(a<0)  //当为负整数时,要转为的字符串的开头需要加字符'-'      {            str[i]='-';            i++;             Int2Str(-a,str);      }else if(a==0){str[i]='0';i++;}else{    Int2Str(a,str);  //a>0时}    str[i]='\0';//将字符数组的最后添加'\0',构造字符串        printf("the string that convert from interger is:%s\n",str);        return 0;  }

2.2非递归算法(2014.8.30改,之前的那个版本发现很多细节错误,而且对于整数0的字符串转化并没有处理)

#include<stdio.h>    #include<malloc.h>   #include<assert.h>    #define MAX 20    void Int2Str(const int a,char *str)  {      //char *temp=(char *)malloc(sizeof(char )*MAX);       char *temp=(char *)malloc(sizeof(char)*MAX);      if(temp==NULL)          printf("malloc failed!\n");      //assert(temp);      int i=0,j=0;      int b=a;      if(b<0)          b=-b;      while(b>0)      {          temp[i]=b%10+'0'-0;          b=b/10;          i++;      }      int n=i;//n为temp中有效字符的个数,亦为正整数的位数      if(a<0)//当n为负整数时,需要在字符串前面加'-'      {            str[0]='-';          for(i=1,j=n-1;j>=0;i++,j--)          {              str[i]=temp[j];          }      }      else if(a==0){i=0;str[i]='0';i++;}else    {          for(i=0,j=n-1;j>=0;i++,j--)          {              str[i]=temp[j];          }      } str[i]='\0';  }  int main(void)    {        long int a;        char *str=(char *)malloc(sizeof(char )*MAX);       assert(str);  //if the malloc failed(str==NULL) ,then abort the program       printf("please enter the interger a:\n");        scanf("%ld",&a);      Int2Str(a,str);      printf("the string that convert from interger is:%s\n",str);   free(str);str=NULL;    return 0;  }



                                             
0 0
原创粉丝点击