-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomialGUI.cpp
More file actions
388 lines (339 loc) · 10.3 KB
/
PolynomialGUI.cpp
File metadata and controls
388 lines (339 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// PolynomialGUI.cpp
#include "PolynomialGUI.h"
#include "resource.h"
#include <sstream>
#include <fstream>
#define MAX_LOADSTRING 100
extern HINSTANCE hInst;
extern WCHAR szWindowClass[MAX_LOADSTRING];
extern WCHAR szTitle[MAX_LOADSTRING];
PolynomialGUI::PolynomialGUI(HINSTANCE hInst)
: hInstance(hInst), hWnd(nullptr), currentPoly(nullptr) {
hEditPoly1 = hEditPoly2 = nullptr;
hBtnSplit = hBtnAdd = hBtnMultiply = hBtnSave = hBtnDivide = nullptr;
}
PolynomialGUI::~PolynomialGUI() {
delete currentPoly;
}
void PolynomialGUI::createControls() {
// 创建输入框1
hEditPoly1 = CreateWindowW(
L"EDIT",
L"3x^2+5x+7",
WS_CHILD | WS_VISIBLE | WS_BORDER,
20, 20, 300, 30,
hWnd,
(HMENU)1001,
hInstance,
nullptr);
// "拆分多项式"按钮
hBtnSplit = CreateWindowW(
L"BUTTON",
L"拆分多项式",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 60, 120, 30,
hWnd,
(HMENU)1002,
hInstance,
nullptr);
// 创建输入框2
hEditPoly2 = CreateWindowW(
L"EDIT",
L"2x^2+3x+1",
WS_CHILD | WS_VISIBLE | WS_BORDER,
20, 100, 300, 30,
hWnd,
(HMENU)1003,
hInstance,
nullptr);
// "多项式相加"按钮
hBtnAdd = CreateWindowW(
L"BUTTON",
L"多项式相加",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 140, 120, 30,
hWnd,
(HMENU)1004,
hInstance,
nullptr);
// "多项式相乘"按钮
hBtnMultiply = CreateWindowW(
L"BUTTON",
L"多项式相乘",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 180, 120, 30,
hWnd,
(HMENU)1005,
hInstance,
nullptr);
// "保存结果"按钮
hBtnSave = CreateWindowW(
L"BUTTON",
L"保存结果",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 220, 120, 30,
hWnd,
(HMENU)1006,
hInstance,
nullptr);
// "多项式相除"按钮
hBtnDivide = CreateWindowW(
L"BUTTON",
L"多项式相除",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 260, 120, 30,
hWnd,
(HMENU)1007,
hInstance,
nullptr);
}
void PolynomialGUI::drawBarChart(HDC hdc) {
if (!currentPoly || !currentPoly->getHead()) return;
int x = LEFT_X;
for (Term* t = currentPoly->getHead(); t; t = t->next) {
int h = abs(t->coef) * SCALE;
COLORREF col = (t->exp & 1) ? RGB(255, 100, 100) : RGB(100, 100, 255);
HBRUSH hbr = CreateSolidBrush(col);
RECT rc = { x, TOP_Y, x + BAR_W, TOP_Y + h };
FillRect(hdc, &rc, hbr);
DeleteObject(hbr);
// 在柱下方标指数
wchar_t label[16];
wsprintfW(label, L"x^%d", t->exp);
TextOutW(hdc, x, TOP_Y + h + 5, label, (int)wcslen(label));
x += BAR_W + GAP;
}
}
void PolynomialGUI::updateDisplay(const std::wstring& text, Polynomial* poly) {
displayText = text;
delete currentPoly;
if (poly) {
currentPoly = new Polynomial(*poly);
}
else {
currentPoly = nullptr;
}
InvalidateRect(hWnd, nullptr, TRUE);
}
void PolynomialGUI::onSplit() {
HWND hEdit = GetDlgItem(hWnd, 1001);
wchar_t buf[256] = {};
GetWindowTextW(hEdit, buf, 256);
Polynomial poly(buf);
std::wstringstream ss;
for (Term* t = poly.getHead(); t; t = t->next) {
wchar_t tmp[64];
wsprintfW(tmp, L"%dx^%d ", t->coef, t->exp);
ss << tmp;
}
updateDisplay(ss.str(), &poly);
}
void PolynomialGUI::onAdd() {
// 读第一个多项式
HWND h1 = GetDlgItem(hWnd, 1001);
wchar_t buf1[256];
GetWindowTextW(h1, buf1, 256);
Polynomial p1(buf1);
// 读第二个多项式
HWND h2 = GetDlgItem(hWnd, 1003);
wchar_t buf2[256];
GetWindowTextW(h2, buf2, 256);
Polynomial p2(buf2);
// 相加
Polynomial sum = p1.add(p2);
// 显示结果
std::wstring poly1 = p1.toString();
std::wstring poly2 = p2.toString();
std::wstring result = sum.toString();
std::wstring display = L"(" + poly1 + L") + (" + poly2 + L") = " + result;
updateDisplay(display, &sum);
}
void PolynomialGUI::onMultiply() {
// 读第一个多项式
HWND h1 = GetDlgItem(hWnd, 1001);
wchar_t buf1[256];
GetWindowTextW(h1, buf1, 256);
Polynomial p1(buf1);
// 读第二个多项式
HWND h2 = GetDlgItem(hWnd, 1003);
wchar_t buf2[256];
GetWindowTextW(h2, buf2, 256);
Polynomial p2(buf2);
// 相乘
Polynomial prod = p1.multiply(p2);
// 显示结果
std::wstring poly1 = p1.toString();
std::wstring poly2 = p2.toString();
std::wstring result = prod.toString();
std::wstring display = L"(" + poly1 + L") * (" + poly2 + L") = " + result;
updateDisplay(display, &prod);
}
void PolynomialGUI::onSave() {
std::wofstream fs("result.txt");
if (fs) {
fs << displayText;
fs.close();
MessageBoxW(hWnd, L"已保存到程序目录下的 result.txt", L"提示", MB_OK);
}
else {
MessageBoxW(hWnd, L"保存文件失败", L"错误", MB_OK | MB_ICONERROR);
}
}
void PolynomialGUI::onDivide() {
// 读被除式、除式
HWND h1 = GetDlgItem(hWnd, 1001);
HWND h2 = GetDlgItem(hWnd, 1003);
wchar_t buf1[256], buf2[256];
GetWindowTextW(h1, buf1, 256);
GetWindowTextW(h2, buf2, 256);
Polynomial p1(buf1);
Polynomial p2(buf2);
// 检查除式是否为空
if (p2.isZero()) {
MessageBoxW(hWnd, L"除式不能为 0!", L"错误", MB_OK);
return;
}
try {
// 执行除法 - 不使用结构化绑定
auto divisionResult = p1.divide(p2);
Polynomial quotient = divisionResult.first;
Polynomial remainder = divisionResult.second;
// 使用 toString 显示美观结果
std::wstring poly1_str = p1.toString();
std::wstring poly2_str = p2.toString();
std::wstring quotient_str = quotient.toString();
std::wstring remainder_str = remainder.toString();
// 构建清晰的显示字符串
std::wstringstream ss;
ss << L"多项式除法:\r\n";
ss << L"=========================\r\n";
ss << L"被除式: " + poly1_str + L"\r\n";
ss << L"除式: " + poly2_str + L"\r\n";
ss << L"-------------------------\r\n";
if (remainder_str == L"0") {
// 能整除的情况
ss << L"商: " + quotient_str + L"\r\n";
ss << L"余式: " + remainder_str + L"\r\n";
ss << L"-------------------------\r\n";
ss << L"结果: (" + poly1_str + L") ÷ (" + poly2_str + L") = " + quotient_str;
}
else if (quotient_str == L"0") {
// 商为0的情况(不能整除)
ss << L"商: " + quotient_str + L"\r\n";
ss << L"余式: " + remainder_str + L"\r\n";
ss << L"-------------------------\r\n";
ss << L"结果: (" + poly1_str + L") ÷ (" + poly2_str + L") = ";
ss << L"(" + remainder_str + L")/(" + poly2_str + L")";
}
else {
// 一般情况(有商有余式)
ss << L"商: " + quotient_str + L"\r\n";
ss << L"余式: " + remainder_str + L"\r\n";
ss << L"-------------------------\r\n";
ss << L"结果: (" + poly1_str + L") ÷ (" + poly2_str + L") = ";
ss << quotient_str + L" + (" + remainder_str + L")/(" + poly2_str + L")";
}
updateDisplay(ss.str(), "ient);
// 保存到文件
std::wofstream fs("division.txt");
if (fs) {
fs << L"多项式除法结果" << std::endl;
fs << L"==================" << std::endl;
fs << L"被除式: " << poly1_str << std::endl;
fs << L"除式: " << poly2_str << std::endl;
fs << L"商: " << quotient_str << std::endl;
fs << L"余式: " << remainder_str << std::endl;
fs << std::endl << L"结果显示:" << std::endl;
fs << displayText << std::endl;
fs.close();
MessageBoxW(hWnd, L"除法结果已保存到 division.txt", L"提示", MB_OK);
}
}
catch (const std::exception& e) {
MessageBoxW(hWnd, L"多项式除法出错", L"错误", MB_OK | MB_ICONERROR);
}
}
bool PolynomialGUI::createWindow(int nCmdShow) {
hWnd = CreateWindowW(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
800, 900,
nullptr,
nullptr,
hInstance,
nullptr);
if (!hWnd) {
return false;
}
createControls();
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return true;
}
LRESULT PolynomialGUI::handleMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND: {
int wmId = LOWORD(wParam);
switch (wmId) {
case IDM_ABOUT:
DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd,
[](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
switch (message) {
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
});
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case 1002:
onSplit();
break;
case 1004:
onAdd();
break;
case 1005:
onMultiply();
break;
case 1006:
onSave();
break;
case 1007:
onDivide();
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 1. 左侧文字区
RECT textRc = { 20, 320, 400, 580 };
SetBkMode(hdc, TRANSPARENT);
DrawTextW(hdc, displayText.c_str(), (int)displayText.length(),
&textRc, DT_LEFT | DT_WORDBREAK | DT_EXPANDTABS);
// 2. 右侧柱形图
drawBarChart(hdc);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}