Advertisement
Marf_72

Untitled

Dec 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework assignment 2
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * Winter semester 2019/2020
  7. *
  8. * @author Martin Ravanov
  9. * @idnumber 62379
  10. * @task 1
  11. * @compiler VC
  12. *
  13. */
  14.  
  15. #include <iostream>
  16.  
  17. using namespace std;
  18.  
  19.  
  20. char* parseInput(){ // validates input - only capital letters, length 1 to 3
  21. char* input = new char[3]{0};
  22. bool isValid = false;
  23.  
  24. cout << "Enter column name with length between 1 and 3: ";
  25.  
  26. do{
  27. //read input
  28. cin.getline(input, 100);
  29.  
  30. isValid = true;
  31. int length = strlen(input);
  32.  
  33. for(int i = 0; i < length; i++){
  34. if((input[i] < 'A') || input[i] > 'Z'){ //is invalid symbol
  35. isValid = false;
  36. }
  37. }
  38.  
  39. //is empty or is too long
  40. if(length == 0 || length > 3){
  41. isValid = false;
  42. }
  43.  
  44. if(!isValid){
  45. cout << "Invalid input. Please enter a valid column name: ";
  46. }
  47. } while(!isValid);
  48.  
  49. return input;
  50. }
  51.  
  52.  
  53. int getNumberOfColumn(char* str){
  54. int result = 0;
  55. int cnt = 0;
  56.  
  57. int length = strlen(str);
  58.  
  59. for(int i = length - 1; i >= 0; i--){ //start from the last element
  60. // we take the ascii code of the current char
  61. // ! we think of it as a base 26 numerical system
  62. result += int(str[i] - 'A' + 1) * pow(26, cnt++);
  63. }
  64.  
  65. return result;
  66. }
  67.  
  68.  
  69. int main(){
  70. char* str = new char[3]{0};
  71.  
  72. //read input
  73. str = parseInput();
  74.  
  75. int numberOfColumn = getNumberOfColumn(str);
  76.  
  77. //print result
  78. cout << "The column's number is: " << numberOfColumn << endl;
  79.  
  80. system("pause");
  81. return 1;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement