i was trying to solve this problem. as it mentions:
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
the exercise is to write c++ program by using standard input/output. and allow the user to pass entries and convert those value to its corresponding values
Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Example:
Input: 2 R23C55 BC23 Output: BC23 R23C55
Here my attempt solution, it works as expected, i have slightly change the input to make it interactive. All inputs are guaranteed to be valid and correct as well as the result.
How can I improve it further.
#include <iostream>
#include <string>
#include <regex>
#include <cctype>
#include <limits>
#include <sstream>
#include <cassert>
#include <type_traits>
// C::B : error: 'stoi' is not a member of 'std'!
int string_to_int(std::string::const_iterator first, std::string::const_iterator last)
{
int value = 0;
if(first == last) return value;
const typename std::string::value_type sign = *first;
if (*first == '-' || *first == '+')
first++;
while (first != last)
{
value = value * 10 - (*first++ - '0');
}
if (sign != '-')
value = -value;
return value;
}
int is_RC_type(const std::string& in)
{
const auto index = in.find_first_of("C");
if (index == std::string::npos) return false;
const auto row = in.substr(1, index - 1);
const auto col = in.substr(index + 1);
static const int Min = 1;
static const int Max = 999999;
const auto colVal = string_to_int(col.cbegin(), col.cend());
const auto rowval = string_to_int(row.cbegin(), row.cend());
return col[0] != '0'
&& row[0] != '0'
&& rowval >= Min
&& rowval <= Max
&& colVal >= Min
&& colVal <= Max
&& std::all_of(row.cbegin(), row.cend(),
[](char c)
{
return std::isdigit(c);
})
&& std::all_of(col.cbegin(), col.cend(),
[](char c)
{
return std::isdigit(c);
});
}
bool is_Cell_type(const std::string& in)
{
const auto index = in.find_first_of("0123456789");
if (index == std::string::npos) return false;
const auto row = in.substr(0, index);
const auto col = in.substr(index);
static const int Min = 1;
static const int Max = 999999;
static const std::string alphaMin{"A"};
static const std::string alphaMax{"BDWGM"};
static const auto AMin = string_to_int(alphaMin.cbegin(), alphaMin.cend());
static const auto AMax = string_to_int(alphaMax.cbegin(), alphaMax.cend());
const auto colVal = string_to_int(col.cbegin(), col.cend());
const auto rowval = string_to_int(row.cbegin(), row.cend());
return col[0] != '0'
&& colVal >= Min
&& colVal <= Max
&& rowval >= AMin
&& rowval <= AMax
&& std::all_of(col.cbegin(), col.cend(),
[](char c)
{
return std::isdigit(c);
})
&& std::all_of(row.cbegin(), row.cend(),
[](char c)
{
return std::isalpha(c);
});
}
template <bool B, typename T, typename F>
std::enable_if_t<std::integral_constant<bool, B>::value, T>
conditional(T&& t, F&&)
{
return std::forward<T>(t);
}
template <bool B, typename T, typename F>
std::enable_if_t<!std::integral_constant<bool, B>::value, F>
conditional(T&&, F&& f)
{
return std::forward<F>(f);
}
template <typename Input, typename Prompt>
Input read_input(Prompt&& prompt)
{
static_assert((std::is_integral<Input>::value && !std::numeric_limits<Input>::is_signed)
|| std::is_same<std::decay_t<Input>, std::string>::value, "type not supported!\n");
Input input{};
bool is_not_valid = false;
static const std::string Error = "Not valid input.\n";
static const std::regex Letters("^[A-Z0-9]+$");
static const std::regex Numbers("^[0-9]+$");
static auto int_input = [] (auto& in, std::istream& is = std::cin)
{
std::string s;
std::getline(is, s);
std::stringstream stream(s);
return is.fail()
|| !std::regex_match(stream.str(), Numbers)
|| !(stream >> in)
|| (1 < in && in > 100000);
};
static auto string_input = [] (auto& in, std::istream& is = std::cin)
{
std::getline(is, in);
return is.fail()
|| !std::regex_match(in, Letters)
|| in.size() == 1
|| std::isdigit(in[0])
|| std::isalpha(in[in.size() - 1])
|| !(is_Cell_type(in) != is_RC_type(in));
};
const auto handle_input = conditional<std::is_integral<Input>::value>(int_input, string_input);
do
{
std::cout << std::forward<Prompt>(prompt) << std::flush;
is_not_valid = handle_input(input);
if (is_not_valid)
{
std::cout << Error;
}
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
} while (is_not_valid);
return input;
}
int main()
{
auto n = read_input<unsigned int>("Enter a number of entries: ");
std::cout << "\n\n\tWelcome\n\n";
do
{
const auto word = read_input<std::string>("Enter your choice: ");
if (is_RC_type(word))
{
const auto index = word.find_first_of("C");
assert(index != std::string::npos);
const auto row = word.substr(1, index - 1);
const auto col = word.substr(index + 1);
auto c = string_to_int(col.cbegin(), col.cend());
std::string result;
do
result = static_cast<char>('A' + c % 26 - 1) + result;
while (c /= 26);
std::cout << "\n\nResult: " << result << row << "\n\n";
}
else
{
const auto index = word.find_first_of("0123456789");
assert(index != std::string::npos);
const auto row = word.substr(index);
const auto col = word.substr(0, index);
int c = 0;
for (const auto& ch : col)
{
c = 26 * c + (ch - 'A' + 1);
}
std::cout << "\n\nResult: R" << row << "C" << c << "\n\n";
}
} while (--n);
}