#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;
}
itoa函数的实现
最新推荐文章于 2023-02-02 19:13:53 发布