Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions submissions/quadraticinfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Quadratic Information Finder
This program allows the user to find useful information about a quadratic.

## How to use
Using one of the binaries, run it in the terminal. You will then be prompted for the equation, and afterwards will be given calculated information about the quadratic.

## Why create this
I did it to make solving quadratics faster, as it gives general information based on the input rather than how a calculator could solve for each aspect invidually.

## Dependencies
libm (Math library)
Headers: cmath, cstdio
Binary file added submissions/quadraticinfo/binary/linux-main
Binary file not shown.
Binary file not shown.
58 changes: 58 additions & 0 deletions submissions/quadraticinfo/src/equation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "equation.hpp"

// Parent methods

int64_t Equation::getA(void) const
{
return this->a;
}

int64_t Equation::getB(void) const
{
return this->b;
}

int64_t Equation::getC(void) const
{
return this->c;
}

double Equation::getVertexX(void) const
{
return (-b) / (2*(double)a);
}

double Equation::getVertexY(void) const
{
return evaluate(getVertexX());
}

double Equation::getDiscriminant(void) const
{
return (b*b) - (4*a*c);
}

double Equation::evaluate(double x) const
{
return a*x*x + b*x + c;
}

int64_t VertexEquation::getBFromGiven(int64_t a, int64_t h, int64_t k)
{
return -2*a*h;
}

int64_t VertexEquation::getCFromGiven(int64_t a, int64_t h, int64_t k)
{
return a*h*h + k;
}

int64_t InterceptEquation::getBFromGiven(int64_t a, int64_t p, int64_t q)
{
return -a * (p + q);
}

int64_t InterceptEquation::getCFromGiven(int64_t a, int64_t p, int64_t q)
{
return a*p*q;
}
52 changes: 52 additions & 0 deletions submissions/quadraticinfo/src/equation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <cstdint>

enum EquationType
{
EQUATION_STANDARD,
EQUATION_VERTEX,
EQUATION_INTERCEPT,
EQUATION_INVALID
};

class Equation
{
private:
int64_t a, b, c;
public:
Equation(int64_t a, int64_t b, int64_t c) : a(a), b(b), c(c) {}

int64_t getA(void) const;
int64_t getB(void) const;
int64_t getC(void) const;
double getVertexX(void) const;
double getVertexY(void) const;
double getDiscriminant(void) const;

double evaluate(double x) const;
};

class StandardEquation : public Equation
{
public:
StandardEquation(int64_t a, int64_t b, int64_t c) : Equation(a, b, c) {}
};

class VertexEquation : public Equation
{
private:
int64_t getBFromGiven(int64_t a, int64_t h, int64_t k);
int64_t getCFromGiven(int64_t a, int64_t h, int64_t k);
public:
VertexEquation(int64_t a, int64_t h, int64_t k) : Equation(a, getBFromGiven(a, h, k), getCFromGiven(a, h, k)) {}
};

class InterceptEquation : public Equation
{
private:
int64_t getBFromGiven(int64_t a, int64_t p, int64_t q);
int64_t getCFromGiven(int64_t a, int64_t p, int64_t q);
public:
InterceptEquation(int64_t a, int64_t p, int64_t q) : Equation(a, getBFromGiven(a, p, q), getCFromGiven(a, p, q)) {}
};
197 changes: 197 additions & 0 deletions submissions/quadraticinfo/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#include "main.hpp"

inline void setCursorPos(uint32_t x, uint32_t y)
{
printf(ESC "[%d;%dH", y, x);
}

inline int calculateRealY(uint32_t vertexY, bool openDir)
{
return round(vertexY) - (GRAPH_HEIGHT / 2) + (openDir ? OPEN_OFFSET : -OPEN_OFFSET);
}

bool printGraphChar(double realY, double evalY)
{
bool errorDirection = evalY > realY;
double error = abs(evalY - realY);

if (error < 1) printf("▓");
else if (error < 2) printf("▒");
else if (error < 3) printf("░");

return error < 3;
}

int main(void)
{
std::shared_ptr<Equation> equation;

printf(ESC "[?1049h"); // Set alternate buffer
setCursorPos(1, 1);

#if defined(_WIN32) || defined(_WIN64)
SetConsoleOutputCP(CP_UTF8);
#endif

EquationType type = EQUATION_INVALID;
printf(LABEL("Enter Equation Type") "(0:Standard, 1:Vertex, 2:Intercept): ");

while (type == EQUATION_INVALID)
{
int inputType;
scanf("%d", &inputType);
switch (inputType)
{
case 0:
type = EQUATION_STANDARD;
break;
case 1:
type = EQUATION_VERTEX;
break;
case 2:
type = EQUATION_INTERCEPT;
break;
default:
setCursorPos(1, 1);
printf(ESC "[2J" LABEL("Invalid type, try again") "(0:Standard, 1:Vertex, 2:Intercept): ");
break;
}
}

{
int64_t a, b, c, h, k, p, q;
switch (type)
{
case EQUATION_STANDARD:
printf(LABEL("Equation") "y = ax^2 + bx + c\n");

printf("Enter a: ");
scanf("%" PRId64, &a);
printf("Enter b: ");
scanf("%" PRId64, &b);
printf("Enter c: ");
scanf("%" PRId64, &c);

equation = std::make_shared<StandardEquation>(a, b, c);
break;
case EQUATION_VERTEX:
printf(LABEL("Equation") "y = a(x - h)^2 + k\n");

printf("Enter a: ");
scanf("%" PRId64, &a);
printf("Enter h: ");
scanf("%" PRId64, &h);
printf("Enter k: ");
scanf("%" PRId64, &k);

equation = std::make_shared<VertexEquation>(a, h, k);
break;
case EQUATION_INTERCEPT:
printf(LABEL("Equation") "y = a(x - p)(x - q)\n");

printf("Enter a: ");
scanf("%" PRId64, &a);
printf("Enter p: ");
scanf("%" PRId64, &p);
printf("Enter q: ");
scanf("%" PRId64, &q);

equation = std::make_shared<InterceptEquation>(a, p, q);
break;
default:
printf("Invalid type.");
exit(type);
break;
}
}

// Print information

printf("\n");

printf(LABEL("Equation (Standard Form)") "y = " "%" PRId64 "x^2 + " "%" PRId64 "x + " "%" PRId64 "\n", equation->getA(), equation->getB(), equation->getC());

double discriminant = equation->getDiscriminant();
printf(LABEL("Discriminant") "%f ", discriminant);

if (discriminant > 0) printf("(Solutions: 2)\n");
else if (discriminant == 0) printf("(Solutions: 1)\n");
else if (discriminant < 0) printf("(Solutions: 0)\n");

printf("\n" LABEL("Solutions") "\n");
if (discriminant > 0)
{
double numeratorSqrt = sqrt(discriminant);
double denomenator = 2*equation->getA();

printf(LABEL("x1") "%f\n", (-equation->getB() + numeratorSqrt) / denomenator);
printf(LABEL("x2") "%f\n", (-equation->getB() - numeratorSqrt) / denomenator);
} else if (discriminant == 0)
{
double numeratorSqrt = sqrt(discriminant);
double denomenator = 2*equation->getA();

printf(LABEL("x1") ": %f\n", (-equation->getB() + numeratorSqrt) / denomenator);
} else if (discriminant < 0) printf("Solutions are imaginary\n");

printf("\n" LABEL("Graph Information") "\n");

double vertX = equation->getVertexX();
double vertY = equation->getVertexY();
printf(LABEL("Line of Symmetry") "x = %f\n", vertX);
printf(LABEL("Vertex") "(%f, %f)\n", vertX, vertY);

bool opensUp = equation->getA() > 0;
printf(LABEL("Direction") "Opens %s\n", opensUp ? "Up" : "Down");

printf("\nPress enter to exit...");
printf(ESC "[s"); // Save cursor position

// Print graph

setCursorPos(GRAPH_OFFSET_X, GRAPH_OFFSET_Y);

int64_t realX = round(vertX) - (GRAPH_WIDTH / 2);
int64_t realY = calculateRealY(vertY, opensUp);
for (int x = 0; x < GRAPH_WIDTH; x++)
{
for (int y = 0; y < GRAPH_HEIGHT; y++)
{
setCursorPos(GRAPH_OFFSET_X + x, GRAPH_OFFSET_Y + GRAPH_HEIGHT - y);

double evalY = equation->evaluate(realX);

printf(ESC "[31m");
if (printGraphChar(realY, evalY));
else {
printf(ESC "[32m");
if (realX == 0 && realY == 0) printf("┼");
else if (realX == 0) {
printf("│");
if (y == 0) printf(ESC "[2B" ESC "[1D" "0");
}
else if (realY == 0) {
printf("─");
if (x == GRAPH_WIDTH - 1) printf(" 0");
}
}
realY++;
}
setCursorPos(GRAPH_OFFSET_X + x, GRAPH_OFFSET_Y);

realX++;
realY = calculateRealY(vertY, opensUp);
}

printf(ESC "[0m");

// Wait for enter

printf(ESC "[u"); // Return to saved cursor position
getchar();
getchar();

printf(ESC "[?1049l"); // Return main buffer

return 0;
}
19 changes: 19 additions & 0 deletions submissions/quadraticinfo/src/main.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "equation.hpp"
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <memory>

#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#endif

#define ESC "\x1b"
#define GRAPH_OFFSET_X 50
#define GRAPH_OFFSET_Y 2
#define OPEN_OFFSET 2
#define GRAPH_WIDTH 25
#define GRAPH_HEIGHT 15
#define LABEL(text) ESC "[32m" text ESC "[0m: "