#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int **numbers = 0;
int count = 0, size = 0;
{
int number;
ostringstream buf;
string digits;
do {
cout << "Enter another number (or < 0 to stop): ";
cin >> number;
if (number >= 0) {
if (count >= size) {
int size2 = (size == 0) ? 2 : size * 2;
int **numbers2 = new int *[size2];
if (count > 0) {
int i = 0;
do {
numbers2[i] = numbers[i];
i++;
} while (i < count);
delete[] numbers;
}
size = size2;
numbers = numbers2;
}
buf.clear();
buf.str("");
buf << number;
digits = buf.str();
int subsize = digits.size() + 1;
int i = count, j = 1, j2 = 0;
numbers[i] = new int[subsize];
numbers[i][0] = digits.size();
do {
numbers[i][j] = digits[j2] - '0';
j++;
j2++;
} while (j < subsize);
count++;
}
} while (number >= 0);
}
{
int i = 0;
do {
int j = 1;
int subsize = numbers[i][0] + 1;
do {
if (numbers[i][j] == 9) {
numbers[i][j] = 0;
}
} while (++j < subsize);
} while (++i < count);
}
{
int i = 0;
bool swapped;
do {
swapped = false;
if (i + 2 <= count) {
int *temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
swapped = true;
}
i += 2;
} while (swapped);
}
{
int i = 0;
do {
int j = 1;
int subsize = numbers[i][0] + 1;
do {
cout << numbers[i][j];
} while (++j < subsize);
cout << endl;
delete[] numbers[i];
} while (++i < count);
delete[] numbers;
}
}
Comments
Leave a comment