Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created May 13, 2014 23:31
Show Gist options
  • Save dillmo/f9f52ee980855a8d5d5e to your computer and use it in GitHub Desktop.
Save dillmo/f9f52ee980855a8d5d5e to your computer and use it in GitHub Desktop.
Linear Equation Solver
#include <iostream>
#include <regex>
#include <cstdlib>
using namespace std;
double* getInput();
bool undefined(double*);
double a(double*);
double c(double*, double);
int main(int argc, char const* argv[])
{
double* input = getInput();
if ( undefined(input) )
cout << "Undefined\n";
else
cout << "Output:\n"
<< "(" << a(input) << ", " << c(input, a(input)) << ")\n";
return 0;
}
double* getInput() {
string rawInput;
static double input[4];
regex nums("(\\d|\\.)+");
smatch result;
cout << "Input:\n";
cin >> rawInput;
sregex_iterator matches(rawInput.begin(), rawInput.end(), nums);
for ( int i = 0; i < 4; i++ ) {
result = *matches;
input[i] = atof( result.str().c_str() );
matches++;
}
return input;
}
bool undefined(double* input) {
bool ans;
if ( input[0] == input[2] )
ans = true;
else
ans = false;
return ans;
}
double a(double* input) {
return ( input[1] - input[3] ) / ( input[0] - input[2] );
}
double c(double* input, double slope) {
return input[1] - slope * input[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment