Skip to content

Instantly share code, notes, and snippets.

@HalanoSiblee
Created December 31, 2024 16:56
Show Gist options
  • Save HalanoSiblee/cd2c3b79341c07e031ae1bdafcea8c86 to your computer and use it in GitHub Desktop.
Save HalanoSiblee/cd2c3b79341c07e031ae1bdafcea8c86 to your computer and use it in GitHub Desktop.
FLTK calculator using input method to evaluate the expression
/*
πŸ‘‘ BY HALANO SIBLEE πŸ‘‘
CAREFUL THIS CODE NOT SAFE
*/
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Box.H>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
Fl_Box* box; // global pointer ^-^ for the box
Fl_Input* input; // same for input too so we can use and trace them early
// Function to split the string into numbers and operations
std::vector<std::string> tokenize(const std::string& expr) {
std::vector<std::string> tokens;
std::string current;
for (char c : expr) {
if (std::isdigit(c) || c == '.') { // Part of a number
current += c;
} else {
if (!current.empty()) {
tokens.push_back(current);
current.clear();
}
if (c != ' ') { // Ignore spaces, but keep other characters as tokens
tokens.push_back(std::string(1, c));
}
}
}
if (!current.empty()) tokens.push_back(current);
return tokens;
}
// Function to evaluate the expression
const char* evaluate_expression(const char* expr) {
if (expr == nullptr || expr[0] == '\0') {
return "Error: Empty input!";
}
std::vector<std::string> tokens = tokenize(expr);
if (tokens.empty()) {
return "Error: Empty or invalid expression!";
}
std::vector<double> numbers;
std::vector<char> operations;
for (const auto& token : tokens) {
if (token.length() == 1 && (token[0] == '+' || token[0] == '-' || token[0] == '*' || token[0] == '/')) {
operations.push_back(token[0]);
} else {
try {
numbers.push_back(std::stod(token));
} catch (const std::invalid_argument&) {
return "Error: Invalid number format!";
}
}
}
double result = numbers[0];
for (size_t i = 0; i < operations.size(); ++i) {
switch(operations[i]) {
case '+': result += numbers[i + 1]; break;
case '-': result -= numbers[i + 1]; break;
case '*': result *= numbers[i + 1]; break;
case '/':
if (numbers[i + 1] == 0) return "Error: Division by zero!";
result /= numbers[i + 1];
break;
default: return "Error: Invalid operation!";
}
}
// Convert result to string and return as const char*
static char buffer[64]; // Static buffer for simplicity
snprintf(buffer, sizeof(buffer), "%.6f", result); // Format with 6 decimal places
return buffer;
}
//transform input into evaluate function
void MathTrans(Fl_Widget* w, void* data) {
Fl_Input* input = static_cast<Fl_Input*>(data);
const char* result = evaluate_expression(input->value());
box->label(result);
}
// ⚑ πŸ•· ⚑
int main() {
Fl_Window* win = new Fl_Window(360,150, "FastCalc");
input = new Fl_Input(0, 0, win->w(), 30, NULL);
box = new Fl_Box(0, 60, win->w(), 20, "Fast Light Calculator");
win->position(Fl::w() / 2 - win->w() / 2, Fl::h() / 2 - win->h() / 2);
win->border(true);
win->color(FL_BLACK);
input->textcolor(FL_BLACK);
input->labelcolor(FL_GREEN);
box->labelcolor(FL_WHITE);
box->labelsize(30);
input->box(FL_FLAT_BOX);
input->textfont(FL_HELVETICA_BOLD);
input->selection_color(FL_GRAY);
win->resizable(win);
input->callback(MathTrans,input);
input->when(FL_WHEN_ENTER_KEY);
win->end();
win->show();
return Fl::run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment