Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int MAX_STRING_SIZE = 4;
- int symbColToNumCol(char* colSym, int length) {
- int element = 0;
- int sum = 0;
- if (length == 1) {
- element = colSym[0];
- element = (element - 'A') + 1;
- sum += element;
- }
- else if (length == 2) {
- for (int i = 0; i < length; i++) {
- element = colSym[i];
- element = (element - 'A') + 1;
- if (i == 0) {
- sum = element * 26;
- }
- else if (i == 1) {
- sum += element;
- }
- }
- }
- else {
- for (int i = 0; i < length; i++) {
- element = colSym[i];
- element = (element - 'A') + 1;
- if (i == 0) {
- sum = (pow(26, 2) * element);
- }
- else if (i == 1) {
- sum += (element * 26);
- }
- else {
- sum += element;
- }
- }
- }
- return sum;
- }
- bool isInputValid(char* str)
- {
- int strLength = strlen(str);
- if (strLength >= MAX_STRING_SIZE)
- {
- return false;
- }
- for (int i = 0; i < strLength; i++)
- {
- if (str[i] < 'A' || str[i] > 'Z')
- {
- return false;
- }
- }
- return true;
- }
- int main()
- {
- char colSym[MAX_STRING_SIZE];
- char input[100];
- int length = 0;
- cout << "Please, enter column letter with length 1-3 symbols: ";
- std::cin >> input;
- while (isInputValid(input) == false)
- {
- std::cout << "Invalid input! Please, enter column letter with length 1-3 symbols: " << std::endl;
- std::cin >> input;
- }
- for (int i = 0; i < strlen(input) + 1; i++)
- {
- colSym[i] = input[i];
- }
- length = strlen(colSym);
- cout << symbColToNumCol(colSym, length);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement