Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- int calcDistance(string code);
- int main(){
- string code;
- const string codeFilename = "codes.txt";
- ifstream infile;
- infile.open(codeFilename);
- if (!infile)
- {
- cout << "The input file does not exist." << endl;
- }
- else
- {
- cout << "Code\t\t\t\t\tDistance" << endl;
- getline(infile, code, '\n');
- while (infile)
- {
- cout << code << "\t\t" << calcDistance(code) << endl;
- getline(infile, code, '\n');
- }
- infile.close();
- }
- system("pause");
- return 0;
- }
- int calcDistance(string code){
- int distance = 0;
- int yPosition = 1;
- int xPosition = 0;
- int changeX;
- int changeY;
- vector<vector <char>> alphabet{
- { '1', '2', '3', '4', '5', '6', '7', '8', '9' },
- { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' },
- { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' },
- { 'z', 'x', 'c', 'v', 'b', 'n', 'm' }
- };
- for (int i = 0; i < code.length(); i++){
- for (vector<vector<char>>::size_type j = 0; j < alphabet.size(); j++)
- {
- for (vector<char>::size_type k = 0; k < alphabet[j].size(); k++)
- {
- if (alphabet[j][k] == code[i]){
- changeX = j - xPosition;
- changeY = k - yPosition;
- if (j != xPosition && k != yPosition){
- if (abs(changeX)>abs(changeY)){
- //cout << changeX << endl;
- distance += abs(changeX);
- yPosition = k;
- xPosition = j;
- }
- else{
- //cout << changeY << endl;
- distance += abs(changeY);
- yPosition = k;
- xPosition = j;
- }
- }
- else if (j != xPosition){
- //cout << changeX << endl;
- distance += abs(changeX);
- yPosition = k;
- xPosition = j;
- }
- else if (k != yPosition){
- //cout << changeY << endl;
- distance += abs(changeY);
- yPosition = k;
- xPosition = j;
- }
- }
- }
- }
- }
- return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement