abubaca

Untitled

Dec 23rd, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include <vector>
  4. #include <string>
  5. #include <conio.h>
  6. #include <functional>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  12. #define WIDTH 80
  13. #define HEIGHT 30
  14. const double PI = 3.141592653589793238463;
  15.  
  16. void print(const char text[], int x, int y)
  17. {
  18.     COORD pos;
  19.     pos.X = x;
  20.     pos.Y = y;
  21.     SetConsoleCursorPosition(hCon, pos);
  22.     cout << text;
  23. }
  24.  
  25. class menu
  26. {
  27.     vector <string> points;
  28.     vector <function<void()>> actions;
  29.     int point = 0;
  30.     void show()
  31.     {
  32.         system("cls");
  33.         int xMiddle = WIDTH / 2;
  34.         int yPart = HEIGHT / (points.size() + 1);
  35.         for (int i = 0; i < points.size(); i++)
  36.         {
  37.             if (i == point)
  38.             {
  39.                 string temp = "<<" + points[i] + ">>";
  40.                 print(temp.c_str(), xMiddle - temp.length() / 2, yPart*(i+1));
  41.             }
  42.             else
  43.                 print(points[i].c_str(), xMiddle - points[i].length() / 2, yPart*(i+1));
  44.         }
  45.     }
  46.     void up()
  47.     {
  48.         point = point == 0 ? points.size() - 1 : point - 1;
  49.     }
  50.     void down()
  51.     {
  52.         point = point == points.size() - 1 ? 0 : point + 1;
  53.     }
  54.     void enter()
  55.     {
  56.         actions[point]();
  57.     }
  58.     public:
  59.         void add(string newP, function<void()> newA)
  60.         {
  61.             points.push_back(newP);
  62.             actions.push_back(newA);
  63.         }
  64.         void get()
  65.         {
  66.             show();
  67.             int button;
  68.             while (true)
  69.             {
  70.                 button = _getch();
  71.                 switch (button)
  72.                 {
  73.                 case 72:
  74.                     up();
  75.                     break;
  76.                 case 80:
  77.                     down();
  78.                     break;
  79.                 case 13:
  80.                     enter();
  81.                     break;
  82.                 case 27:
  83.                     return;
  84.                     break;
  85.                 }
  86.                 show();
  87.             }
  88.         }
  89. };
  90.  
  91. void calck_sin()
  92. {
  93.     system("cls");
  94.     print("sin(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
  95.     print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
  96.     double temp;
  97.     cin >> temp;
  98.     string output = "sin("+ to_string(temp)+")="+ to_string(sin((temp * PI) / 180));
  99.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  100.     int input = 0;
  101.     while (input!=27 && input!=13)
  102.     {
  103.         input = _getch();
  104.     }
  105. }
  106.  
  107. void calck_cos()
  108. {
  109.     system("cls");
  110.     print("cos(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
  111.     print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
  112.     double temp;
  113.     cin >> temp;
  114.     string output = "cos(" + to_string(temp) + ")=" + to_string(cos((temp * PI) / 180));
  115.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  116.     int input = 0;
  117.     while (input != 27 && input != 13)
  118.     {
  119.         input = _getch();
  120.     }
  121. }
  122.  
  123. void calck_tg()
  124. {
  125.     system("cls");
  126.     print("tg(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
  127.     print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
  128.     double temp;
  129.     cin >> temp;
  130.     string output = "tg(" + to_string(temp) + ")=" + to_string(tan((temp * PI) / 180));
  131.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  132.     int input = 0;
  133.     while (input != 27 && input != 13)
  134.     {
  135.         input = _getch();
  136.     }
  137. }
  138.  
  139. void calck_simple()
  140. {
  141.     system("cls");
  142.     print("Enter action:", WIDTH / 2 - 7, HEIGHT / 2 - 2);
  143.     int action = 0;
  144.     while (action != '+' && action != '-' && action != '*' && action != '/')
  145.     {
  146.         action = _getch();
  147.         if (action == '+' || action == '-' || action == '*' || action == '/')
  148.             cout << (char)action;
  149.     }
  150.     string temp = "x";
  151.     temp += (char)action;
  152.     temp += "y";
  153.     print(temp.c_str(), WIDTH / 2 - 1, HEIGHT / 2 - 1);
  154.     double x, y, result;
  155.     print("x=", WIDTH / 2 - 1, HEIGHT / 2);
  156.     cin >> x;
  157.     print("y=", WIDTH / 2 - 1, HEIGHT / 2 + 1);
  158.     cin >> y;
  159.     switch (action)
  160.     {
  161.     case '+':
  162.         result = x + y;
  163.         break;
  164.     case '-':
  165.         result = x - y;
  166.         break;
  167.     case '*':
  168.         result = x * y;
  169.         break;
  170.     case '/':
  171.         result = x / y;
  172.         break;
  173.     }
  174.     temp = to_string(x);
  175.     temp += (char)action;
  176.     temp += to_string(y) + "=" + to_string(result);
  177.     print(temp.c_str(), WIDTH / 2 - temp.length() / 2, HEIGHT / 2 + 2);
  178.     int input = 0;
  179.     while (input != 27 && input != 13)
  180.     {
  181.         input = _getch();
  182.     }
  183. }
  184.  
  185. int main()
  186. {
  187.     string resize = "mode "+ to_string(WIDTH)+ ", "+ to_string(HEIGHT);
  188.     system(resize.c_str());
  189.     menu first;
  190.     first.add("sin", calck_sin);
  191.     first.add("cos", calck_cos);
  192.     first.add("tg", calck_tg);
  193.     first.add("simple function", calck_simple);
  194.     first.get();
  195. }
Advertisement
Add Comment
Please, Sign In to add comment