Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. const int MAX_STRING_SIZE = 4;
  7.  
  8. int symbColToNumCol(char* colSym, int length) {
  9. int element = 0;
  10. int sum = 0;
  11.  
  12. if (length == 1) {
  13. element = colSym[0];
  14. element = (element - 'A') + 1;
  15. sum += element;
  16. }
  17.  
  18. else if (length == 2) {
  19. for (int i = 0; i < length; i++) {
  20. element = colSym[i];
  21. element = (element - 'A') + 1;
  22. if (i == 0) {
  23. sum = element * 26;
  24. }
  25. else if (i == 1) {
  26. sum += element;
  27. }
  28. }
  29. }
  30. else {
  31. for (int i = 0; i < length; i++) {
  32. element = colSym[i];
  33. element = (element - 'A') + 1;
  34. if (i == 0) {
  35. sum = (pow(26, 2) * element);
  36. }
  37. else if (i == 1) {
  38. sum += (element * 26);
  39. }
  40. else {
  41. sum += element;
  42. }
  43. }
  44. }
  45. return sum;
  46. }
  47.  
  48. bool isInputValid(char* str)
  49. {
  50. int strLength = strlen(str);
  51.  
  52. if (strLength >= MAX_STRING_SIZE)
  53. {
  54. return false;
  55. }
  56.  
  57. for (int i = 0; i < strLength; i++)
  58. {
  59. if (str[i] < 'A' || str[i] > 'Z')
  60. {
  61. return false;
  62. }
  63. }
  64.  
  65. return true;
  66. }
  67.  
  68. int main()
  69. {
  70. char colSym[MAX_STRING_SIZE];
  71. char input[100];
  72.  
  73. int length = 0;
  74.  
  75. cout << "Please, enter column letter with length 1-3 symbols: ";
  76. std::cin >> input;
  77.  
  78. int nextChar = std::cin.peek();
  79.  
  80. while (isInputValid(input) == false || (!isalpha(nextChar) && nextChar != '\n'))
  81. {
  82.  
  83. std::cin.clear();
  84. std::cin.ignore(INT_MAX, '\n');
  85. std::cout << "Invalid input!" << std::endl;
  86. std::cin >> input;
  87.  
  88. nextChar = std::cin.peek();
  89. }
  90.  
  91. for (int i = 0; i < strlen(input) + 1; i++)
  92. {
  93. colSym[i] = input[i];
  94. }
  95.  
  96. length = strlen(colSym);
  97.  
  98. cout << symbColToNumCol(colSym, length);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement