Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-arash authored Aug 24, 2024
1 parent f76d738 commit 1e957f5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/equation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "equation.h"
#include "logging.h"
#include <cstdlib>

int getRandomNumber(int min, int max) {
logInformation(1, "Generating random number with min: " + std::to_string(min) + " and max: " + std::to_string(max));
return rand() % (max - min + 1) + min;
}

void generateLinearEquation(int& a, int& b, int& c, int& correctAnswer) {
logInformation(1, "Generating linear equation...");
a = getRandomNumber(1, 10);
correctAnswer = getRandomNumber(1, 10);
c = getRandomNumber(0, 10);
b = a * correctAnswer + c;
}

void generateQuadraticEquation(int& a, int& b, int& c, int& d, int& correctAnswer) {
logInformation(1, "Generating quadratic equation...");
a = getRandomNumber(1, 10);
correctAnswer = getRandomNumber(1, 10);
b = getRandomNumber(1, 10);
c = getRandomNumber(1, 10);
d = a * correctAnswer * correctAnswer + b * correctAnswer + c;
}
23 changes: 23 additions & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "logging.h"
#include <iostream>

bool debugMode = false;

void logInformation(int level, const std::string& information) {
if (!debugMode) return;

switch (level) {
case 1:
std::cout << "[INFO] " << information << "\n";
break;
case 2:
std::cout << "[WARN] " << information << "\n";
break;
case 3:
std::cout << "[ERROR] " << information << "\n";
break;
default:
std::cout << "[INTERNAL-ERROR] Incorrect usage of logInformation()!\n";
break;
}
}
68 changes: 68 additions & 0 deletions src/tui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "tui.h"
#include "equation.h"
#include "logging.h"
#include <iostream>

void displayMenu() {
std::cout << "Welcome to Extreme Math!\n";
std::cout << "Please select an option:\n";
std::cout << "1. Solve Linear Equations\n";
std::cout << "2. Solve Quadratic Equations\n";
std::cout << "3. Challenge Mode\n";
std::cout << "4. Exit\n";
}

void solveLinear() {
int a, b, c, correctAnswer, userAnswer;
generateLinearEquation(a, b, c, correctAnswer);
std::cout << "Solve for x: " << a << "x + " << b << " = " << c << "\n";
std::cout << "Your answer: ";
std::cin >> userAnswer;

if (userAnswer == correctAnswer) {
std::cout << "Correct! Well done.\n";
} else {
std::cout << "Wrong answer. The correct answer was: " << correctAnswer << "\n";
}
}

void solveQuadratic() {
int a, b, c, d, correctAnswer, userAnswer;
generateQuadraticEquation(a, b, c, d, correctAnswer);
std::cout << "Solve for x: " << a << "x^2 + " << b << "x + " << c << " = " << d << "\n";
std::cout << "Your answer: ";
std::cin >> userAnswer;

if (userAnswer == correctAnswer) {
std::cout << "Correct! Well done.\n";
} else {
std::cout << "Wrong answer. The correct answer was: " << correctAnswer << "\n";
}
}

void playGame() {
char choice;
do {
displayMenu();
std::cin >> choice;

switch (choice) {
case '1':
solveLinear();
break;
case '2':
solveQuadratic();
break;
case '3':
solveLinear();
solveQuadratic();
break;
case '4':
std::cout << "Thanks for playing!\n";
break;
default:
std::cout << "Invalid option. Please try again.\n";
}

} while (choice != '4');
}

0 comments on commit 1e957f5

Please sign in to comment.