Click Here to go back to the homepage.

Imperial Measurement Solution:


#include <bits/stdc++.h>
using namespace std;

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    double original;
    cin >> original;
    string startunit, endunit;
    cin >> startunit >> endunit >> endunit;
    
    pair<string, string> measurements[8] = { {"thou", "th"}, {"inch", "in"}, {"foot", "ft"},
        {"yard", "yd"}, {"chain", "ch"}, {"furlong", "fur"}, {"mile", "mi"}, {"league", "lea"}};
    int startIndex, endIndex;
    for(int i = 0; i < 8; i++){
        if(startunit == measurements[i].first || startunit == measurements[i].second){
            startIndex = i;
        }
        if(endunit == measurements[i].first || endunit == measurements[i].second){
            endIndex = i;
        }
    }
    int conversion[8] = {1, 1000, 12, 3, 22, 10, 8, 3};
    bool forward = endIndex - startIndex > 0;
    int increment = endIndex - startIndex > 0 ? 1 : -1;
    if(!forward){
        endIndex += 1;
    } else {
        startIndex += 1;
    }
    cout << fixed << setprecision(10);
    for(int i = startIndex; i != endIndex + increment; i += increment){
        if(forward){
            original /= conversion[i];
        } else {
            original *= conversion[i];
        }
    }
    cout << original << endl;


    return 0;   
}