实现atoi和itoa

  1 /********************************
  2  * 实现atoi和itoa
  3  ********************************/
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <stdlib.h>
  7 //将字符串转化为整数
  8 int chartoi(char a)
  9 {
 10     if(a >= ‘0‘ || a <= ‘9‘)
 11         return a - ‘0‘;
 12     else
 13         return 0;
 14 }
 15
 16 int length(const char *str)
 17 {
 18     int i = 0;
 19     while(str[i])
 20     {
 21         i++;
 22     }
 23     return i;
 24 }
 25
 26 int myatoi(const char *str)
 27 {
 28     int sum = 0;
 29     int len = length(str);
 30     if(str[0] == ‘-‘)
 31     {
 32         int i = 1;
 33         for(i = 1; i < len; i++)
 34         {
 35             sum = sum * 10 + chartoi(str[i]);
 36         }
 37         return 0 - sum;
 38     }
 39     else
 40     {
 41         int i = 0;
 42         for(i = 0; i < len; i++)
 43         {
 44             sum = sum * 10 + chartoi(str[i]);
 45         }
 46         return sum;
 47     }
 48
 49 }
 50
 51 //将整数转化为字符串
 52 void reverse(char *str)
 53 {
 54     int len = length(str);
 55     int i = 0;
 56     for(i = 0; i < len/2; i++)
 57     {
 58         char temp = str[i];
 59         str[i] = str[len - i -1];
 60         str[len - i -1] = temp;
 61     }
 62 }
 63
 64 const char* neg_num(char *str)
 65 {
 66     int len = length(str);
 67     int i = 0;
 68     for(i = len; i > 0; i--)
 69     {
 70         str[i] = str[i - 1];
 71     }
 72     str[0] = ‘-‘;
 73     return str;
 74 }
 75
 76 const char* myitoa(int num)
 77 {
 78     static char str[100];
 79     memset(str, 0, strlen(str));
 80     int neg = 0; //正负判断符
 81     if(num < 0)
 82     {
 83         neg = 0;
 84         num = 0 - num;
 85     }
 86     else
 87     {
 88         neg = 1;
 89     }
 90
 91     int i = 0;
 92     while(num)
 93     {
 94         str[i] = num % 10 + 0x30;
 95         num = num / 10;
 96         i++;
 97     }
 98     reverse(str);
 99     if(neg == 0)
100         return neg_num(str);
101     else
102         return str;
103 }
104
105 int main(void)
106 {
107     printf("Hello World!\n");108
109     printf("%s\n", myitoa(-1234556));
110     return 0;
111 }
时间: 2024-08-30 11:22:04

实现atoi和itoa的相关文章

c++实现atoi()和itoa()函数(字符串和整数转化)

  c++实现atoi()和itoa()函数(字符串和整数转化) 一:起因 (1)字符串类型转化为整数型(Integer),还是字符串类型(String)转化为Double类型,这在java里面有非常好的内部函数,很easy的事情: (2)但是在c里面没有Integer Double等包装类,由char[]数组转化为整数型就变得不那么简单了,atoi()  itoa()在widows下面有,但是网上说linux 下好像没有 itoa() 函数,用 sprintf() 好了,但是本人测试了一下sp

atoi、itoa,strcpy,strcmp,memcpy等实现

原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http://note.youdao.com/share/?id=1f826e4337c7db272e94fdb4f267a8de&type=note 2.strcpy.strcat等源码 link:http://note.youdao.com/share/?id=d23a598b2e31321517ed5

关于atoi,itoa与itob的重写和字符统计

首先关于函数atoi的重写, atoi的功能是字符串能够转换为整数保存,仅仅针对于整数,浮点数以后会有写: //实现一个函数int my_atoi(char s[]),可以将一个字符串转换为对应的整数. #include <stdio.h> #include <ctype.h> int main() { char st[50]; gets(st);  printf("%d",atoi(st)); return 0; } int atoi(char s[]) {

【练习题】atoi和itoa函数的实现

int atoi (const char * str); //Convert string to integer char * itoa ( int value, char * str, int base ); //Convert integer to string (non-standard function) #include <stdio.h> #include <ctype.h> int my_atoi(const char *s) { int i =0; int tota

atoi 和itoa用法

1.itoa 在linux下没有itoa这个函数       原型:char  *itoa(int   value,char   *string,int   radix)          用法:#include           功能:将整数value转换成字符串存入string, radix为转换时所用基数(保存到字符串中的数据的进制基数 2 8 10 16)             说明:返回指向转换后的字符串的指针              举例: #include    #inclu

剑指offer 把数组排成最小的数 atoi和itoa,pow

pow(x,y)在#include<math.h>文件中,计算x的y次方. C++引入头文件:#include <stdlib.h> 或者 #include <cstdlib> 1.整数转化为字符串的方法: 1.1 atoi原型:注意:参数若为string类型一定转换成char*型(str.c_str()) #include <stilib.h>或者#include<cstdlib> int atoi(const char *str); atoi

atoi(),itoa() ,atol(),atof ()的实现

atoi(将字符串转换成整型数):atoi(const char *nptrnt ) 函数说明 : atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回. itoa(将整型数转换成字符串):char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. radix:转换后的进制数,可以是

atoi和itoa

itoa()函数的原型为: char *itoa( int value, char *string,int radix);itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转换数字时所用的基数.在例中,转换基数为10.10:十进制:2:二进制...itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf. 是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似

常用的函数:atoi,itoa,atof,_ttoi等

出自http://blog.csdn.net/zzyoucan/article/details/10260093 atoi---ASCII to integer,将字符串转换成整形,从数字或正负号开始转换,一直到非数字为止 #include <stdlib.h> #include <stdio.h> int main(void) { float n; char *str = "12345.67"; n = atoi(str); printf("stri