From 1dbd9968c9ca1700c5a1c73f1cb8a0b838b65255 Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Mon, 30 Jun 2025 15:46:31 +0800 Subject: [PATCH] Add files via upload --- others/C/C++/Makefile | 20 +++++ others/C/C++/include/password_generator.hpp | 27 ++++++ others/C/C++/include/password_gui.hpp | 28 ++++++ others/C/C++/include/string.hpp | 25 ++++++ others/C/C++/src/main.cpp | 7 ++ others/C/C++/src/password_generator.cpp | 96 +++++++++++++++++++++ others/C/C++/src/password_gui.cpp | 36 ++++++++ others/C/C++/src/string.cpp | 51 +++++++++++ 8 files changed, 290 insertions(+) create mode 100644 others/C/C++/Makefile create mode 100644 others/C/C++/include/password_generator.hpp create mode 100644 others/C/C++/include/password_gui.hpp create mode 100644 others/C/C++/include/string.hpp create mode 100644 others/C/C++/src/main.cpp create mode 100644 others/C/C++/src/password_generator.cpp create mode 100644 others/C/C++/src/password_gui.cpp create mode 100644 others/C/C++/src/string.cpp diff --git a/others/C/C++/Makefile b/others/C/C++/Makefile new file mode 100644 index 0000000..1bcac6d --- /dev/null +++ b/others/C/C++/Makefile @@ -0,0 +1,20 @@ +CXX = g++ +CXXFLAGS = -std=c++11 -Iinclude +LDFLAGS = -static -mwindows -lfltk -lole32 -luuid -lcomctl32 -lgdi32 -lwsock32 -lcomdlg32 -lwinspool + +SRC = $(wildcard src/*.cpp) +OBJ = $(patsubst src/%.cpp,obj/%.o,$(SRC)) +TARGET = password_generator + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CXX) $^ -o $@ $(LDFLAGS) + +obj/%.o: src/%.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJ) $(TARGET) + +.PHONY: all clean diff --git a/others/C/C++/include/password_generator.hpp b/others/C/C++/include/password_generator.hpp new file mode 100644 index 0000000..ca66659 --- /dev/null +++ b/others/C/C++/include/password_generator.hpp @@ -0,0 +1,27 @@ +#ifndef PASSWORD_GENERATOR_HPP +#define PASSWORD_GENERATOR_HPP + +#include "string.hpp" + +class PasswordGenerator { +public: + enum Mode { + DIGITS, // 纯数字 + ENGLISH, // 纯英文 + SYMBOLS, // 纯符号 + DIGITS_ENGLISH, // 数字+英文 + DIGITS_SYMBOLS, // 数字+符号 + ENGLISH_SYMBOLS // 英文+符号 + }; + + PasswordGenerator(); + String generate(int length, Mode mode = DIGITS); + +private: + String generateDigits(int length); + String generateEnglish(int length); + String generateSymbols(int length); + String generateMixed(int length, const String& charSet); +}; + +#endif // PASSWORD_GENERATOR_HPP diff --git a/others/C/C++/include/password_gui.hpp b/others/C/C++/include/password_gui.hpp new file mode 100644 index 0000000..54f82e5 --- /dev/null +++ b/others/C/C++/include/password_gui.hpp @@ -0,0 +1,28 @@ +#ifndef PASSWORD_GUI_HPP +#define PASSWORD_GUI_HPP + +#include +#include +#include +#include +#include +#include +#include "password_generator.hpp" + +class PasswordGUI { +public: + PasswordGUI(); + void show(); + +private: + Fl_Window* window; + Fl_Input* lengthInput; + Fl_Choice* modeChoice; + Fl_Button* generateButton; + Fl_Output* passwordOutput; + PasswordGenerator generator; + + static void onGenerate(Fl_Widget* widget, void* data); +}; + +#endif // PASSWORD_GUI_HPP diff --git a/others/C/C++/include/string.hpp b/others/C/C++/include/string.hpp new file mode 100644 index 0000000..b2a152a --- /dev/null +++ b/others/C/C++/include/string.hpp @@ -0,0 +1,25 @@ +#ifndef STRING_HPP +#define STRING_HPP + +#include + +class String { +public: + String(); + String(const char* str); + String(const String& other); + ~String(); + + size_t length() const; + const char* c_str() const; + + String& operator=(const String& other); + String operator+(const String& other) const; + bool operator==(const String& other) const; + +private: + char* data; + size_t len; +}; + +#endif // STRING_HPP diff --git a/others/C/C++/src/main.cpp b/others/C/C++/src/main.cpp new file mode 100644 index 0000000..7fd08d9 --- /dev/null +++ b/others/C/C++/src/main.cpp @@ -0,0 +1,7 @@ +#include "password_gui.hpp" + +int main(int argc, char** argv) { + PasswordGUI gui; + gui.show(); + return Fl::run(); +} diff --git a/others/C/C++/src/password_generator.cpp b/others/C/C++/src/password_generator.cpp new file mode 100644 index 0000000..96e37c4 --- /dev/null +++ b/others/C/C++/src/password_generator.cpp @@ -0,0 +1,96 @@ +#include "password_generator.hpp" +#include +#include + +PasswordGenerator::PasswordGenerator() { + srand(time(nullptr)); +} + +String PasswordGenerator::generate(int length, Mode mode) { + if (length < 1 || length > 100) { + return String("无效长度(1-100)"); + } + + switch (mode) { + case DIGITS: + return generateDigits(length); + case ENGLISH: + return generateEnglish(length); + case SYMBOLS: + return generateSymbols(length); + case DIGITS_ENGLISH: { + String charSet = String("0123456789") + + String("abcdefghijklmnopqrstuvwxyz") + + String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + return generateMixed(length, charSet); + } + case DIGITS_SYMBOLS: { + String charSet = String("0123456789") + String("!@#$%^&*()_+-=[]{}|;:,.<>?"); + return generateMixed(length, charSet); + } + case ENGLISH_SYMBOLS: { + String charSet = String("abcdefghijklmnopqrstuvwxyz") + + String("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + + String("!@#$%^&*()_+-=[]{}|;:,.<>?"); + return generateMixed(length, charSet); + } + default: + return String("不支持的模式"); + } +} + +String PasswordGenerator::generateDigits(int length) { + char* buffer = new char[length + 1]; + for (int i = 0; i < length; ++i) { + buffer[i] = '0' + (rand() % 10); + } + buffer[length] = '\0'; + + String result(buffer); + delete[] buffer; + return result; +} + +String PasswordGenerator::generateEnglish(int length) { + char* buffer = new char[length + 1]; + for (int i = 0; i < length; ++i) { + int choice = rand() % 2; + if (choice == 0) { + buffer[i] = 'a' + (rand() % 26); // 小写字母 + } else { + buffer[i] = 'A' + (rand() % 26); // 大写字母 + } + } + buffer[length] = '\0'; + + String result(buffer); + delete[] buffer; + return result; +} + +String PasswordGenerator::generateSymbols(int length) { + const char symbols[] = "!@#$%^&*()_+-=[]{}|;:,.<>?"; + const int symbolCount = sizeof(symbols) - 1; // 减去末尾的\0 + + char* buffer = new char[length + 1]; + for (int i = 0; i < length; ++i) { + buffer[i] = symbols[rand() % symbolCount]; + } + buffer[length] = '\0'; + + String result(buffer); + delete[] buffer; + return result; +} + +String PasswordGenerator::generateMixed(int length, const String& charSet) { + char* buffer = new char[length + 1]; + for (int i = 0; i < length; ++i) { + buffer[i] = charSet.c_str()[rand() % charSet.length()]; + } + buffer[length] = '\0'; + + String result(buffer); + delete[] buffer; + return result; +} diff --git a/others/C/C++/src/password_gui.cpp b/others/C/C++/src/password_gui.cpp new file mode 100644 index 0000000..a18f764 --- /dev/null +++ b/others/C/C++/src/password_gui.cpp @@ -0,0 +1,36 @@ +#include "password_gui.hpp" +#include + +PasswordGUI::PasswordGUI() { + window = new Fl_Window(400, 350, "密码生成器"); + + lengthInput = new Fl_Input(150, 50, 200, 30, "密码长度:"); + modeChoice = new Fl_Choice(150, 90, 200, 30, "密码模式:"); + modeChoice->add("纯数字"); + modeChoice->add("纯英文"); + modeChoice->add("纯符号"); + modeChoice->add("数字+英文"); + modeChoice->add("数字+符号"); + modeChoice->add("英文+符号"); + modeChoice->value(0); // 默认选择纯数字模式 + + generateButton = new Fl_Button(150, 140, 100, 40, "生成密码"); + passwordOutput = new Fl_Output(100, 210, 250, 40, "生成结果:"); + + generateButton->callback(onGenerate, this); + window->end(); +} + +void PasswordGUI::show() { + window->show(); +} + +void PasswordGUI::onGenerate(Fl_Widget* widget, void* data) { + PasswordGUI* gui = static_cast(data); + const char* lengthStr = gui->lengthInput->value(); + int length = atoi(lengthStr); + + PasswordGenerator::Mode mode = static_cast(gui->modeChoice->value()); + String password = gui->generator.generate(length, mode); + gui->passwordOutput->value(password.c_str()); +} diff --git a/others/C/C++/src/string.cpp b/others/C/C++/src/string.cpp new file mode 100644 index 0000000..8bc4c73 --- /dev/null +++ b/others/C/C++/src/string.cpp @@ -0,0 +1,51 @@ +#include "string.hpp" +#include + +String::String() : data(nullptr), len(0) {} + +String::String(const char* str) { + len = strlen(str); + data = new char[len + 1]; + strcpy(data, str); +} + +String::String(const String& other) { + len = other.len; + data = new char[len + 1]; + strcpy(data, other.data); +} + +String::~String() { + delete[] data; +} + +size_t String::length() const { + return len; +} + +const char* String::c_str() const { + return data; +} + +String& String::operator=(const String& other) { + if (this != &other) { + delete[] data; + len = other.len; + data = new char[len + 1]; + strcpy(data, other.data); + } + return *this; +} + +String String::operator+(const String& other) const { + String result; + result.len = len + other.len; + result.data = new char[result.len + 1]; + strcpy(result.data, data); + strcat(result.data, other.data); + return result; +} + +bool String::operator==(const String& other) const { + return strcmp(data, other.data) == 0; +}