Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kursova_OKS-44.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
387 Кб
Скачать
☆

Додаток а Текст програми

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#include <math.h>

#include "resource.h"

#include "conert.h"

//Global:

HINSTANCE hInst;

//End Global

//Classes

struct Page

{

HDC hdc,hMemDC;

HWND hwndP;

HBITMAP hBitmap;

RECT rc;

void Init(HWND hwndP,bool clear);

HWND GetHwnd();//Визначення вікна

void Show(HWND hwndP);

void Delete();

};

void Page::Init(HWND hwndP,bool clear)//Створення сторінки з/без очистки сторінки

{

Page::hwndP=hwndP;

hdc=GetDC(hwndP);

GetClientRect(hwndP,&rc);

hBitmap=CreateCompatibleBitmap(hdc,rc.right,rc.bottom);

hMemDC=CreateCompatibleDC(hdc);

SelectObject(hMemDC,hBitmap);

if (!clear)

BitBlt(hMemDC,0,0,rc.right,rc.bottom,hdc,0,0,SRCCOPY);

else

Rectangle(hMemDC,0,0,rc.right,rc.bottom);

}

void Page::Show(HWND hwndP)//Відображення сторінки

{

HDC hdc1=GetDC(hwndP);

RECT rc1; GetClientRect(hwndP,&rc1);

BitBlt(hdc1,0,0,rc1.right,rc1.bottom,hMemDC,0,0,SRCCOPY);

DeleteDC(hdc1);

}

void Page::Delete()//Знищення сторінки

{

DeleteDC(hdc); DeleteDC(hMemDC); DeleteObject(hBitmap);

}

//////////////////////////////////////////////////////////

//End classes

void Line(HDC hdc,int x1,int y1,int x2,int y2)

{

MoveToEx(hdc,x1,y1,NULL);

LineTo(hdc,x2,y2);

}

void DrawTab(Page page,bool clear,int x1,int x2,int y1,int y2)

{

if (clear)

Rectangle(page.hMemDC,0,0,page.rc.right,page.rc.bottom);

int x_center=(int)(page.rc.right-page.rc.left)/2;

int y_center=(int)(page.rc.bottom-page.rc.top)/2;

HPEN hPen=CreatePen(1,1,0);

HPEN hOldPen=(HPEN)SelectObject(page.hMemDC,(HGDIOBJ)hPen);

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

for (int x=0; x<=x2-x1; x++)

{

if (x==-x1)

{

hPen=CreatePen(1,2,RGB(255,0,0));

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

Line(page.hMemDC,(int)(2*x_center*x/(x2-x1)),0,(int)(2*x_center*x/(x2-x1)),page.rc.bottom);

hPen=CreatePen(1,1,0);

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

}

else

{

Line(page.hMemDC,(int)(2*x_center*x/(x2-x1)),0,(int)(2*x_center*x/(x2-x1)),page.rc.bottom);

}

}

for (int y=0; y<=y2-y1; y++)

{

if (y==-y1)

{

hPen=CreatePen(1,2,RGB(255,0,0));

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

Line(page.hMemDC,0,2*y_center-(int)(2*y_center*y/(y2-y1)),page.rc.right,2*y_center-(int)(2*y_center*y/(y2-y1)));

hPen=CreatePen(1,1,0);

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

}

else

{

Line(page.hMemDC,0,2*y_center-(int)(2*y_center*y/(y2-y1)),page.rc.right,2*y_center-(int)(2*y_center*y/(y2-y1)));

}

}

DeleteObject((HGDIOBJ)hPen);

SelectObject(page.hMemDC,hOldPen);

DeleteObject((HGDIOBJ)hOldPen);

}

Page page;

String *built=NULL;

int num=0,xs=-10,xe=10,ys=-10,ye=10;

struct Graphic

{

bool show;

bool built_once;

Page look;

void Draw(Page page,bool once,int num,int type,int x1,int x2,int y1,int y2)

{

String vir=built[num];

int x_center=Round((page.rc.right-page.rc.left)/2);

int y_center=Round((page.rc.bottom-page.rc.top)/2);

long int color=((int)RGB(255,255,255)/2+rand())%(RGB(255,255,255));

HPEN hPen=CreatePen(1,1,0);

HPEN hOldPen=(HPEN)SelectObject(page.hMemDC,(HGDIOBJ)hPen);

hPen=CreatePen(1,2,color);

SelectObject(page.hMemDC,(HGDIOBJ)hPen);

look.Init(page.hwndP,true);

SelectObject(look.hMemDC,(HGDIOBJ)hPen);

double x_last,y_last;

bool last_error=true;

for (double i=0; i<=page.rc.right; i++)

{

long double x=i*(x2-x1)/page.rc.right+x1;

sprintf(::x.string,"%f",x);

double y=Change(vir);

if (type==0)

{

if (last_error)

{

MoveToEx(page.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(y-y1)/(y2-y1)),NULL);

MoveToEx(look.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(y-y1)/(y2-y1)),NULL);

}

else

{

if (!def_error)

{

LineTo(page.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(y-y1)/(y2-y1)));

LineTo(look.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(y-y1)/(y2-y1)));

}

}

}

if (type==1)

{

if (last_error)

{

double yp=(y-y_last)/(x-x_last);

MoveToEx(page.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(yp-y1)/(y2-y1)),NULL);

MoveToEx(look.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(yp-y1)/(y2-y1)),NULL);

}

else

{

if (!def_error)

{

double yp=(y-y_last)/(x-x_last);

LineTo(page.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(yp-y1)/(y2-y1)));

LineTo(look.hMemDC,Round(i),Round(page.rc.bottom-page.rc.bottom*(yp-y1)/(y2-y1)));

}

}

x_last=x; y_last=y;

}

if (!once)

page.Show(page.hwndP);

last_error=def_error;

def_error=false;

}

if (strlen(error.string)>0)

MessageBox(0,error.string,"Помилка",0);

DeleteObject((HGDIOBJ)hPen);

SelectObject(page.hMemDC,hOldPen);

DeleteObject((HGDIOBJ)hOldPen);

}

};

Graphic *gr=NULL;

Graphic *pr=NULL;

void DrawAll(Page page)

{

for (int i=0; i<num; i++)

{

if (gr[i].show && gr[i].built_once)

BitBlt(page.hMemDC,0,0,page.rc.right,page.rc.bottom,gr[i].look.hMemDC,0,0,SRCAND);

if (gr[i].show && !gr[i].built_once)

gr[i].Draw(page,true,i,0,xs,xe,ys,ye);

if (pr[i].show && pr[i].built_once)

BitBlt(page.hMemDC,0,0,page.rc.right,page.rc.bottom,pr[i].look.hMemDC,0,0,SRCAND);

if (pr[i].show && !pr[i].built_once)

pr[i].Draw(page,true,i,0,xs,xe,ys,ye);

}

}

void AddMessage(HWND hWnd,int lb_list,char *message,int num)

{

static int i;

if (num==1) { i=0; num=0; }

if (num==0)

{

SendDlgItemMessage(hWnd,lb_list,LB_ADDSTRING,0,(LPARAM)message);

i++;

SendDlgItemMessage(hWnd,lb_list,LB_SETCURSEL,i-1,0);

if (gr[i-1].show==true)

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Вилучити графік");

else

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Побудувати график");

if (pr[i-1].show==true)

SetDlgItemText(hWnd,DRAW_PROIZV,"Вилучити графік похідною");

else

SetDlgItemText(hWnd,DRAW_PROIZV,"Побудувати графік похідною");

}

if (num==2)

{

SendDlgItemMessage(hWnd,lb_list,LB_RESETCONTENT,0,0);

i=0;

}

if (num==3)

{

SendDlgItemMessage(hWnd,lb_list,LB_ADDSTRING,0,(LPARAM)message);

SendDlgItemMessage(hWnd,lb_list,LB_SETCURSEL,i-1,0);

}

}

LRESULT CALLBACK DlgProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

int wmId = LOWORD(wParam),wmEvent = HIWORD(wParam);

int x=LOWORD(lParam),y=HIWORD(lParam);

switch (iMsg)

{

case WM_INITDIALOG:

{

strcpy(name.string,"x");

page.Init(GetDlgItem(hWnd,IDC_PICTURE),true);

SetDlgItemText(hWnd,IDC_X1,"-10");

SetDlgItemText(hWnd,IDC_X2,"10");

SetDlgItemText(hWnd,IDC_Y1,"-10");

SetDlgItemText(hWnd,IDC_Y2,"10");

SetDlgItemText(hWnd,IDC_X,"0");

SetDlgItemText(hWnd,IDC_F,"0");

DrawTab(page,false,xs,xe,ys,ye);

page.Show(page.hwndP);

return 0;

}

case WM_COMMAND:

{

switch(wmId)

{

case IDC_HISTORY:

{

static int last_pos=-1;

int pos=SendDlgItemMessage(hWnd,IDC_HISTORY,LB_GETCURSEL,0,0);

if (pos!=last_pos)

{

if (gr[pos].show==true)

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Вилучити графік");

else

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Побудувати график");

if (pr[pos].show==true)

SetDlgItemText(hWnd,IDC_DRAW_PROIZV,"Вилучити графік похідною");

else

SetDlgItemText(hWnd,IDC_DRAW_PROIZV,"Побудувати графік похідною");

last_pos=pos;

}

}

break;

case IDC_ONLY_ADD:

{

String vir;

GetDlgItemText(hWnd,IDC_F_X,vir.string,1024);

if (strlen(vir.string)>0)

{

bool match=false;

for (int i=0;i<num;i++)

{

if (strcmp(vir.string,built[i].string)==1)

match=true;

}

if (match)

MessageBox(hWnd,"Функція вже добавлена",0,0);

if (match==false)

{

num++;

String *a;

a=(String*)malloc(sizeof(String)*(num-1));

for (int i=0; i<num-1; i++)

a[i]=built[i];

free((void*)built);

built=(String*)malloc(sizeof(String)*num);

for (int i=0; i<num-1; i++)

built[i]=a[i];

built[num-1]=vir;

free((void*)a);

Graphic *g=(Graphic*)malloc(sizeof(Graphic)*(num-1));

for (int i=0; i<num-1; i++)

g[i]=gr[i];

free((void*)gr);

gr=(Graphic*)malloc(sizeof(Graphic)*num);

for (int i=0; i<num-1; i++)

gr[i]=g[i];

gr[num-1].built_once=false;

gr[num-1].look.Init(page.hwndP,true);

gr[num-1].show=false;

free((void*)g);

g=(Graphic*)malloc(sizeof(Graphic)*(num-1));

for (int i=0; i<num-1; i++)

g[i]=pr[i];

free((void*)pr);

pr=(Graphic*)malloc(sizeof(Graphic)*num);

for (int i=0; i<num-1; i++)

pr[i]=g[i];

pr[num-1].built_once=false;

pr[num-1].look.Init(page.hwndP,true);

pr[num-1].show=false;

free((void*)g);

if (strlen(error.string)==0)

{

AddMessage(hWnd,IDC_HISTORY,vir.string,0);

}

}

}

}

break;

case IDC_ADD_SHOW:

{

String vir;

GetDlgItemText(hWnd,IDC_F_X,vir.string,1024);

if (strlen(vir.string)>0)

{

bool match=false;

for (int i=0;i<num;i++)

{

if (strcmp(vir.string,built[i].string)==1)

match=true;

}

if (match)

MessageBox(hWnd,"Функція вже добавлена",0,0);

if (match==false)

{

num++;

String *a;

a=(String*)malloc(sizeof(String)*(num-1));

for (int i=0; i<num-1; i++)

a[i]=built[i];

free((void*)built);

built=(String*)malloc(sizeof(String)*num);

for (int i=0; i<num-1; i++)

built[i]=a[i];

built[num-1]=vir;

free((void*)a);

Graphic *g=(Graphic*)malloc(sizeof(Graphic)*(num-1));

for (int i=0; i<num-1; i++)

g[i]=gr[i];

free((void*)gr);

gr=(Graphic*)malloc(sizeof(Graphic)*num);

for (int i=0; i<num-1; i++)

gr[i]=g[i];

gr[num-1].built_once=true;

gr[num-1].look.Init(page.hwndP,true);

gr[num-1].show=true;

free((void*)g);

g=(Graphic*)malloc(sizeof(Graphic)*(num-1));

for (int i=0; i<num-1; i++)

g[i]=pr[i];

free((void*)pr);

pr=(Graphic*)malloc(sizeof(Graphic)*num);

for (int i=0; i<num-1; i++)

pr[i]=g[i];

pr[num-1].built_once=false;

pr[num-1].look.Init(page.hwndP,true);

pr[num-1].show=false;

free((void*)g);

if (strlen(error.string)==0)

AddMessage(hWnd,IDC_HISTORY,vir.string,0);

gr[num-1].Draw(page,false,num-1,0,xs,xe,ys,ye);

}

}

}

break;

case IDC_DRAW_PROIZV:

{

int pos=SendDlgItemMessage(hWnd,IDC_HISTORY,LB_GETCURSEL,0,0);

if (pos>=0 && pos<num)

{

if (pr[pos].show)

{

pr[pos].show=false;

DrawTab(page,true,xs,xe,ys,ye);

DrawAll(page);

page.Show(page.hwndP);

}

else

{

pr[pos].show=true;

if (pr[pos].built_once)

{

BitBlt(page.hMemDC,0,0,page.rc.right,page.rc.bottom,pr[pos].look.hMemDC,0,0,SRCAND);

page.Show(page.hwndP);

}

else

{

pr[pos].Draw(page,false,pos,1,xs,xe,ys,ye);

pr[pos].built_once=true;

}

}

if (pr[pos].show==true)

SetDlgItemText(hWnd,IDC_DRAW_PROIZV,"Вилучити графік похідною");

else

SetDlgItemText(hWnd,DRAW_PROIZV,"Побудувати графік похідною");

}

else

MessageBox(hWnd,"Виберіть функцію",0,0);

}

break;

case IDC_TABL:

{

char buf[10];

GetDlgItemText(hWnd,IDC_X1,buf,10);

xs=atoi(buf);

GetDlgItemText(hWnd,IDC_X2,buf,10);

xe=atoi(buf);

GetDlgItemText(hWnd,IDC_Y1,buf,10);

ys=atoi(buf);

GetDlgItemText(hWnd,IDC_Y2,buf,10);

ye=atoi(buf);

if (xs>=xe || ys>=ye)

{

xs=ys=-10; xe=ye=10;

MessageBox(hWnd,"Неправильні розміри таблиці",0,0);

}

else

{

DrawTab(page,true,xs,xe,ys,ye);

for (int i=0; i<num; i++)

if (gr[i].show)

gr[i].Draw(page,true,i,0,xs,xe,ys,ye);

}

itoa(xs,buf,10);

SetDlgItemText(hWnd,IDC_X1,buf);

itoa(xe,buf,10);

SetDlgItemText(hWnd,IDC_X2,buf);

itoa(ys,buf,10);

SetDlgItemText(hWnd,IDC_Y1,buf);

itoa(ye,buf,10);

SetDlgItemText(hWnd,IDC_Y2,buf);

page.Show(page.hwndP);

}

break;

case IDC_CALCULATE:

{

int pos=SendDlgItemMessage(hWnd,IDC_HISTORY,LB_GETCURSEL,0,0);

if (pos>=0 && num>=pos)

{

char buf[128];

GetDlgItemText(hWnd,IDC_X,buf,10);

strcpy(::x.string,buf);

sprintf(buf,"%f",Change(built[pos]));

if (!def_error)

SetDlgItemText(hWnd,IDC_F,buf);

else

SetDlgItemText(hWnd,IDC_F,"Помилка");

def_error=false;

}

else

MessageBox(hWnd,"Виберітьфункцію",0,0);

}

break;

case IDC_DELETE_FUNCTION:

{

int pos=SendDlgItemMessage(hWnd,IDC_HISTORY,LB_GETCURSEL,0,0);

if (pos>=0 && num>=pos)

{

bool s=gr[pos].show;

AddMessage(hWnd,IDC_HISTORY,NULL,2);

for (int i=pos; i<num-1; i++)

{

built[i]=built[i+1];

gr[i]=gr[i+1];

pr[i]=pr[i+1];

}

num--;

if (s)

DrawTab(page,true,xs,xe,ys,ye);

for (int i=0; i<num; i++)

AddMessage(hWnd,IDC_HISTORY,built[i].string,1);

DrawAll(page);

page.Show(page.hwndP);

}

else

MessageBox(hWnd,"Виберіть функцію",0,0);

}

break;

case IDC_DELETE_GRAPHIC:

{

int pos=SendDlgItemMessage(hWnd,IDC_HISTORY,LB_GETCURSEL,0,0);

if (pos>=0 && pos<num)

{

if (gr[pos].show)

{

gr[pos].show=false;

DrawTab(page,true,xs,xe,ys,ye);

DrawAll(page);

page.Show(page.hwndP);

}

else

{

gr[pos].show=true;

if (gr[pos].built_once)

{

BitBlt(page.hMemDC,0,0,page.rc.right,page.rc.bottom,gr[pos].look.hMemDC,0,0,SRCAND);

page.Show(page.hwndP);

}

else

{

gr[pos].Draw(page,false,pos,0,xs,xe,ys,ye);

gr[pos].built_once=true;

}

}

if (gr[pos].show==true)

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Вилучити графік");

else

SetDlgItemText(hWnd,IDC_DELETE_GRAPHIC,"Побудувати графік");

}

else

MessageBox(hWnd,"Виберіть функцію",0,0);

}

break;

}

return 0;

}

case WM_PAINT:

{

page.Show(page.hwndP);

return 0;

}

case WM_CLOSE:

{

page.Delete();

EndDialog( hWnd, FALSE);

return 0;

}

}

return FALSE;

}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)

{

Init();

int ret=DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAIN),NULL,(DLGPROC)DlgProc);

return ret;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]