반응형
int main()
{
// calc add
std::string a = "1000";
std::string b = "99000";
std::string c = "100000";
std::string result;
TRACE("a [%s] + b [%s]", a.c_str(), b.c_str());
int a_len = a.length();
int b_len = b.length();
int len = a_len > b_len ? a_len : b_len;
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
char sum = 0;
char ai = 0;
char bi = 0;
char up = 0;
char rem = 0;
for (size_t i = 0; i < len; i++)
{
ai = 0;
bi = 0;
sum = 0;
if (i < a_len)
{
auto aa = a.at(i);
//ai = atoi(&a.at(i));
ai = atoi(&aa);
}
if (i < b_len)
{
auto bb = b.at(i);
bi = atoi(&bb);
//bi = atoi(&b.at(i));
}
sum = ai + bi + up;
rem = sum % 10;
result.push_back(rem + '0');
if (sum > 9)
{
up = 1;
}
else
{
up = 0;
}
}
if (up == 1)
{
result.push_back(up + '0');
}
std::reverse(result.begin(), result.end());
TRACE("result : [%s]", result.c_str());
// calc end
}
반응형
'Program Tip' 카테고리의 다른 글
| Unity 2021.3.15f 버전에서 UnityAds 4.4.1 버전을 추가할 시 발생하는 오류 수정 방법(기록용) (0) | 2023.01.06 |
|---|---|
| C# 클리커 게임의 화폐 단위 표현 방법 (Double 형) (0) | 2022.02.08 |
| Unity에서 유용하게 사용될 오브젝트 풀링(Object Pooling) (0) | 2022.01.11 |
| [Unity3D] Vector2 에서 Vector2 사이의 사이각 찾기 (3) | 2015.12.18 |
| [Cocos2d-x] Angle만 알고 있을 경우 Vector 구하기 (1) | 2015.05.15 |