48 lines
813 B
C++
48 lines
813 B
C++
#include "itos.h"
|
|
#include <stack>
|
|
#include <cstdlib>
|
|
|
|
using std::string;
|
|
using std::stack;
|
|
using std::abs;
|
|
|
|
string itos(int val)
|
|
{
|
|
string answer;
|
|
stack<int> stk;
|
|
int temp;
|
|
|
|
if (val < 0)
|
|
{
|
|
answer += "-";
|
|
val = abs(val);
|
|
}
|
|
|
|
while (val > 0)
|
|
{
|
|
temp = val % 10;
|
|
stk.push(temp);
|
|
val = val / 10;
|
|
}
|
|
|
|
if (stk.empty()) return "0";
|
|
|
|
while (!stk.empty())
|
|
{
|
|
temp = stk.top();
|
|
if (temp == 0) answer += '0';
|
|
else if (temp == 1) answer += '1';
|
|
else if (temp == 2) answer += '2';
|
|
else if (temp == 3) answer += '3';
|
|
else if (temp == 4) answer += '4';
|
|
else if (temp == 5) answer += '5';
|
|
else if (temp == 6) answer += '6';
|
|
else if (temp == 7) answer += '7';
|
|
else if (temp == 8) answer += '8';
|
|
else if (temp == 9) answer += '9';
|
|
stk.pop();
|
|
}
|
|
|
|
return answer;
|
|
}
|