Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <Windows.h>
- #include <locale.h>
- #include <iostream>
- #include <vector>
- #include <conio.h>
- using namespace std;
- HWND hWnd;
- HANDLE HandleCons;
- HDC dc;
- HPEN hPen;
- HBRUSH hBrush;
- vector<double> dots;
- int iter;
- double equation(double x )
- {
- double c=(2*x*x*x-11*x*x-17*x+115);
- return c;
- }
- void Oxy(int x, int y, double n)
- {
- ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
- ::LineTo(dc, x/2-(dots[dots.size()-1]*10*n), y);//1
- ::MoveToEx(dc, 0, y/2, NULL);
- ::LineTo(dc, x, y/2);//2
- ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
- ::LineTo(dc, (x/2)-6-(dots[dots.size()-1]*10*n), 10);//3
- ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
- ::LineTo(dc, (x/2)+6-(dots[dots.size()-1]*10*n), 10);//4
- ::MoveToEx(dc, x, y/2, NULL);
- ::LineTo(dc, x-10, (y/2)-6);//5
- ::MoveToEx(dc, x, y/2, NULL);
- ::LineTo(dc, x-10, (y/2)+6);//6
- }
- void Put_nib(int r, int g, int b, int wid)
- {
- hPen=CreatePen(PS_INSIDEFRAME, wid, RGB(r,g,b));
- SelectObject(dc, hPen);
- }
- void Set_brush(int r,int g,int b)
- {
- if(r==-1)
- hBrush = ::CreateSolidBrush(HOLLOW_BRUSH);
- else
- hBrush = ::CreateSolidBrush(RGB(r,g,b));
- ::SelectObject(dc, hBrush);
- }
- void clear(int width, int height)
- {
- POINT mas[4];
- Set_brush(0,0,0);
- mas[0].x=0;
- mas[0].y=0;
- mas[1].x=width;
- mas[1].y=0;
- mas[2].x=width;
- mas[2].y=height;
- mas[3].x=0;
- mas[3].y=height;
- Polygon(dc, mas, 4);
- }
- void Chart(double x1, double x2)
- {
- RECT rect;
- int height,width;
- ::SetConsoleTitle("Graphics1");
- ::system("mode con cols=95 lines=50");//75*35
- HandleCons = ::GetStdHandle(STD_OUTPUT_HANDLE);
- hWnd = ::FindWindow(NULL, "Graphics1");
- dc=GetDC(hWnd);
- ::GetClientRect(hWnd, &rect);
- height = rect.bottom;
- width = rect.right;
- Put_nib(255, 0, 0, 1);
- double x, n=1;
- int xc, yc, smotr=0;
- bool kg=0;
- SetTextColor(dc, RGB(255, 255, 255));
- SetBkColor(dc, RGB(0, 0, 0));
- while(kg!=1)
- {
- x=-(width/2);
- TextOutA(dc, width/2+3-(dots[dots.size()-1]*10*n), height/2+3, "0", 1);
- TextOutA(dc, (dots[1]*10*n)+(width/2)-(dots[dots.size()-1]*10*n), height/2+3, "2", 1);
- TextOutA(dc, width/2+(dots[0]*10*n)-(dots[dots.size()-1]*10*n), (height/2)+3, "4", 1);
- TextOutA(dc, width-10, (height/2)+3, "x", 1);
- TextOutA(dc, (width/2)+3-(dots[dots.size()-1]*10*n), 2, "y", 1);
- Put_nib(60, 60, 60, 1);
- Oxy(width, height, n);
- xc=0;
- Put_nib(255, 160, 0, 1);
- while(xc<width/2+(dots[dots.size()-1]*10*n))
- {
- xc=x*10*n;
- yc=equation(x)*n;
- ::MoveToEx(dc, xc+(width/2)-(dots[dots.size()-1]*10*n), (height/2)-yc, NULL);
- x=x+0.01;
- xc=x*10*n;
- yc=equation(x)*n;
- ::LineTo(dc, xc+(width/2)-(dots[dots.size()-1]*10*n), (height/2)-yc);//1
- }
- Put_nib(0, 160, 160, 1);
- for(int i=0;i<dots.size();i++)
- {
- ::MoveToEx(dc, ((dots[i]*10*n)+(width/2)-(dots[dots.size()-1]*10*n)), (height/2)-(equation(dots[i]))*n, NULL);
- ::LineTo(dc, ((dots[i]*10*n)+(width/2)-(dots[dots.size()-1]*10*n)), (height/2)/*-(equation(dots[i]))*n-60*/);
- }
- cout<<"Нажмите ENTER для выхода,"<<endl<<"W для увеличения графика,"<<endl<<"S для его уменьшения:";
- bool istina=0;
- while(istina!=1)
- {
- smotr=_getch();
- system("cls");
- if(smotr==13)
- {
- kg=1;
- istina=1;
- }
- else if(smotr==87)
- {
- n=n*1.4;
- istina=1;
- clear(width, height);
- }
- else if(smotr==83)
- {
- n=n/1.4;
- istina=1;
- clear(width, height);
- }
- else cout<<"Нажмите ENTER для выхода,"<<endl<<"W для увеличения графика,"<<endl<<"S для его уменьшения:";
- }
- }
- ::ReleaseDC(hWnd,dc);
- ::DeleteDC(dc);
- }
- int main(){
- double e,c,rez;
- int met;
- double a=2,b=4;
- setlocale(LC_CTYPE, "rus");
- cout<<"Введите номер метода нахождения корня:"<<endl;
- cout << "По точности - Нажмите 1"<<endl;
- cout << "По количеству итераций - Нажмите 2"<<endl;
- cin>>met;
- if(equation(a)>equation(b))
- {
- double x=a;
- a=b;
- b=x;
- }
- if(met==1){
- cout<<"Введите значение погрешности:"<<endl;
- cin>>e;
- dots.push_back(a);
- dots.push_back(b);
- int i=0;
- while(abs((a-b))>e)
- {
- rez=equation((a+b)/2);
- if (rez>0) {
- b=(a+b)/2;
- dots.push_back(b);
- }
- else if(equation((a+b)/2)<0)
- {
- a=(a+b)/2;
- dots.push_back(a);
- }
- i+=1;
- cout<<i<<")"<<"Корень уравнения: "<<((a+b)/2)<<" "<<"Погрешность: "<<e<<endl;
- }
- system("pause");
- }
- if(met==2){
- cout<<"Введите номер итерации:"<<endl;
- cin>>iter;
- dots.push_back(a);
- dots.push_back(b);
- for(int i=1;i<=iter;i++)
- {
- rez=equation((a+b)/2);
- if (rez>0) {
- b=(a+b)/2;
- dots.push_back(b);
- }
- else if(equation((a+b)/2)<0)
- {
- a=(a+b)/2;
- dots.push_back(a);
- }
- cout<<i<<")"<<"Корень уравнения:"<<((a+b)/2)<<endl;
- }
- system("pause");
- }
- Chart(a,b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement