C语言窗口计算器实现

person 区块链    watch_later 2024-07-30 17:47:21
visibility 142    class C语言,计算器    bookmark 分享

使用 C 语言开发一个支持基本算术运算和括号优先级的计算器图形界面应用,可以借助 Win32 API 实现。我们将创建一个简单的 GUI 应用程序,允许用户输入表达式并计算其结果。

以下是实现一个计算器 GUI 的完整代码,包括支持加减乘除和括号优先级计算的功能。

完整代码

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define ID_INPUT 101
#define ID_OUTPUT 102
#define ID_BUTTON 103

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void CalculateExpression(const char* input, char* output);
double EvaluateExpression(const char* expr, int* pos);
double EvaluateTerm(const char* expr, int* pos);
double EvaluateFactor(const char* expr, int* pos);

// 应用程序入口点
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    const char CLASS_NAME[] = "CalculatorWindowClass";

    WNDCLASS wc = {0};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        "Simple Calculator",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, nShowCmd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    static HWND hInput, hOutput, hButton;

    switch (uMsg) {
        case WM_CREATE:
            // 创建输入框
            hInput = CreateWindow(
                "EDIT", 
                "",
                WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
                10, 10, 260, 20,
                hwnd, (HMENU)ID_INPUT, NULL, NULL);

            // 创建输出框
            hOutput = CreateWindow(
                "EDIT", 
                "",
                WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_READONLY,
                10, 40, 260, 20,
                hwnd, (HMENU)ID_OUTPUT, NULL, NULL);

            // 创建计算按钮
            hButton = CreateWindow(
                "BUTTON", 
                "Calculate",
                WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                10, 70, 260, 30,
                hwnd, (HMENU)ID_BUTTON, NULL, NULL);
            break;

        case WM_COMMAND:
            if (LOWORD(wParam) == ID_BUTTON) {
                char input[256], output[256];
                // 从输入框获取表达式
                GetWindowText(hInput, input, sizeof(input));
                // 计算表达式结果
                CalculateExpression(input, output);
                // 显示结果在输出框
                SetWindowText(hOutput, output);
            }
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// 计算表达式并返回结果字符串
void CalculateExpression(const char* input, char* output) {
    int pos = 0;
    double result = EvaluateExpression(input, &pos);
    if (input[pos] != '\0') {
        strcpy(output, "Error: Invalid Expression");
    } else {
        sprintf(output, "%.2f", result);
    }
}

// 解析表达式(支持加减运算和括号优先级)
double EvaluateExpression(const char* expr, int* pos) {
    double result = EvaluateTerm(expr, pos);

    while (expr[*pos] == '+' || expr[*pos] == '-') {
        char op = expr[*pos];
        (*pos)++;
        double term = EvaluateTerm(expr, pos);
        if (op == '+') {
            result += term;
        } else {
            result -= term;
        }
    }
    return result;
}

// 解析项(支持乘除运算)
double EvaluateTerm(const char* expr, int* pos) {
    double result = EvaluateFactor(expr, pos);

    while (expr[*pos] == '*' || expr[*pos] == '/') {
        char op = expr[*pos];
        (*pos)++;
        double factor = EvaluateFactor(expr, pos);
        if (op == '*') {
            result *= factor;
        } else {
            result /= factor;
        }
    }
    return result;
}

// 解析因子(支持括号)
double EvaluateFactor(const char* expr, int* pos) {
    double result;

    while (isspace(expr[*pos])) (*pos)++;

    if (expr[*pos] == '(') {
        (*pos)++;
        result = EvaluateExpression(expr, pos);
        if (expr[*pos] == ')') {
            (*pos)++;
        } else {
            return 0;  // 错误:括号不匹配
        }
    } else {
        char number[64];
        int n = 0;
        while (isdigit(expr[*pos]) || expr[*pos] == '.') {
            number[n++] = expr[*pos];
            (*pos)++;
        }
        number[n] = '\0';
        result = atof(number);
    }

    while (isspace(expr[*pos])) (*pos)++;

    return result;
}

代码详细解释

1. 头文件和宏定义

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define ID_INPUT 101
#define ID_OUTPUT 102
#define ID_BUTTON 103
  • windows.h:包含 Windows API 函数和类型定义。
  • stdio.hstring.hstdlib.hctype.h:包含标准 I/O、字符串操作、通用工具函数和字符操作函数。
  • 定义控件的 ID(输入框、输出框、按钮)。

2. 窗口过程函数

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  • WindowProc:用于处理窗口的消息,包括创建控件、按钮点击事件、窗口销毁等。

3. 应用程序入口点

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    const char CLASS_NAME[] = "CalculatorWindowClass";

    WNDCLASS wc = {0};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        "Simple Calculator",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, nShowCmd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
  • WinMain:程序入口函数,用于初始化窗口类、创建窗口和进入消息循环。
  • RegisterClass:注册窗口类。
  • CreateWindowEx:创建窗口并设置其属性。
  • ShowWindow:显示窗口。
  • GetMessageTranslateMessageDispatchMessage:处理消息循环。

4. 创建控件

case WM_CREATE:
    hInput = CreateWindow(
        "EDIT", 
        "",
        WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
        10, 10, 260, 20,
        hwnd, (HMENU)ID_INPUT, NULL, NULL);

    hOutput = CreateWindow(
        "EDIT", 
        "",
        WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_READONLY,
        10, 40, 260, 20,
        hwnd, (HMENU)ID_OUTPUT, NULL, NULL);

    hButton = CreateWindow(
        "BUTTON", 
        "Calculate",
        WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
        10, 70, 260, 30,
        hwnd, (HMENU)ID_BUTTON, NULL, NULL);
    break;
  • WM_CREATE:窗口创建消息,用于创建控件。
  • CreateWindow:用于创建输入框、输出框和按钮控件。

5. 处理按钮

点击事件

case WM_COMMAND:
    if (LOWORD(wParam) == ID_BUTTON) {
        char input[256], output[256];
        GetWindowText(hInput, input, sizeof(input));
        CalculateExpression(input, output);
        SetWindowText(hOutput, output);
    }
    break;
  • WM_COMMAND:处理按钮点击事件。
  • GetWindowText:从输入框获取文本。
  • CalculateExpression:计算表达式并生成结果。
  • SetWindowText:将结果设置到输出框。

6. 计算表达式

void CalculateExpression(const char* input, char* output) {
    int pos = 0;
    double result = EvaluateExpression(input, &pos);
    if (input[pos] != '\0') {
        strcpy(output, "Error: Invalid Expression");
    } else {
        sprintf(output, "%.2f", result);
    }
}
  • CalculateExpression:计算表达式的结果并生成字符串输出。

7. 表达式解析函数

double EvaluateExpression(const char* expr, int* pos);
double EvaluateTerm(const char* expr, int* pos);
double EvaluateFactor(const char* expr, int* pos);
  • EvaluateExpression:解析加减运算。
  • EvaluateTerm:解析乘除运算。
  • EvaluateFactor:解析括号和数字。

8. 实现表达式解析

double EvaluateExpression(const char* expr, int* pos) {
    double result = EvaluateTerm(expr, pos);
    while (expr[*pos] == '+' || expr[*pos] == '-') {
        char op = expr[*pos];
        (*pos)++;
        double term = EvaluateTerm(expr, pos);
        if (op == '+') {
            result += term;
        } else {
            result -= term;
        }
    }
    return result;
}

double EvaluateTerm(const char* expr, int* pos) {
    double result = EvaluateFactor(expr, pos);
    while (expr[*pos] == '*' || expr[*pos] == '/') {
        char op = expr[*pos];
        (*pos)++;
        double factor = EvaluateFactor(expr, pos);
        if (op == '*') {
            result *= factor;
        } else {
            result /= factor;
        }
    }
    return result;
}

double EvaluateFactor(const char* expr, int* pos) {
    double result;
    while (isspace(expr[*pos])) (*pos)++;
    if (expr[*pos] == '(') {
        (*pos)++;
        result = EvaluateExpression(expr, pos);
        if (expr[*pos] == ')') {
            (*pos)++;
        } else {
            return 0;  // 错误:括号不匹配
        }
    } else {
        char number[64];
        int n = 0;
        while (isdigit(expr[*pos]) || expr[*pos] == '.') {
            number[n++] = expr[*pos];
            (*pos)++;
        }
        number[n] = '\0';
        result = atof(number);
    }
    while (isspace(expr[*pos])) (*pos)++;
    return result;
}
  • EvaluateExpression:计算加减运算。
  • EvaluateTerm:计算乘除运算。
  • EvaluateFactor:计算括号内的表达式和数字。

编译和运行

使用 MinGW 编译

将上述代码保存为 calculator.c 文件,然后使用 MinGW 编译器编译:

gcc -o calculator calculator.c -lgdi32
  • -lgdi32:链接 GDI 库,用于图形绘制。

运行

在命令行中运行生成的 calculator.exe 文件:

calculator.exe

这样,您就可以在 Windows 上运行一个基本的计算器应用程序,支持加减乘除和括号优先级计算。

评论区
评论列表
menu