#include "std_lib_facilities.h"
#include "calculator.h"

// -----------------------------------------------------------------------
template <typename T>
string tostr(const T& t) {
   ostringstream os;
   os<<t;
   return os.str();
}

int main()
try
{
    // predefine names:
    //st.declare("pi",3.1415926535,true);
    //st.declare("e",2.7182818284,true);
    //st.declare("k",1000,true);

    string fstr;
    float x{0}, fatx{0}, xstart{0}, xstop{0}, deltaX{0}, meshSize{10};
    std::cout << "\nEnter the function string you want to evaluate in terms of x: ";
    getline(cin,fstr);
    fstr += ";";
    std::cout << "\nEnter a starting value for x: ";
    cin >> xstart;
    cout << "\nEnter a stopping value for x: ";
    cin >> xstop;
    deltaX=(xstop-xstart)/meshSize;
    std::string xstring = "let x = " + tostr(x) + ";";
    std::cout << endl << "x = " << calc(xstring);
    std::cout << "\nf(" << x << ") = " << calc(fstr);
    for(x = xstart; x <= xstop; x+=deltaX) {
        xstring = "x = " + tostr(x) + ";";
        calc(xstring);
        std::cout << "\nf(" << x << ") = " << calc(fstr);
    }
    return 0;
}
catch (exception& e) {
    cerr << "exception: " << e.what() << endl;
    char c;
    while (cin>>c && c!=';');
    return 1;
}
catch (...) {
   cerr << "exception\n";
    char c;
    while (cin>>c && c!=';');
    return 2;
}
