// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    string eq;
    cout << "Vi skal nu løse en ligning af formen a * x + b = 0" << endl;
    cout << "Indtast ligning: ";
    getline(cin, eq);
    string::size_type ixmul = eq.find('*');
    string::size_type ixadd = eq.find('+', ixmul);
    string::size_type ixeq = eq.find('=', ixadd);
    if(ixmul == string::npos || ixadd == string::npos || ixeq == string::npos)
    {
        cout << "Ligning ikke på korrekt format" << endl;
    }
    else
    {
        string astr = eq.substr(0, ixmul);
        string xstr = eq.substr(ixmul + 1, ixadd - ixmul - 1);
        string bstr = eq.substr(ixadd + 1, ixeq - ixadd - 1);
        string zstr = eq.substr(ixeq + 1);
        char *pa, *pb, *pz;
        double a = strtod(astr.c_str(), &pa);
        double b = strtod(bstr.c_str(), &pb);
        double z = strtod(zstr.c_str(), &pz);
        if(pa > astr.c_str() && pb > bstr.c_str() && pz > zstr.c_str() && a != 0 && z == 0)
        {
            cout << "Løsningen er x = " << (-b/a) << endl;
        }
        else
        {
            cout << "Ligning ikke på korrekt format" << endl;
        }
    }
    return EXIT_SUCCESS;
}
