专栏文章
炒鸡计算机
科技·工程参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miq7wmu2
- 此快照首次捕获于
- 2025/12/04 00:24 3 个月前
- 此快照最后确认于
- 2025/12/04 00:24 3 个月前
待完善.
CPP#include <windows.h>
#include <sstream>
#include <string>
#include <cmath>
HINSTANCE hInst;
HWND hwndDisplay, hwndButton[16];
std::string currentInput = "";
std::string result = "";
// 计算函数,支持加减乘除
double calculate(const std::string& input) {
double num1 = 0.0, num2 = 0.0;
char op = '+';
size_t pos = 0;
// 遍历输入字符串,进行计算
for (size_t i = 0; i < input.size(); ++i) {
if (isdigit(input[i]) || input[i] == '.') {
// 处理数字和小数点
num2 = num2 * 10 + (input[i] - '0');
if (i < input.size() - 1 && input[i + 1] == '.') {
// 如果是小数点,处理小数部分
double decimal = 0.1;
++i; // 跳过小数点
while (i < input.size() && isdigit(input[i])) {
num2 += (input[i] - '0') * decimal;
decimal *= 0.1;
++i;
}
--i; // 回到正确的字符位置
}
} else if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/') {
// 根据操作符计算
if (op == '+') num1 += num2;
else if (op == '-') num1 -= num2;
else if (op == '*') num1 *= num2;
else if (op == '/') num1 /= num2;
num2 = 0; // 重新开始计算下一个数字
op = input[i]; // 更新操作符
}
}
// 最后一次运算
if (op == '+') num1 += num2;
else if (op == '-') num1 -= num2;
else if (op == '*') num1 *= num2;
else if (op == '/') num1 /= num2;
return num1;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE: {
// 创建显示框
hwndDisplay = CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT,
10, 10, 250, 30, hwnd, (HMENU)1, hInst, NULL);
// 创建按钮
const char* buttonLabels[] = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "C"};
for (int i = 0; i < 16; ++i) {
hwndButton[i] = CreateWindow("BUTTON", buttonLabels[i], WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
10 + (i % 4) * 60, 50 + (i / 4) * 50, 50, 40, hwnd, (HMENU)(i + 2), hInst, NULL);
}
break;
}
case WM_COMMAND: {
if (LOWORD(wp) >= 2 && LOWORD(wp) <= 17) {
// 获取按钮的标签
int index = LOWORD(wp) - 2;
const char* label = ((const char*[]){"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "C"})[index];
if (label[0] == '=') {
// 计算结果
try {
double value = calculate(currentInput);
std::ostringstream oss;
oss << value;
result = oss.str();
}
catch (std::exception&) {
result = "Error";
}
currentInput = result; // 显示结果后清空输入框
} else if (label[0] == 'C') {
// 清空输入
currentInput = "";
result = "";
} else {
// 更新输入
currentInput += label;
}
SetWindowText(hwndDisplay, currentInput.c_str());
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
int main() {
// 初始化窗口
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = "CalculatorWindowClass";
if (!RegisterClass(&wc)) {
return -1;
}
HWND hwnd = CreateWindowEx(0, "CalculatorWindowClass", "Calculator", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 280, 400, NULL, NULL, hInst, NULL);
if (hwnd == NULL) {
return -1;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...