Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Solution to homework assignment 2
- * Introduction to programming course
- * Faculty of Mathematics and Informatics of Sofia University
- * Winter semester 2019/2020
- *
- * @author Martin Ravanov
- * @idnumber 62379
- * @task 1
- * @compiler VC
- *
- */
- #include <iostream>
- using namespace std;
- char* parseInput(){ // validates input - only capital letters, length 1 to 3
- char* input = new char[3]{0};
- bool isValid = false;
- cout << "Enter column name with length between 1 and 3: ";
- do{
- //read input
- cin.getline(input, 100);
- isValid = true;
- int length = strlen(input);
- for(int i = 0; i < length; i++){
- if((input[i] < 'A') || input[i] > 'Z'){ //is invalid symbol
- isValid = false;
- }
- }
- //is empty or is too long
- if(length == 0 || length > 3){
- isValid = false;
- }
- if(!isValid){
- cout << "Invalid input. Please enter a valid column name: ";
- }
- } while(!isValid);
- return input;
- }
- int getNumberOfColumn(char* str){
- int result = 0;
- int cnt = 0;
- int length = strlen(str);
- for(int i = length - 1; i >= 0; i--){ //start from the last element
- // we take the ascii code of the current char
- // ! we think of it as a base 26 numerical system
- result += int(str[i] - 'A' + 1) * pow(26, cnt++);
- }
- return result;
- }
- int main(){
- char* str = new char[3]{0};
- //read input
- str = parseInput();
- int numberOfColumn = getNumberOfColumn(str);
- //print result
- cout << "The column's number is: " << numberOfColumn << endl;
- system("pause");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement