Advertisement
Mirbek

Lab4

Apr 5th, 2022
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.25 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6.  
  7. float newparam(float anb) {
  8.  
  9.     char sta[20];
  10.     char cha;
  11.     int ia=0;
  12.     sta[ia]='\0';
  13.     setcolor(3);
  14.     rectangle(50, 320+anb, 150, 355+anb);
  15.     setcolor(0);
  16.  
  17.     do {
  18.         cha=getch();
  19.         if((cha>='0')&&(cha<='9')) {
  20.             sta[ia]+=cha;
  21.             sta[ia+1]='\0';
  22.             ia++;
  23.         }
  24.         switch(cha) {
  25.             case '.':
  26.             case '-':{
  27.                 sta[ia]+=cha;
  28.                 sta[ia+1]='\0';
  29.                 ia++;
  30.             }break;
  31.             case 8: {
  32.                 setfillstyle(1,15);
  33.                 bar(70,(331+anb)-5,70+strlen(sta)*8,(331+anb)+15);
  34.                 sta[strlen(sta)-1]='\0';
  35.                 ia--;
  36.             }break;
  37.         }
  38.         outtextxy(70,331+anb,sta);
  39.     }while(cha!=13);
  40.     setcolor(0);
  41.     rectangle(50, 320+anb, 150, 355+anb);
  42.  
  43.  
  44.     float ab=atof(sta);
  45.     //sprintf(st,a);рнгпdlya tablicy
  46.     printf("%f",ab);
  47.     return ab;
  48. }
  49.  
  50.  
  51. void tablenum(float a, float b)
  52. {
  53.     int i;
  54.     float x1, x2;
  55.     char num1[100], num2[100];
  56.     setcolor(0);
  57.     for(i=-5; i<=5; i++)
  58.     {
  59.         x1=a*sin(i)+b*cos(i);
  60.         sprintf(num1,"%.2f",x1);
  61.         if((x1>0)||(x1==0)) sprintf(num1," %.2f",x1);
  62.         outtextxy(135,170+18*i,num1);
  63.         x2=a*tan(i)+b;
  64.         sprintf(num2,"%.2f",x2);
  65.         if((x2>0)||(x2==0)) sprintf(num2," %.2f",x2);
  66.         outtextxy(220,170+18*i,num2);
  67.     }
  68. }
  69.  
  70.  
  71. float f1(float a, float b, float xa)
  72. {
  73.     float x;
  74.     x=a*sin(xa)+b*cos(xa);
  75.     return x;
  76. }
  77.  
  78.  
  79. float f2(float a, float b, float xa)
  80. {
  81.     float x;
  82.     x=a*tan(xa)+b;
  83.     return x;
  84. }
  85.  
  86. void grafic(float a,float b)
  87. {
  88.     float xa;
  89.     float xxa;
  90.     float dxa=1.0/260;
  91.     xa=-5;
  92.     xxa=0;
  93.     while(xa<=5)
  94.     {
  95.        if((50<(200-round(f1(a,b,xa))))&&(200-round(f1(a,b,xa))<360))
  96.         {putpixel(350+round(xxa),200-round(f1(a,b,xa)),2);}
  97.        if((50<(200-round(f2(a,b,xa))))&&(200-round(f2(a,b,xa))<360))
  98.         {putpixel(350+round(xxa),200-round(f2(a,b,xa)),4);}
  99.  
  100.         xxa=xxa+0.1;
  101.         xa=xa+dxa;
  102.         //tablenum(a, b);
  103.     }
  104.     tablenum(a, b);
  105. }
  106. void gr()
  107. {
  108.     setbkcolor(15);
  109.     floodfill(200,200,WHITE);
  110.     setcolor(0);
  111.  
  112.     rectangle(10, 30, 292, 280);
  113.     line(104, 30, 104, 280);
  114.     line(198, 30, 198, 280);
  115.     line(320, 0, 320, 400);
  116.     line(10, 70, 292, 70);
  117.     line(0, 400, 639, 400);
  118.     rectangle(50, 360, 150, 395);
  119.     rectangle(50, 320, 150, 355);
  120.  
  121.     setcolor(0);
  122.  
  123.     char a[] = "a=", b[] = "b=", y[] = "Y", x[] = "X", o[] = "0", y1[] = "Y1", y2[] = "Y2";
  124.  
  125.     outtextxy(55, 331, a);
  126.     outtextxy(55, 371, b);
  127.     outtextxy(465, 35, y);
  128.     outtextxy(605, 204, x);
  129.     outtextxy(50, 45, x);
  130.     outtextxy(145, 45, y1);
  131.     outtextxy(225, 45, y2);
  132.     outtextxy(470, 201, o);
  133.  
  134.     line(479, 40, 479, 360);//y
  135.     line(350, 200, 610, 200);//x
  136.     line(479, 40, 476, 43);
  137.     line(479, 40, 482, 43);
  138.     line(610, 200, 607, 197);
  139.     line(610, 200, 607, 203);
  140.  
  141.  
  142.     setcolor(0);
  143.     rectangle(170,320,315,390);
  144.     char *xt[12]={"-5","-4","-3","-2","-1","0","1","2","3","4","5","\0"};
  145.     for(int i=-5; i<=5; i++)
  146.         outtextxy(55,170+18*i,xt[i+5]);
  147.     for(float i=350.0; i<=605.0;i=i+26)
  148.         line(i,195,i,205);
  149.     for(float j=50.0; j<=360.0;j=j+50)
  150.         line(474,j,484,j);
  151.     outtextxy(345,206,"-5");
  152.     outtextxy(371,206,"-4");
  153.     outtextxy(397,206,"-3");
  154.     outtextxy(423,206,"-2");
  155.     outtextxy(449,206,"-1");
  156.     outtextxy(501,206,"1");
  157.     outtextxy(527,206,"2");
  158.     outtextxy(553,206,"3");
  159.     outtextxy(579,206,"4");
  160.     outtextxy(605,206,"5");//x
  161.     outtextxy(490,45,"150");
  162.     outtextxy(490,95,"100");
  163.     outtextxy(490,145,"50");
  164.     outtextxy(490,245,"-50");
  165.     outtextxy(490,295,"-100");
  166.     outtextxy(490,345,"-150");
  167.  
  168.     setcolor(2);
  169.     outtextxy(180,330,"Y1=a*sin(x)+b*cos(x)");
  170.     setcolor(4);
  171.     outtextxy(180,370,"Y2=a*tg(x)+b");
  172. }
  173.  
  174.  
  175. void k(int x1, int y1, int x2, int y2, char *s, int col)
  176. {
  177.     setcolor(col);
  178.     rectangle(x1,y1,x2,y2);
  179.     setcolor(0);
  180.     outtextxy(x1+10,(y2-y1)/3+y1, s);
  181. }
  182.  
  183. void knopkiupr()
  184. {
  185.     char ch;
  186.     int xr,yr;
  187.     int nk=1;
  188.     float a;
  189.     float b;
  190.     do {
  191.         getmouseclick(WM_LBUTTONDOWN,xr,yr);
  192.         if((xr>=470)&&(xr<=570)&&(yr>=420)&&(yr<=460)) {
  193.             ch='e';
  194.             k(50, 420,150,460, "NEW PARAM", 0);
  195.             k(270, 420, 370,460, "GRAFIC", 0);
  196.             k(470, 420, 570, 460, "EXIT", GREEN);}
  197.         if((xr>=50)&&(xr<=150)&&(yr>=420)&&(yr<=460)) {
  198.             k(50, 420,150,460, "NEW PARAM",GREEN );
  199.             k(470, 420, 570, 460, "EXIT", 0);
  200.             k(270, 420, 370,460, "GRAFIC", 0);
  201.             setcolor(0);
  202.             bar(70,325,145,350);
  203.             bar(70,365,145,390);
  204.             bar(110,75,190,280);
  205.             bar(200,75,280,280);
  206.             bar(350,40,610,360);
  207.             line(479, 40, 479, 360);//y
  208.             line(350, 200, 610, 200);//x
  209.             for(float i=350.0; i<=605.0;i=i+26)
  210.                 line(i,195,i,205);
  211.             for(float j=50.0; j<=360.0;j=j+50)
  212.                 line(474,j,484,j);
  213.             outtextxy(465, 35, "Y");
  214.             outtextxy(605, 204, "X");
  215.             outtextxy(470, 201, "0");
  216.  
  217.             line(479, 40, 476, 43);
  218.             line(479, 40, 482, 43);
  219.             line(610, 200, 607, 197);
  220.             line(610, 200, 607, 203);
  221.  
  222.             outtextxy(345,206,"-5");
  223.             outtextxy(371,206,"-4");
  224.             outtextxy(397,206,"-3");
  225.             outtextxy(423,206,"-2");
  226.             outtextxy(449,206,"-1");
  227.             outtextxy(501,206,"1");
  228.             outtextxy(527,206,"2");
  229.             outtextxy(553,206,"3");
  230.             outtextxy(579,206,"4");
  231.             outtextxy(605,206,"5");//x
  232.             outtextxy(490,45,"150");
  233.             outtextxy(490,95,"100");
  234.             outtextxy(490,145,"50");
  235.             outtextxy(490,245,"-50");
  236.             outtextxy(490,295,"-100");
  237.             outtextxy(490,345,"-150");
  238.  
  239.             a=newparam(0.0);
  240.             b=newparam(40.0);
  241.         }
  242.         if((xr>=270)&&(xr<=370)&&(yr>=420)&&(yr<=460)) {
  243.             grafic(a,b);
  244.             k(270, 420, 370,460, "GRAFIC",GREEN );
  245.             k(50, 420,150,460, "NEW PARAM", 0);
  246.             k(470, 420, 570, 460, "EXIT", 0);
  247.         }
  248.  
  249.         if(kbhit()) {
  250.             ch = getch();
  251.             switch (ch)
  252.             {
  253.             case 'n':{
  254.                 k(50, 420,150,460, "NEW PARAM",GREEN );
  255.                 k(470, 420, 570, 460, "EXIT", 0);
  256.                 k(270, 420, 370,460, "GRAFIC", 0);
  257.  
  258.                 setcolor(0);
  259.                 bar(70,325,145,350);
  260.                 bar(70,365,145,390);
  261.                 bar(110,75,190,280);
  262.                 bar(200,75,280,280);
  263.                 bar(350,40,610,360);
  264.                 line(479, 40, 479, 360);//y
  265.                 line(350, 200, 610, 200);//x
  266.                 for(float i=350.0; i<=605.0;i=i+26)
  267.                     line(i,195,i,205);
  268.                 for(float j=50.0; j<=360.0;j=j+50)
  269.                     line(474,j,484,j);
  270.                 outtextxy(465, 35, "Y");
  271.                 outtextxy(605, 204, "X");
  272.                 outtextxy(470, 201, "0");
  273.                 line(479, 40, 476, 43);
  274.                 line(479, 40, 482, 43);
  275.                 line(610, 200, 607, 197);
  276.                 line(610, 200, 607, 203);
  277.                 outtextxy(345,206,"-5");
  278.                 outtextxy(371,206,"-4");
  279.                 outtextxy(397,206,"-3");
  280.                 outtextxy(423,206,"-2");
  281.                 outtextxy(449,206,"-1");
  282.                 outtextxy(501,206,"1");
  283.                 outtextxy(527,206,"2");
  284.                 outtextxy(553,206,"3");
  285.                 outtextxy(579,206,"4");
  286.                 outtextxy(605,206,"5");//x
  287.                 outtextxy(490,45,"150");
  288.                 outtextxy(490,95,"100");
  289.                 outtextxy(490,145,"50");
  290.                 outtextxy(490,245,"-50");
  291.                 outtextxy(490,295,"-100");
  292.                 outtextxy(490,345,"-150");
  293.  
  294.  
  295.                 a=newparam(0.0);
  296.                 b=newparam(40.0);
  297.             } break;
  298.  
  299.             case 'g':{
  300.                 k(270, 420, 370,460, "GRAFIC",GREEN );
  301.                 k(50, 420,150,460, "NEW PARAM", 0);
  302.                 k(470, 420, 570, 460, "EXIT", 0);
  303.                }
  304.  
  305.                 grafic(a,b); break;
  306.  
  307.             case 'e':{
  308.                  k(50, 420,150,460, "NEW PARAM", 0);
  309.                 k(270, 420, 370,460, "GRAFIC", 0);
  310.                  k(470, 420, 570, 460, "EXIT", GREEN);
  311.                  ch='e';
  312.             } break;
  313.  
  314.             case 77:
  315.                 if(nk<3)
  316.                 nk++;
  317.                 k(50, 420,150,460, "NEW PARAM", 0);
  318.                 k(270, 420, 370,460, "GRAFIC", 0);
  319.                 k(470, 420, 570, 460, "EXIT", 0);
  320.                 switch(nk){
  321.                 case 1: k(50, 420,150,460, "NEW PARAM", GREEN);break;
  322.                 case 2: k(270, 420, 370,460, "GRAFIC", GREEN);break;
  323.                 case 3: k(470, 420, 570, 460, "EXIT", GREEN);}break;
  324.  
  325.             case 75:
  326.                 if(nk>1) nk--;
  327.  
  328.                 k(50, 420,150,460, "NEW PARAM", 0);
  329.                 k(270, 420, 370,460, "GRAFIC", 0);
  330.                 k(470, 420, 570, 460, "EXIT", 0);
  331.                 switch(nk){
  332.                     case 1: k(50, 420,150,460, "NEW PARAM", GREEN); break;
  333.                     case 2: k(270, 420, 370,460, "GRAFIC", GREEN);  break;
  334.                     case 3: k(470, 420, 570, 460, "EXIT", GREEN);   break;
  335.                 }break;
  336.  
  337.             case 13:
  338.                 switch(nk) {
  339.                     case 1:
  340.                         setcolor(0);
  341.                         bar(70,325,145,350);
  342.                         bar(70,365,145,390);
  343.                         bar(110,75,190,280);
  344.                         bar(200,75,280,280);
  345.                         bar(350,40,610,360);
  346.                         line(479, 40, 479, 360);//y
  347.                         line(350, 200, 610, 200);//x
  348.                         for(float i=350.0; i<=605.0;i=i+26)
  349.                             line(i,195,i,205);
  350.                         for(float j=50.0; j<=360.0;j=j+50)
  351.                             line(474,j,484,j);
  352.                         outtextxy(465, 35, "Y");
  353.                         outtextxy(605, 204, "X");
  354.                         outtextxy(470, 201, "0");
  355.                         line(479, 40, 476, 43);
  356.                         line(479, 40, 482, 43);
  357.                         line(610, 200, 607, 197);
  358.                         line(610, 200, 607, 203);
  359.                         outtextxy(345,206,"-5");
  360.                         outtextxy(371,206,"-4");
  361.                         outtextxy(397,206,"-3");
  362.                         outtextxy(423,206,"-2");
  363.                         outtextxy(449,206,"-1");
  364.                         outtextxy(501,206,"1");
  365.                         outtextxy(527,206,"2");
  366.                         outtextxy(553,206,"3");
  367.                         outtextxy(579,206,"4");
  368.                         outtextxy(605,206,"5");//x
  369.                         outtextxy(490,45,"150");
  370.                         outtextxy(490,95,"100");
  371.                         outtextxy(490,145,"50");
  372.                         outtextxy(490,245,"-50");
  373.                         outtextxy(490,295,"-100");
  374.                         outtextxy(490,345,"-150");
  375.  
  376.                         a=newparam(0.0);
  377.                         b=newparam(40.0);
  378.                         break;
  379.                     case 2:grafic(a,b); break;
  380.                     case 3: ch='e'; break;
  381.                 }
  382.             }
  383.         }
  384.    } while (ch!='e');
  385.  
  386. }
  387.  
  388. int main(void)
  389. {
  390.     int GrDr, GrMod, rez;
  391.     GrDr = DETECT;
  392.  
  393.     initgraph(&GrDr, &GrMod, " "); // GrDr - графический драйвер;
  394.                                                           //GrMod -графический режим;
  395.  // третий параметр- путь до графического       //драйвера;
  396.  
  397.     rez = graphresult();
  398.     if (rez != grOk)
  399.     {
  400.         printf("\n Error graph modeи");
  401.         return(1);
  402.     }
  403.     gr();
  404.  
  405.     k(50, 420,150,460, "NEW PARAM", GREEN);
  406.     k(270, 420, 370,460, "GRAFIC", 0);
  407.     k(470, 420, 570, 460, "EXIT", 0);
  408.     knopkiupr();
  409.  
  410.     closegraph();
  411.     return(0);
  412. }
  413.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement