您的位置:首页 > 编程语言

atoi与itoa代码实现

2014-12-04 15:27 260 查看
#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{
	int num=-12345,j=0,i=0;
	char temp[7],str[7];
	if(num<0)
	{
		temp[i]='-';
		i++;
	}
	num=abs(num);
	while(num)
	{
		temp[i]=num%10+'0';
		i++;
		num/=10;
	}
	temp[i]=0;
	i--;
	if(temp[0]=='-')
	{
		str[j++]=temp[0];

	while(i>0)
	{
		str[j]=temp[i];
		j++;
		i--;
	}
	}
	else
	{
while(i>=0)
	{
		str[j]=temp[i];
		j++;
		i--;
	}
	}

	str[j]=0;
	cout<<str<<endl;

	int numb=-34567;
	char strs[7];
	itoa(numb,strs,10);
	cout<<strs<<endl;

	/*---------------atoi--------------*/
	char *ch="123456";//不包含负数情况
	int chi=0,tempnum=0;
	while(chi<strlen(ch))
	{
		tempnum=tempnum*10+ ch[chi++]-'0';
	}
	cout<<"tempnum="<<tempnum<<endl;
	int sumtemp=0;
	cout<<"atoi="<<atoi(ch)<<endl;
	system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: