///GH: exercise 7 of PPP
/*************
7. Modify the “mini calculator” from exercise 5 to accept (just) single-digit numbers written as
either digits or spelled out.
****************/

#include "..\std_lib_facilities.h"

/********************************
PROTOTYPES
******************************/
/// set up a vector of integer strings: {"zero", "one", ... "nine"}
void initialize_integer_strings(vector<string>& intStrings);
int convertString(vector<string> vs, string s);


int main() {
    vector<string> intStrings;
    initialize_integer_strings(intStrings);
    cout << "\nHello! My numbers are ";
    for(int i = 0 ; i < 10; ++i) cout << intStrings[i] << endl;
    int a, b;
    string A, B;
    char op;
    cout << "\nEnter two single digit integers, either as numbers or spelled out as strings"
         << "\nand an operation, separated by spaces: ";
    while(cin >> a >> b >> op) {
        switch(op) {
        case '+' :
            cout << a << " + " << b << " = " << a+b << endl;
            break;
        case '-' :
            cout << a << " - " << b << " = " << a-b << endl;
            break;
        case '*' :
            cout << a << " * " << b << " = " << a*b << endl;
            break;
        case '/' :
            cout << a << " / " << b << " = " << a/b << endl;
            break;
        default :
            continue;
        }
        cout << "\nEnter two doubles and an operation, separated"
             << "\nby spaces: ";
    }
	cin.clear();	/// clear string after failed attempt to read an integer
    while(cin >> A >> B >> op) {
        switch(op) {
        case '+' :
            cout << A << " + " << B << " = " << convertString(intStrings,A)+convertString(intStrings,B) << endl;
            break;
        case '-' :
            cout << A << " - " << B << " = " << convertString(intStrings,A)-convertString(intStrings,B) << endl;
            break;
        case '*' :
            cout << A << " * " << B << " = " << convertString(intStrings,A)*convertString(intStrings,B) << endl;
            break;
        case '/' :
            cout << A << " / " << B << " = " << convertString(intStrings,A)/convertString(intStrings,B) << endl;
            break;
        default :
            continue;
        }
        cout << "\nEnter two doubles and an operation, separated"
             << "\nby spaces: ";
    }

}

/// set up a vector of integer strings: {"zero", "one", ... "nine"}
void initialize_integer_strings(vector<string>& intString) {
    intString.push_back("zero");
    intString.push_back("one");
    intString.push_back("two");
    intString.push_back("three");
    intString.push_back("four");
    intString.push_back("five");
    intString.push_back("six");
    intString.push_back("seven");
    intString.push_back("eight");
    intString.push_back("nine");
}

/// take a string like "four" and return the integer 4
int convertString(vector<string> vs, string s) {
    int i = 0;
    while(vs[i] != s && i < vs.size()) ++i;
    if(i<10) return i;
    else return -1;
}
