Answer the question
In order to leave comments, you need to log in
How to convert an array of numbers from char to int?
How to convert an array of char to int numbers in a class that works with "long" numbers?
class Long
{
private:
char arr[100];
public:
Long();
void input();
void show();
Long operator+(const Long& l1);
};
void Long::show()
{
for (int i = 0; i < 5; i++)
{
cout << (arr1[i]);
}
}
Long Long::operator+(const Long& l1)
{
Long l2;
int k = 0;
for (int i = 4; i >= 0; i--)
{
if (i == 4)
{
if ((arr[i] + l1.arr[i]) >= 10)
{
l2.arr[i] = (arr[i] + l1.arr[i]) % 10;
k = 1;
}
else if ((arr[i] + l1.arr[i]) < 10)
{
l2.arr[i] = (arr[i] + l1.arr[i]);
}
}
if (i != 4)
{
if (k == 1)
{
l2.arr[i] = (arr[i] + l1.arr[i] + k) % 10;
if ((arr[i] + l1.arr[i] + k) >= 10)
{
k = 1;
}
else if ((arr[i] + l1.arr[i] + k) < 10)
{
k = 0;
continue;
}
}
if ((arr[i + 1] + l1.arr[i + 1] + k) < 10)
{
if (k == 0)
{
l2.arr[i] = (arr[i] + l1.arr[i]) % 10;
if ((arr[i] + l1.arr[i] + k) >= 10)
{
k = 1;
}
else if ((arr[i] + l1.arr[i] + k) < 10)
{
k = 0;
continue;
}
}
else if (k == 1)
{
l2.arr[i] = (arr[i] + l1.arr[i] + k);
if ((arr[i] + l1.arr[i] + k) >= 10)
{
k = 1;
}
else if ((arr[i] + l1.arr[i] + k) < 10)
{
k = 0;
continue;
}
}
}
}
}
return l2;
}
Answer the question
In order to leave comments, you need to log in
Show seems to work fine for numbers that are 5 digits long. You don't need to cast to int for this.
Long a, b;
a.input();
b.input();
Long sum = a + b;
sum.show();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question