#5
by かずま » 5年前
A.M さんが書きました: ↑5年前
係数を求めるプログラムと3次関数を求めるプログラムは同じということなのでしょうか?
同じじゃないんですか?
コード:
for (i = 0; i <= M; i++)
printf("p%d = %7.3lf\n", i, a[i][M + 1]);
これを次のように書き換えれば同じでしょう。
コード:
printf("y = %.2f %+.2f x %+.2f x^2 %+.2f x^3",
a[0][M+1], a[1][M+1], a[2][M+1], a[3][M+1]);
A.M さんが書きました: ↑5年前
また、「DxLib を使って図を書くプログラム」の方なのですが、学校では「Visual Studio 用開発者コマンド」を使っており、DxLibのやり方が申し訳ないのですがよくわかりません...。
Windows API を使って書き直してみました。
コンパイルできますか?
コード:
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 8
#define M 3
#define EPS 0.0001
double a[M + 1][M + 2];
double x[N] = { -4.0, -2.0, -1.0, 0.0, 1.0, 3.0, 4.0, 6.0 };
double y[N] = { -35.1, 15.1, 15.9, 8.9, 0.1, 0.1, 21.1, 135.0 };
double f(double x)
{
double y = 0;
for (int i = 3; i >= 0; i--) y = y * x + a[i][M+1];
return y;
}
int Jordan()
{
for (int i = 0; i <= M; i++) {
double pivot = a[i][i];
if (fabs(pivot) < EPS) {
//printf("ピボットが許容範囲誤差以下\n");
return 1;
}
for (int j = 0; j <= M + 1; j++)
a[i][j] /= pivot;
for (int k = 0; k <= M; k++) {
double delta = a[k][i];
for (int j = 0; j <= M + 1; j++)
if (k != i) a[k][j] -= delta * a[i][j];
}
}
return 0;
}
int calc()
{
for (int i = 0; i <= M; i++)
for (int j = 0; j <= M + 1; j++) {
a[j][i] = 0;
for (int k = 0; k < N; k++)
a[j][i] += pow(x[k], i + j);
}
for (int j = 0; j <= M; j++)
for (int k = 0; k < N; k++)
a[j][M + 1] += y[k] * pow(x[k], j);
return Jordan();
}
#define X(x) ((int)(O + (x - xa) * W / w))
#define Y(y) ((int)(O + H - (y - ya) * H / h))
void onPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
double O = 40;
double W = rect.right - rect.left - 2 * O;
double H = rect.bottom - rect.top - 2 * O;
double dx = 0.2;
double xa = -5, xb = 7, ya = f(xa), yb = f(xb);
double w = xb - xa, h = yb - ya;
int n = (int)(w / dx);
int x1 = X(0), y1 = Y(0);
MoveToEx(hdc, x1, O, NULL), LineTo(hdc, x1, O + H);
MoveToEx(hdc, O, y1, NULL), LineTo(hdc, O + W, y1);
HPEN pen = CreatePen(PS_SOLID, 1, RGB(192, 0, 0));
HPEN pen0 = SelectObject(hdc, pen);
x1 = X(xa), y1 = Y(f(xa));
MoveToEx(hdc, x1, y1, NULL);
for (int i = 1; i <= n; i++) {
double x = xa + i * dx;
int x2 = X(x), y2 = Y(f(x));
LineTo(hdc, x2, y2);
x1 = x2, y1 = y2;
}
SelectObject(hdc, pen0);
HBRUSH br = GetStockObject(BLACK_BRUSH);
HBRUSH br0 = SelectObject(hdc, br);
for (int i = 0; i < N; i++) {
x1 = X(x[i]), y1 = Y(y[i]);
Ellipse(hdc, x1-2, y1-2, x1+2, y1+2);
}
SelectObject(hdc, br0);
char buf[256];
sprintf(buf, "y = %.2f %+.2f x %+.2f x^2 %+.2f x^3",
a[0][M+1], a[1][M+1], a[2][M+1], a[3][M+1]);
TextOut(hdc, 10, 10, buf, strlen(buf));
DeleteObject(pen);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_PAINT: onPaint(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cl, int cs)
{
WNDCLASS wc = { 0, WindowProc, 0, 0, h, NULL,
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "wc" };
if (!RegisterClass(&wc)) return FALSE;
HWND hwnd = CreateWindowEx(WS_EX_COMPOSITED, "wc", cl,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
80, 60, 640, 480, NULL, NULL, h, NULL);
if (!hwnd) return 0;
calc();
ShowWindow(hwnd, cs);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
[quote=A.M post_id=152136 time=1543025975]
係数を求めるプログラムと3次関数を求めるプログラムは同じということなのでしょうか?
[/quote]
同じじゃないんですか?
[code]
for (i = 0; i <= M; i++)
printf("p%d = %7.3lf\n", i, a[i][M + 1]);
[/code]
これを次のように書き換えれば同じでしょう。
[code]
printf("y = %.2f %+.2f x %+.2f x^2 %+.2f x^3",
a[0][M+1], a[1][M+1], a[2][M+1], a[3][M+1]);
[/code]
[quote=A.M post_id=152136 time=1543025975]
また、「DxLib を使って図を書くプログラム」の方なのですが、学校では「Visual Studio 用開発者コマンド」を使っており、DxLibのやり方が申し訳ないのですがよくわかりません...。
[/quote]
Windows API を使って書き直してみました。
コンパイルできますか?
[code]
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 8
#define M 3
#define EPS 0.0001
double a[M + 1][M + 2];
double x[N] = { -4.0, -2.0, -1.0, 0.0, 1.0, 3.0, 4.0, 6.0 };
double y[N] = { -35.1, 15.1, 15.9, 8.9, 0.1, 0.1, 21.1, 135.0 };
double f(double x)
{
double y = 0;
for (int i = 3; i >= 0; i--) y = y * x + a[i][M+1];
return y;
}
int Jordan()
{
for (int i = 0; i <= M; i++) {
double pivot = a[i][i];
if (fabs(pivot) < EPS) {
//printf("ピボットが許容範囲誤差以下\n");
return 1;
}
for (int j = 0; j <= M + 1; j++)
a[i][j] /= pivot;
for (int k = 0; k <= M; k++) {
double delta = a[k][i];
for (int j = 0; j <= M + 1; j++)
if (k != i) a[k][j] -= delta * a[i][j];
}
}
return 0;
}
int calc()
{
for (int i = 0; i <= M; i++)
for (int j = 0; j <= M + 1; j++) {
a[j][i] = 0;
for (int k = 0; k < N; k++)
a[j][i] += pow(x[k], i + j);
}
for (int j = 0; j <= M; j++)
for (int k = 0; k < N; k++)
a[j][M + 1] += y[k] * pow(x[k], j);
return Jordan();
}
#define X(x) ((int)(O + (x - xa) * W / w))
#define Y(y) ((int)(O + H - (y - ya) * H / h))
void onPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd, &rect);
double O = 40;
double W = rect.right - rect.left - 2 * O;
double H = rect.bottom - rect.top - 2 * O;
double dx = 0.2;
double xa = -5, xb = 7, ya = f(xa), yb = f(xb);
double w = xb - xa, h = yb - ya;
int n = (int)(w / dx);
int x1 = X(0), y1 = Y(0);
MoveToEx(hdc, x1, O, NULL), LineTo(hdc, x1, O + H);
MoveToEx(hdc, O, y1, NULL), LineTo(hdc, O + W, y1);
HPEN pen = CreatePen(PS_SOLID, 1, RGB(192, 0, 0));
HPEN pen0 = SelectObject(hdc, pen);
x1 = X(xa), y1 = Y(f(xa));
MoveToEx(hdc, x1, y1, NULL);
for (int i = 1; i <= n; i++) {
double x = xa + i * dx;
int x2 = X(x), y2 = Y(f(x));
LineTo(hdc, x2, y2);
x1 = x2, y1 = y2;
}
SelectObject(hdc, pen0);
HBRUSH br = GetStockObject(BLACK_BRUSH);
HBRUSH br0 = SelectObject(hdc, br);
for (int i = 0; i < N; i++) {
x1 = X(x[i]), y1 = Y(y[i]);
Ellipse(hdc, x1-2, y1-2, x1+2, y1+2);
}
SelectObject(hdc, br0);
char buf[256];
sprintf(buf, "y = %.2f %+.2f x %+.2f x^2 %+.2f x^3",
a[0][M+1], a[1][M+1], a[2][M+1], a[3][M+1]);
TextOut(hdc, 10, 10, buf, strlen(buf));
DeleteObject(pen);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_PAINT: onPaint(hwnd); break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE h, HINSTANCE p, LPSTR cl, int cs)
{
WNDCLASS wc = { 0, WindowProc, 0, 0, h, NULL,
LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "wc" };
if (!RegisterClass(&wc)) return FALSE;
HWND hwnd = CreateWindowEx(WS_EX_COMPOSITED, "wc", cl,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
80, 60, 640, 480, NULL, NULL, h, NULL);
if (!hwnd) return 0;
calc();
ShowWindow(hwnd, cs);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
[/code]