您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页itoa函数的实现

itoa函数的实现

来源:叨叨游戏网
#include <iostream>
using namespace std;

//itoa函数的实现
void MyItoa(int x, char* str)
{
	int i, j;
	i = j = 0;
	char tmp[50];
	bool negative = false;
	if(x < 0)
	{
		x = -x;
		negative = true;
	}
		
	while(x > 0)
	{
		tmp[i++] = x%10 + '0';
		x /= 10;
	}
	
	if(negative)
		tmp[i++] = '-';
	tmp[i] = '\0';
	
	--i;
	while(i >= 0)
	{
		str[j++] = tmp[i--];
	}
	str[j] = '\0';
}
	
	

int main() {
	// your code goes here
	char str[50];
	MyItoa(-12345, str);
	cout << str << endl;
	
	return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务