-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSource.cpp
243 lines (225 loc) · 5.48 KB
/
Source.cpp
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
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib, "x64\\Debug\\WinNTL-9_8_1.lib")
#else
#pragma comment(lib, "x64\\Release\\WinNTL-9_8_1.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "Debug\\WinNTL-9_8_1.lib")
#else
#pragma comment(lib, "Release\\WinNTL-9_8_1.lib")
#endif
#endif
#include "WinNTL-9_8_1\include\NTL\ZZ.h"
#include <windows.h>
#include <sstream>
using namespace NTL;
using namespace std;
TCHAR szClassName[] = TEXT("Window");
string ZZToString(const ZZ &z)
{
stringstream buffer;
buffer << z;
return buffer.str();
}
void hsv2rgb(double h, double s, double v, double*r, double*g, double*b)
{
while (h >= 360)h -= 360;
const int i = (int)(h / 60.0);
const double f = (h / 60.0) - i;
const double p1 = v * (1 - s);
const double p2 = v * (1 - s*f);
const double p3 = v * (1 - s*(1 - f));
switch (i)
{
case 0: *r = v; *g = p3; *b = p1; break;
case 1: *r = p2; *g = v; *b = p1; break;
case 2: *r = p1; *g = v; *b = p3; break;
case 3: *r = p1; *g = p2; *b = v; break;
case 4: *r = p3; *g = p1; *b = v; break;
case 5: *r = v; *g = p1; *b = p2; break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit1;
static HWND hButton1;
static HWND hButton2;
static HWND hButton3;
static HWND hButton4;
static HWND hButton5;
static ZZ n;
static enum
{
IDADD,
IDSUB,
IDMUL,
IDDIV,
IDPOW,
} Mode;
switch (msg)
{
case WM_CREATE:
hEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT("15"),
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_NUMBER | ES_AUTOHSCROLL,
0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hButton1 = CreateWindow(TEXT("BUTTON"), TEXT("加法"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)100, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hButton2 = CreateWindow(TEXT("BUTTON"), TEXT("減法"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)101, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hButton3 = CreateWindow(TEXT("BUTTON"), TEXT("乗法"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)102, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hButton4 = CreateWindow(TEXT("BUTTON"), TEXT("除法"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)103, ((LPCREATESTRUCT)lParam)->hInstance, 0);
hButton5 = CreateWindow(TEXT("BUTTON"), TEXT("累乗"), WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, (HMENU)104, ((LPCREATESTRUCT)lParam)->hInstance, 0);
break;
case WM_SIZE:
MoveWindow(hEdit1, 10, 10, 128, 32, TRUE);
MoveWindow(hButton1, 128 + 20, 10, 64, 32, TRUE);
MoveWindow(hButton2, 128 + 20 + 74 * 1, 10, 64, 32, TRUE);
MoveWindow(hButton3, 128 + 20 + 74 * 2, 10, 64, 32, TRUE);
MoveWindow(hButton4, 128 + 20 + 74 * 3, 10, 64, 32, TRUE);
MoveWindow(hButton5, 128 + 20 + 74 * 4, 10, 64, 32, TRUE);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
if (n != 0)
{
SIZE size;
GetTextExtentPoint32(hdc, TEXT("8888888888"), (int)GetWindowTextLength(hEdit1) + 1, &size);
for (ZZ x = to_ZZ(0); x <= n; ++x)
{
for (ZZ y = to_ZZ(0); y <= n; ++y)
{
string str;
ZZ mod;
switch (Mode)
{
case IDADD:
mod = (x + y) % n;
break;
case IDSUB:
mod = (x - y) % n;
break;
case IDMUL:
mod = MulMod(x, y, n);
break;
case IDDIV:
if (y != 0)
{
mod = (x / y) % n;
}
else
{
mod = to_ZZ(0);
}
break;
case IDPOW:
{
mod = to_ZZ(1);
for (ZZ i = to_ZZ(0); i < y; ++i)
{
mod *= x;
}
mod %= n;
}
break;
}
double r, g, b;
double c;
long l;
conv(l, n);
long lmod;
conv(lmod, mod);
c = 360.0 * lmod / l;
hsv2rgb(c, 0.5, 1.0, &r, &g, &b);
COLORREF color = RGB(255*r,255*g,255*b);
str = ZZToString(mod);
int xx, yy;
conv(xx, x);
conv(yy, y);
SetBkColor(hdc, color);
TextOutA(hdc, xx * size.cx + 10, yy * 22 + 50, str.c_str(), (int)str.length());
}
}
}
EndPaint(hWnd, &ps);
}
break;
case WM_COMMAND:
if (LOWORD(wParam) >= 100 && LOWORD(wParam) <= 104)
{
const int len = GetWindowTextLengthA(hEdit1) + 1;
LPSTR str = (LPSTR)GlobalAlloc(GMEM_FIXED, len);
GetWindowTextA(hEdit1, str, len);
conv(n, str);
GlobalFree(str);
switch (LOWORD(wParam))
{
case 100:
Mode = IDADD;
break;
case 101:
Mode = IDSUB;
break;
case 102:
Mode = IDMUL;
break;
case 103:
Mode = IDDIV;
break;
case 104:
Mode = IDPOW;
break;
default:
break;
}
InvalidateRect(hWnd, 0, 1);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
0,
LoadCursor(0,IDC_ARROW),
(HBRUSH)(COLOR_WINDOW + 1),
0,
szClassName
};
RegisterClass(&wndclass);
HWND hWnd = CreateWindow(
szClassName,
TEXT("NTLを使ったサンプルプログラム"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
0,
0,
hInstance,
0
);
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}