Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include <vector>
- #include <string>
- #include <conio.h>
- #include <functional>
- #include <cmath>
- using namespace std;
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- #define WIDTH 80
- #define HEIGHT 30
- const double PI = 3.141592653589793238463;
- void print(const char text[], int x, int y)
- {
- COORD pos;
- pos.X = x;
- pos.Y = y;
- SetConsoleCursorPosition(hCon, pos);
- cout << text;
- }
- class menu
- {
- vector <string> points;
- vector <function<void()>> actions;
- int point = 0;
- void show()
- {
- system("cls");
- int xMiddle = WIDTH / 2;
- int yPart = HEIGHT / (points.size() + 1);
- for (int i = 0; i < points.size(); i++)
- {
- if (i == point)
- {
- string temp = "<<" + points[i] + ">>";
- print(temp.c_str(), xMiddle - temp.length() / 2, yPart*(i+1));
- }
- else
- print(points[i].c_str(), xMiddle - points[i].length() / 2, yPart*(i+1));
- }
- }
- void up()
- {
- point = point == 0 ? points.size() - 1 : point - 1;
- }
- void down()
- {
- point = point == points.size() - 1 ? 0 : point + 1;
- }
- void enter()
- {
- actions[point]();
- }
- public:
- void add(string newP, function<void()> newA)
- {
- points.push_back(newP);
- actions.push_back(newA);
- }
- void get()
- {
- show();
- int button;
- while (true)
- {
- button = _getch();
- switch (button)
- {
- case 72:
- up();
- break;
- case 80:
- down();
- break;
- case 13:
- enter();
- break;
- case 27:
- return;
- break;
- }
- show();
- }
- }
- };
- void calck_sin()
- {
- system("cls");
- print("sin(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
- print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
- double temp;
- cin >> temp;
- string output = "sin("+ to_string(temp)+")="+ to_string(sin((temp * PI) / 180));
- print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
- int input = 0;
- while (input!=27 && input!=13)
- {
- input = _getch();
- }
- }
- void calck_cos()
- {
- system("cls");
- print("cos(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
- print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
- double temp;
- cin >> temp;
- string output = "cos(" + to_string(temp) + ")=" + to_string(cos((temp * PI) / 180));
- print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
- int input = 0;
- while (input != 27 && input != 13)
- {
- input = _getch();
- }
- }
- void calck_tg()
- {
- system("cls");
- print("tg(x)", WIDTH / 2 - 3, HEIGHT / 2 - 1);
- print("x(grad)=", WIDTH / 2 - 5, HEIGHT / 2);
- double temp;
- cin >> temp;
- string output = "tg(" + to_string(temp) + ")=" + to_string(tan((temp * PI) / 180));
- print(output.c_str(), WIDTH / 2 - output.length() / 2, HEIGHT / 2 + 1);
- int input = 0;
- while (input != 27 && input != 13)
- {
- input = _getch();
- }
- }
- void calck_simple()
- {
- system("cls");
- print("Enter action:", WIDTH / 2 - 7, HEIGHT / 2 - 2);
- int action = 0;
- while (action != '+' && action != '-' && action != '*' && action != '/')
- {
- action = _getch();
- if (action == '+' || action == '-' || action == '*' || action == '/')
- cout << (char)action;
- }
- string temp = "x";
- temp += (char)action;
- temp += "y";
- print(temp.c_str(), WIDTH / 2 - 1, HEIGHT / 2 - 1);
- double x, y, result;
- print("x=", WIDTH / 2 - 1, HEIGHT / 2);
- cin >> x;
- print("y=", WIDTH / 2 - 1, HEIGHT / 2 + 1);
- cin >> y;
- switch (action)
- {
- case '+':
- result = x + y;
- break;
- case '-':
- result = x - y;
- break;
- case '*':
- result = x * y;
- break;
- case '/':
- result = x / y;
- break;
- }
- temp = to_string(x);
- temp += (char)action;
- temp += to_string(y) + "=" + to_string(result);
- print(temp.c_str(), WIDTH / 2 - temp.length() / 2, HEIGHT / 2 + 2);
- int input = 0;
- while (input != 27 && input != 13)
- {
- input = _getch();
- }
- }
- int main()
- {
- string resize = "mode "+ to_string(WIDTH)+ ", "+ to_string(HEIGHT);
- system(resize.c_str());
- menu first;
- first.add("sin", calck_sin);
- first.add("cos", calck_cos);
- first.add("tg", calck_tg);
- first.add("simple function", calck_simple);
- first.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment