Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <Windows.h>
  4. #include <locale.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include <conio.h>
  8.  
  9. using namespace std;
  10.  
  11. HWND hWnd;
  12. HANDLE HandleCons;
  13. HDC dc;
  14. HPEN hPen;
  15. HBRUSH hBrush;
  16.  
  17. vector<double> dots;
  18. int iter;
  19.  
  20. double equation(double x )
  21. {
  22. double c=(2*x*x*x-11*x*x-17*x+115);
  23. return c;
  24. }
  25.  
  26.  
  27. void Oxy(int x, int y, double n)
  28. {
  29. ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
  30. ::LineTo(dc, x/2-(dots[dots.size()-1]*10*n), y);//1
  31. ::MoveToEx(dc, 0, y/2, NULL);
  32. ::LineTo(dc, x, y/2);//2
  33. ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
  34. ::LineTo(dc, (x/2)-6-(dots[dots.size()-1]*10*n), 10);//3
  35. ::MoveToEx(dc, x/2-(dots[dots.size()-1]*10*n), 0, NULL);
  36. ::LineTo(dc, (x/2)+6-(dots[dots.size()-1]*10*n), 10);//4
  37. ::MoveToEx(dc, x, y/2, NULL);
  38. ::LineTo(dc, x-10, (y/2)-6);//5
  39. ::MoveToEx(dc, x, y/2, NULL);
  40. ::LineTo(dc, x-10, (y/2)+6);//6
  41. }
  42.  
  43. void Put_nib(int r, int g, int b, int wid)
  44. {
  45. hPen=CreatePen(PS_INSIDEFRAME, wid, RGB(r,g,b));
  46. SelectObject(dc, hPen);
  47. }
  48.  
  49. void Set_brush(int r,int g,int b)
  50. {
  51. if(r==-1)
  52. hBrush = ::CreateSolidBrush(HOLLOW_BRUSH);
  53. else
  54. hBrush = ::CreateSolidBrush(RGB(r,g,b));
  55. ::SelectObject(dc, hBrush);
  56. }
  57.  
  58. void clear(int width, int height)
  59. {
  60. POINT mas[4];
  61. Set_brush(0,0,0);
  62. mas[0].x=0;
  63. mas[0].y=0;
  64. mas[1].x=width;
  65. mas[1].y=0;
  66. mas[2].x=width;
  67. mas[2].y=height;
  68. mas[3].x=0;
  69. mas[3].y=height;
  70. Polygon(dc, mas, 4);
  71. }
  72.  
  73. void Chart(double x1, double x2)
  74. {
  75. RECT rect;
  76. int height,width;
  77. ::SetConsoleTitle("Graphics1");
  78. ::system("mode con cols=95 lines=50");//75*35
  79. HandleCons = ::GetStdHandle(STD_OUTPUT_HANDLE);
  80. hWnd = ::FindWindow(NULL, "Graphics1");
  81. dc=GetDC(hWnd);
  82. ::GetClientRect(hWnd, &rect);
  83. height = rect.bottom;
  84. width = rect.right;
  85. Put_nib(255, 0, 0, 1);
  86. double x, n=1;
  87. int xc, yc, smotr=0;
  88. bool kg=0;
  89. SetTextColor(dc, RGB(255, 255, 255));
  90. SetBkColor(dc, RGB(0, 0, 0));
  91. while(kg!=1)
  92. {
  93. x=-(width/2);
  94. TextOutA(dc, width/2+3-(dots[dots.size()-1]*10*n), height/2+3, "0", 1);
  95. TextOutA(dc, (dots[1]*10*n)+(width/2)-(dots[dots.size()-1]*10*n), height/2+3, "2", 1);
  96. TextOutA(dc, width/2+(dots[0]*10*n)-(dots[dots.size()-1]*10*n), (height/2)+3, "4", 1);
  97. TextOutA(dc, width-10, (height/2)+3, "x", 1);
  98. TextOutA(dc, (width/2)+3-(dots[dots.size()-1]*10*n), 2, "y", 1);
  99. Put_nib(60, 60, 60, 1);
  100. Oxy(width, height, n);
  101. xc=0;
  102. Put_nib(255, 160, 0, 1);
  103. while(xc<width/2+(dots[dots.size()-1]*10*n))
  104. {
  105. xc=x*10*n;
  106. yc=equation(x)*n;
  107. ::MoveToEx(dc, xc+(width/2)-(dots[dots.size()-1]*10*n), (height/2)-yc, NULL);
  108. x=x+0.01;
  109. xc=x*10*n;
  110. yc=equation(x)*n;
  111. ::LineTo(dc, xc+(width/2)-(dots[dots.size()-1]*10*n), (height/2)-yc);//1
  112. }
  113. Put_nib(0, 160, 160, 1);
  114. for(int i=0;i<dots.size();i++)
  115. {
  116. ::MoveToEx(dc, ((dots[i]*10*n)+(width/2)-(dots[dots.size()-1]*10*n)), (height/2)-(equation(dots[i]))*n, NULL);
  117. ::LineTo(dc, ((dots[i]*10*n)+(width/2)-(dots[dots.size()-1]*10*n)), (height/2)/*-(equation(dots[i]))*n-60*/);
  118. }
  119. cout<<"Нажмите ENTER для выхода,"<<endl<<"W для увеличения графика,"<<endl<<"S для его уменьшения:";
  120. bool istina=0;
  121. while(istina!=1)
  122. {
  123. smotr=_getch();
  124. system("cls");
  125. if(smotr==13)
  126. {
  127. kg=1;
  128. istina=1;
  129. }
  130. else if(smotr==87)
  131. {
  132. n=n*1.4;
  133. istina=1;
  134. clear(width, height);
  135. }
  136. else if(smotr==83)
  137. {
  138. n=n/1.4;
  139. istina=1;
  140. clear(width, height);
  141. }
  142. else cout<<"Нажмите ENTER для выхода,"<<endl<<"W для увеличения графика,"<<endl<<"S для его уменьшения:";
  143. }
  144. }
  145. ::ReleaseDC(hWnd,dc);
  146. ::DeleteDC(dc);
  147. }
  148.  
  149. int main(){
  150.  
  151. double e,c,rez;
  152. int met;
  153. double a=2,b=4;
  154.  
  155. setlocale(LC_CTYPE, "rus");
  156. cout<<"Введите номер метода нахождения корня:"<<endl;
  157. cout << "По точности - Нажмите 1"<<endl;
  158. cout << "По количеству итераций - Нажмите 2"<<endl;
  159.  
  160. cin>>met;
  161.  
  162. if(equation(a)>equation(b))
  163. {
  164. double x=a;
  165. a=b;
  166. b=x;
  167. }
  168.  
  169. if(met==1){
  170. cout<<"Введите значение погрешности:"<<endl;
  171. cin>>e;
  172. dots.push_back(a);
  173. dots.push_back(b);
  174. int i=0;
  175. while(abs((a-b))>e)
  176. {
  177. rez=equation((a+b)/2);
  178. if (rez>0) {
  179. b=(a+b)/2;
  180. dots.push_back(b);
  181. }
  182. else if(equation((a+b)/2)<0)
  183. {
  184. a=(a+b)/2;
  185. dots.push_back(a);
  186. }
  187. i+=1;
  188. cout<<i<<")"<<"Корень уравнения: "<<((a+b)/2)<<" "<<"Погрешность: "<<e<<endl;
  189. }
  190. system("pause");
  191. }
  192.  
  193. if(met==2){
  194. cout<<"Введите номер итерации:"<<endl;
  195. cin>>iter;
  196.  
  197. dots.push_back(a);
  198. dots.push_back(b);
  199.  
  200. for(int i=1;i<=iter;i++)
  201. {
  202. rez=equation((a+b)/2);
  203. if (rez>0) {
  204. b=(a+b)/2;
  205. dots.push_back(b);
  206. }
  207. else if(equation((a+b)/2)<0)
  208. {
  209. a=(a+b)/2;
  210. dots.push_back(a);
  211. }
  212. cout<<i<<")"<<"Корень уравнения:"<<((a+b)/2)<<endl;
  213. }
  214. system("pause");
  215. }
  216. Chart(a,b);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement