abubaca

Untitled

Dec 23rd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 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. #define WIDTH 80
  12. #define HEIGHT 30
  13. const double PI = 3.141592653589793238463;
  14.  
  15. void print(const char text[], int x, int y)
  16. {
  17.     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  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.     string output;
  95.     output = "sin(x)";
  96.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 - 1);
  97.     output = "x(grad)=";
  98.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2);
  99.     double x;
  100.     cin >> x;
  101.     output = "sin("+ to_string(x)+")="+ to_string(sin((x * PI) / 180));
  102.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  103.     int input = 0;
  104.     while (input!=27 && input!=13)
  105.     {
  106.         input = _getch();
  107.     }
  108. }
  109.  
  110. void calck_cos()
  111. {
  112.     system("cls");
  113.     string output;
  114.     output = "cos(x)";
  115.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 - 1);
  116.     output = "x(grad)=";
  117.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2);
  118.     double x;
  119.     cin >> x;
  120.     output = "cos(" + to_string(x) + ")=" + to_string(cos((x * PI) / 180));
  121.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  122.     int input = 0;
  123.     while (input != 27 && input != 13)
  124.     {
  125.         input = _getch();
  126.     }
  127. }
  128.  
  129. void calck_tg()
  130. {
  131.     system("cls");
  132.     string output;
  133.     output = "tg(x)";
  134.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 - 1);
  135.     output = "x(grad)=";
  136.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2);
  137.     double x;
  138.     cin >> x;
  139.     output = "tg(" + to_string(x) + ")=" + to_string(tan((x * PI) / 180));
  140.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  141.     int input = 0;
  142.     while (input != 27 && input != 13)
  143.     {
  144.         input = _getch();
  145.     }
  146. }
  147.  
  148. void calck_simple()
  149. {
  150.     system("cls");
  151.     string output;
  152.     output = "Enter action:";
  153.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 - 2);
  154.     int action = 0;
  155.     while (action != '+' && action != '-' && action != '*' && action != '/')
  156.     {
  157.         action = _getch();
  158.         if (action == '+' || action == '-' || action == '*' || action == '/')
  159.             cout << (char)action;
  160.     }
  161.     output = "x";
  162.     output += (char)action;
  163.     output += "y";
  164.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 - 1);
  165.     double x, y, result;
  166.     output = "x=";
  167.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2);
  168.     cin >> x;
  169.     output = "y=";
  170.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
  171.     cin >> y;
  172.     switch (action)
  173.     {
  174.     case '+':
  175.         result = x + y;
  176.         break;
  177.     case '-':
  178.         result = x - y;
  179.         break;
  180.     case '*':
  181.         result = x * y;
  182.         break;
  183.     case '/':
  184.         result = x / y;
  185.         break;
  186.     }
  187.     output = to_string(x);
  188.     output += (char)action;
  189.     output += to_string(y) + "=" + to_string(result);
  190.     print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 2);
  191.     int input = 0;
  192.     while (input != 27 && input != 13)
  193.     {
  194.         input = _getch();
  195.     }
  196. }
  197.  
  198. int main()
  199. {
  200.     string resize = "mode "+ to_string(WIDTH)+ ", "+ to_string(HEIGHT);
  201.     system(resize.c_str());
  202.     setlocale(LC_ALL, "Russian");
  203.     menu first;
  204.     first.add("sin", calck_sin);
  205.     first.add("cos", calck_cos);
  206.     first.add("tg", calck_tg);
  207.     first.add("simple function", calck_simple);
  208.     first.get();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment