Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101108
  • 博文数量: 34
  • 博客积分: 30
  • 博客等级: 民兵
  • 技术积分: 217
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-10 23:36
文章分类
文章存档

2013年(34)

我的朋友

分类: C/C++

2013-04-22 22:24:33

1.//整数转换成字符串itoa函数的实现

#include "stdafx.h"

#include < iostream>

using namespace std;

void itoaTest(int  num,char str[] )

{

       int sign = num,i = 0,j = 0;

       char temp[11];

       if(sign<0)//判断是否 是一个负数

       {

              num = -num;

       };

       do

       {

              temp[i] = num%10+'0';        

              num/=10;

              i++;

       }while(num>0);

       if(sign<0)

       {

              temp[i++] = '-';

       }

       temp[i] = '/0';

       i--;

       while(i>=0)

       {

              str[j] = temp[i];

              j++;

              i--;

       }

       str[j] = '/0';

}

2. //字符串转换成整数atoi函数的实现

int atoiTest(char s[])

{

       int i = 0,sum = 0,sign;    //输入的数前面可能还有空格或制表符应加判断

       while(' '==s[i]||'/t'==s[i])

       {

              i++;

       }

       sign = ('-'==s[i])?-1:1;

       if('-'==s[i]||'+'==s[i])

       {

              i++;

       }

       while(s[i]!='/0')  //isdigit

       {

              sum = s[i]-'0'+sum*10;

              i++;

       }    

       return sign*sum;

}



3.//字符串拷贝函数

#include "stdafx.h"

#include

#include

#include

using namespace std;

char *srcpy(char  *dest,const char *source)

{

       assert((dest!=NULL)&&(source!=NULL));

       char *address = dest;

       while(*source!='/0')

       {

              *dest++=*source++;

       }

       *dest = '/0';

       return address;

}

阅读(974) | 评论(0) | 转发(0) |
0

上一篇:随记

下一篇:大数相乘

给主人留下些什么吧!~~