#include <windows.h>
#include <shlobj.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ID_BUTTON_BROWSE 1
#define ID_BUTTON_PROCESS 2
#define ID_EDIT_FOLDER 3
#define MAX_SYMBOLS 10
#define MAX_LINE_LENGTH 10000
#define MAX_PATH_LENGTH 260
typedef struct {
char open;
char close;
} SymbolPair;
SymbolPair symbolPairs[MAX_SYMBOLS];
int numPairs = 0;
char folderPath[MAX_PATH_LENGTH];
// 随机生成Data.txt文件
void generate_random_data(const char *folderPath) {
char filePath[MAX_PATH_LENGTH];
snprintf(filePath, sizeof(filePath), “%s\Data.txt”, folderPath);
FILE *file = fopen(filePath, "w"); if (!file) { MessageBox(NULL, "Error: Unable to create Data.txt file.", "Error", MB_OK | MB_ICONERROR); return; } const char symbols[] = "()[]{}"; srand((unsigned int)time(NULL)); int numLines = rand() % 100 + 1; // 随机生成 1 到 100 行 for (int i = 0; i < numLines; i++) { int length = rand() % 50 + 1; // 每行随机长度 1 到 50 for (int j = 0; j < length; j++) { char symbol = symbols[rand() % strlen(symbols)]; fputc(symbol, file); } fputc('\n', file); // 换行符 } fclose(file); MessageBox(NULL, "Data.txt has been generated successfully!", "Success", MB_OK | MB_ICONINFORMATION);
}
// 从Symbol.txt文件加载符号对
int load_symbol_file(const char *folderPath) {
char filePath[MAX_PATH_LENGTH];
snprintf(filePath, sizeof(filePath), “%s\Symbol.txt”, folderPath);
FILE *file = fopen(filePath, “r”);
if (!file) {
MessageBox(NULL, “Error: Unable to open Symbol.txt file.”, “Error”, MB_OK | MB_ICONERROR);
return 0;
}
numPairs = 0; char line[256]; while (fgets(line, sizeof(line), file) && numPairs < MAX_SYMBOLS) { char open, close; if (sscanf(line, "%c %c", &open, &close) == 2) { symbolPairs[numPairs].open = open; symbolPairs[numPairs].close = close; numPairs++; } } fclose(file); return numPairs > 0;
}
// 判断是否是开符号
int is_opening(char ch) {
for (int i = 0; i < numPairs; i++) {
if (symbolPairs[i].open == ch) {
return 1;
}
}
return 0;
}
// 判断是否是闭符号
int is_closing(char ch) {
for (int i = 0; i < numPairs; i++) {
if (symbolPairs[i].close == ch) {
return 1;
}
}
return 0;
}
// 判断符号是否匹配
int matches(char open, char close) {
for (int i = 0; i < numPairs; i++) {
if (symbolPairs[i].open == open && symbolPairs[i].close == close) {
return 1;
}
}
return 0;
}
// 检查符号是否配对
int check_matching(const char *line) {
char *stack = (char *)malloc(strlen(line) + 1);
int top = -1;
for (int i = 0; line[i] != '\0'; i++) { char ch = line[i]; if (is_opening(ch)) { stack[++top] = ch; // 入栈 } else if (is_closing(ch)) { if (top == -1 || !matches(stack[top], ch)) { free(stack); return 0; } top--; // 出栈 } } int result = (top == -1); free(stack); return result;
}
// 检查Data.txt中的每一行
void check_data_file(const char *folderPath) {
char dataPath[MAX_PATH_LENGTH];
snprintf(dataPath, sizeof(dataPath), “%s\Data.txt”, folderPath);
char resultPath[MAX_PATH_LENGTH]; snprintf(resultPath, sizeof(resultPath), "%s\\Result.txt", folderPath); FILE *dataFile = fopen(dataPath, "r"); if (!dataFile) { MessageBox(NULL, "Error: Unable to open Data.txt file.", "Error", MB_OK | MB_ICONERROR); return; } FILE *resultFile = fopen(resultPath, "w"); if (!resultFile) { MessageBox(NULL, "Error: Unable to create/write to Result.txt file.", "Error", MB_OK | MB_ICONERROR); fclose(dataFile); return; } char line[MAX_LINE_LENGTH]; while (fgets(line, sizeof(line), dataFile)) { // 去掉行末的换行符 line[strcspn(line, "\n")] = '\0'; // 检查是否匹配 if (check_matching(line)) { fprintf(resultFile, "YES\n"); } else { fprintf(resultFile, "NO\n"); } } fclose(dataFile); fclose(resultFile); MessageBox(NULL, "Result.txt has been generated successfully!", "Success", MB_OK | MB_ICONINFORMATION);
}
// 文件夹选择对话框
void browse_folder(HWND hwnd) {
BROWSEINFO bi = {0};
bi.lpszTitle = “Select Folder Containing Symbol.txt and Data.txt”;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi); if (pidl != 0) { SHGetPathFromIDList(pidl, folderPath); SetDlgItemText(hwnd, ID_EDIT_FOLDER, folderPath); }
}
// 窗口过程
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
CreateWindow(“BUTTON”, “Browse”, WS_VISIBLE | WS_CHILD,
50, 50, 100, 30, hwnd, (HMENU)ID_BUTTON_BROWSE, NULL, NULL);
CreateWindow(“EDIT”, “”, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
160, 50, 400, 30, hwnd, (HMENU)ID_EDIT_FOLDER, NULL, NULL);
CreateWindow(“BUTTON”, “Process”, WS_VISIBLE | WS_CHILD,
50, 100, 100, 30, hwnd, (HMENU)ID_BUTTON_PROCESS, NULL, NULL);
break;
case WM_COMMAND: switch (LOWORD(wParam)) { case ID_BUTTON_BROWSE: browse_folder(hwnd); break; case ID_BUTTON_PROCESS: GetDlgItemText(hwnd, ID_EDIT_FOLDER, folderPath, sizeof(folderPath)); if (strlen(folderPath) == 0) { MessageBox(hwnd, "Please select a folder or enter a valid path.", "Error", MB_OK | MB_ICONERROR); break; } if (load_symbol_file(folderPath)) { check_data_file(folderPath); } else { MessageBox(hwnd, "Symbol.txt not found or invalid.", "Error", MB_OK | MB_ICONERROR); } break; } break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 绘制圆形按钮 HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); HBRUSH hBrush = CreateSolidBrush(RGB(246, 182, 6)); // 绿色填充 SelectObject(hdc, hPen); SelectObject(hdc, hBrush); Ellipse(hdc, 320, 150, 400, 230); // 缩小圆形区域(直径 80 像素) // 绘制按钮文字 SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(255, 255, 255)); DrawText(hdc, "Generate", -1, &(RECT){320, 150, 400, 230}, DT_CENTER | DT_VCENTER | DT_SINGLELINE); DeleteObject(hPen); DeleteObject(hBrush); EndPaint(hwnd, &ps); break; } case WM_LBUTTONDOWN: { int xPos = LOWORD(lParam); // 鼠标点击位置 int yPos = HIWORD(lParam); // 检查是否点击了圆形按钮区域 if (xPos >= 300 && xPos <= 400 && yPos >= 100 && yPos <= 200) { GetDlgItemText(hwnd, ID_EDIT_FOLDER, folderPath, sizeof(folderPath)); if (strlen(folderPath) == 0) { MessageBox(hwnd, "Please select a folder or enter a valid path.", "Error", MB_OK | MB_ICONERROR); break; } generate_random_data(folderPath); } break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0;
}
// 主函数入口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
const char CLASS_NAME[] = “Random Data Generator”;
WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); HWND hwnd = CreateWindow(CLASS_NAME, "Symbol Validator", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 300, NULL, NULL, hInstance, NULL); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;
}
整合到原程序中,给出完整代码
最新发布