1
0
mirror of synced 2025-01-19 01:24:15 +01:00

feat: Added Euclidean Algorithms tools

This commit is contained in:
WerWolv 2023-11-07 00:47:10 +01:00
parent 924b4a9436
commit 669e1921a4
2 changed files with 50 additions and 0 deletions

View File

@ -577,6 +577,8 @@
"hex.builtin.tools.demangler.demangled": "Demangled name",
"hex.builtin.tools.demangler.mangled": "Mangled name",
"hex.builtin.tools.error": "Last error: '{0}'",
"hex.builtin.tools.euclidean_algorithm": "Euclidean Algorithm",
"hex.builtin.tools.euclidean_algorithm.description": "The Euclidean algorithm is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder.\n\nBy extension, this also provides an efficient method for computing the least common multiple (LCM), the smallest number divisible by both.",
"hex.builtin.tools.file_tools": "File Tools",
"hex.builtin.tools.file_tools.combiner": "Combiner",
"hex.builtin.tools.file_tools.combiner.add": "Add...",

View File

@ -2067,6 +2067,53 @@ namespace hex::plugin::builtin {
ImGui::EndTabBar();
}
}
void drawEuclidianAlgorithm() {
static u64 a, b;
constexpr static auto gcd = [](u64 a, u64 b) {
while (b != 0) {
u64 temp = b;
b = a % b;
a = temp;
}
return a;
};
static u64 gcdResult = 0;
static u64 lcmResult = 0;
ImGui::TextFormattedWrapped("{}", "hex.builtin.tools.euclidean_algorithm.description"_lang);
ImGui::NewLine();
if (ImGui::BeginBox()) {
bool hasChanged = false;
hasChanged = ImGui::InputScalar("A", ImGuiDataType_U64, &a) || hasChanged;
hasChanged = ImGui::InputScalar("B", ImGuiDataType_U64, &b) || hasChanged;
if (hasChanged) {
gcdResult = gcd(a, b);
if (gcdResult == 0)
lcmResult = 0;
else
lcmResult = (a * b) / gcdResult;
}
ImGui::Separator();
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().DisabledAlpha);
ImGui::InputScalar("gcd(A, B)", ImGuiDataType_U64, &gcdResult, nullptr, nullptr, "%llu", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("lcm(A, B)", ImGuiDataType_U64, &lcmResult, nullptr, nullptr, "%llu", ImGuiInputTextFlags_ReadOnly);
ImGui::PopStyleVar();
ImGui::EndBox();
}
}
}
void registerToolEntries() {
@ -2084,6 +2131,7 @@ namespace hex::plugin::builtin {
ContentRegistry::Tools::add("hex.builtin.tools.ieee754", drawIEEE754Decoder);
ContentRegistry::Tools::add("hex.builtin.tools.invariant_multiplication", drawInvariantMultiplicationDecoder);
ContentRegistry::Tools::add("hex.builtin.tools.tcp_client_server", drawTCPClientServer);
ContentRegistry::Tools::add("hex.builtin.tools.euclidean_algorithm", drawEuclidianAlgorithm);
}
}