PhotoShaman

Laba2

Nov 26th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <cmath>
  4. #include <windows.h>
  5. #include <iomanip>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9.  
  10. static void SetCursorPosition(int x, int y)
  11. {
  12.     COORD coord;
  13.     coord.X = x;
  14.     coord.Y = y;
  15.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  16. }
  17. enum ConsoleColor
  18. {
  19.     Black = 0,
  20.     Blue = 1,
  21.     Green = 2,
  22.     Cyan = 3,
  23.     Red = 4,
  24.     Magenta = 5,
  25.     Brown = 6,
  26.     LightGray = 7,
  27.     DarkGray = 8,
  28.     LightBlue = 9,
  29.     LightGreen = 10,
  30.     LightCyan = 11,
  31.     LightRed = 12,
  32.     LightMagenta = 13,
  33.     Yellow = 14,
  34.     White = 15
  35. };
  36. static void SetColor(int text, int background) // устанавливает цвет текста и фона в консоли
  37. {
  38.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  39.     SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
  40. }
  41. static void SetColor(int text, ConsoleColor/*int*/ background) // устанавливает цвет текста и фона в консоли
  42. {
  43.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  44.     SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
  45. }
  46. static void CursorVisible(bool visible)
  47. {
  48.     void* handle = GetStdHandle(STD_OUTPUT_HANDLE);
  49.     CONSOLE_CURSOR_INFO structCursorInfo;
  50.     GetConsoleCursorInfo(handle, &structCursorInfo);
  51.     structCursorInfo.bVisible = visible;
  52.     SetConsoleCursorInfo(handle, &structCursorInfo);
  53. }
  54. static void Box(int xPos, int yPos, int Weight, int Height)
  55. {
  56.     for (int i = 0; i < Height; i++, yPos++)
  57.     {
  58.         SetCursorPosition(xPos, yPos);
  59.         cout << setw(Weight) << ' ';
  60.     }
  61. }
  62. static void ASCIILogo()
  63. {
  64.     SetColor(15, 7);
  65.     Box(0, 0, 80, 1);
  66.     Box(0, 7, 80, 1);
  67.  
  68.     SetColor(15, 15);
  69.     Box(0, 1, 23, 6);
  70.     Box(59, 1, 21, 6);
  71.  
  72.     int image[6][37] =
  73.     {
  74.         { 12,12,15,15,15,15,15,12,12,12,12,15,15,15,12,12,12,12,12,12,15,15,15,15,15,12,12,12,12,15,15,15,12,12,12,12,16 },
  75.         { 12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,15,15,15,15,15,12,16 },
  76.         { 12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,12,12,12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,12,12,16 },
  77.         { 12,12,15,15,15,15,12,12,12,12,12,12,15,15,12,12,15,15,15,15,12,12,15,15,12,12,12,12,12,12,15,15,12,15,15,15,16 },
  78.         { 12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,15,15,12,15,15,15,16 },
  79.         { 12,12,12,12,15,15,12,12,15,15,12,12,15,15,12,12,12,12,12,12,15,15,15,15,12,12,15,15,12,12,15,15,12,12,12,12,16 }
  80.     };
  81.  
  82.     SetCursorPosition(23, 1);
  83.  
  84.     for (int i = 0; i < 6;)
  85.     {
  86.         for (int j = 0; j < 38; j++)
  87.         {
  88.             if (image[i][j] == 16)
  89.             {
  90.                 i++;
  91.                 SetCursorPosition(23, i + 1); break;
  92.             }
  93.             else if (image[i][j] > 0)
  94.             {
  95.                 SetColor(15, image[i][j]);
  96.                 cout << ' ';
  97.             }
  98.         }
  99.     }
  100.     SetCursorPosition(0, 0);
  101. }
  102. static void Task1() //Задание 1
  103. {
  104.     int x1, x2, x3, y1, y2, y3;
  105.     double lineAB, lineBC, lineAC;
  106.  
  107.     cout << "Введите координату первой точки треугольника A(x1, y1). Например: 2 5\n";
  108.     cin >> x1 >> y1;
  109.  
  110.     cout << "\nВведите координату второй точки треугольника B(x2, y2). Например: 5 1\n";
  111.     cin >> x2 >> y2;
  112.  
  113.     cout << "\nВведите координату третей точки треугольника C(x3 y3). Например: 11 3\n";
  114.     cin >> x3 >> y3;
  115.  
  116.     lineAB = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  117.     lineBC = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
  118.     lineAC = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
  119.  
  120.     if (lineAB < lineBC && lineAB < lineAC) cout << "Сторона AB найменьшая.";
  121.     else if (lineBC < lineAB && lineBC < lineAC) cout << "Сторона BC найменьшая.";
  122.     else if (lineAC < lineAB && lineAC < lineBC) cout << "Сторона AC найменьшая.";
  123.  
  124.     else if (lineAB == lineBC && lineAB == lineAC) cout << "Стороны AB, BC и AC равны.";
  125.  
  126.     else if (lineAB == lineBC && lineAB > lineAC) cout << "Сторона AC найменьшая, а стороны AB и BC равны";
  127.     else if (lineAB == lineBC && lineAB < lineAC) cout << "Стороны AB и BC равны и меньше стороны AC";
  128.  
  129.     else if (lineAC == lineBC && lineAC > lineAB) cout << "Сторона AB найменьшая, а стороны AC и BC равны";
  130.     else if (lineAC == lineBC && lineAC < lineAB) cout << "Стороны AC и BC равны и меньше стороны AB";
  131.  
  132.     else if (lineAB == lineAC && lineAB > lineBC) cout << "Сторона BC найменьшая, а стороны AB и AC равны";
  133.     else if (lineAB == lineAC && lineAB < lineBC) cout << "Стороны AB и AC равны и меньше стороны BC";
  134.     else cout << "Что-то пошло не так...";
  135.     cout << endl;
  136.     system("pause");
  137.     system("cls");
  138. }
  139. static void Task2() //Задание 2
  140. {
  141.     float number1, number2;
  142.     char action;
  143.  
  144.     cout << "Введите операцию для калькулятора. Например: 3 * 5.\n\n";
  145.     cout << "Доступные действия: сложение:  +\n";
  146.     cout << "                    вычитание: -\n";
  147.     cout << "                    умножение: *\n";
  148.     cout << "                    деление:   /\n";
  149.     cin >> number1 >> action >> number2;
  150.  
  151.     switch (action)
  152.     {
  153.     case '+': cout << number1 + number2; break;
  154.     case '-': cout << number1 - number2; break;
  155.     case '*': cout << number1 * number2; break;
  156.     case '/': cout << number1 / number2; break;
  157.     default: cout << "Что-то пошло не так...";
  158.         break;
  159.     }
  160.     cout << endl;
  161.  
  162.     system("pause");
  163.     system("cls");
  164. }
  165. static void Task3() //Задание 3
  166. {
  167.     int a, b, c;
  168.  
  169.     cout << "Введите числа a, b и c. Например: 3 4 5.\n";
  170.     cin >> a >> b >> c;
  171.  
  172.     if (a + b > c && c + b > a && a + c > b) cout << "\nTriangle\n";
  173.     else cout << "\nNot triangle\n";
  174.  
  175.     //Предыдущий вариант
  176.     //if (a + b < c || c + b < a || a + c < b) cout << "\nNot triangle\n";
  177.     //else cout << "\nTriangle\n";
  178.  
  179.     system("pause");
  180.     system("cls");
  181. }
  182. static void Task4() //Задание 4
  183. {
  184.     int number;
  185.     cout << "Введите число от 100 до 999: ";
  186.     cin >> number;
  187.     if (number >= 100 && number <= 999)
  188.     {
  189.         switch (number / 100)
  190.         {
  191.         case 1: cout << "Сто "; break;
  192.         case 2:  cout << "Двести "; break;
  193.         case 3:  cout << "Триста "; break;
  194.         case 4:  cout << "Четыреста "; break;
  195.         case 5:  cout << "Пятьсот "; break;
  196.         case 6:  cout << "Шестьсот "; break;
  197.         case 7:  cout << "Семьсот "; break;
  198.         case 8:  cout << "Восемьсот "; break;
  199.         case 9:  cout << "Девятьсот "; break;
  200.         }
  201.         if (number % 100 > 10 && number % 100 < 21)
  202.         {
  203.             switch (number % 100)
  204.             {
  205.             case 11: cout << "одиннадцать"; break;
  206.             case 12: cout << "двенадцать"; break;
  207.             case 13: cout << "тринадцать"; break;
  208.             case 14: cout << "четырнадцать"; break;
  209.             case 15: cout << "пятнадцать"; break;
  210.             case 16: cout << "шестнадцать"; break;
  211.             case 17: cout << "семнадцать"; break;
  212.             case 18: cout << "восемнадцать"; break;
  213.             case 19: cout << "девятнадцать"; break;
  214.             case 20: cout << "двадцать"; break;
  215.             }
  216.         }
  217.         else
  218.         {
  219.             switch (number % 100 / 10)
  220.             {
  221.             case 0: break;
  222.             case 1: cout << "десять "; break;
  223.             case 2:  cout << "двадцать "; break;
  224.             case 3:  cout << "тридцать "; break;
  225.             case 4:  cout << "сорок "; break;
  226.             case 5:  cout << "пятьдесят "; break;
  227.             case 6:  cout << "шестьдесят "; break;
  228.             case 7:  cout << "семьдесят "; break;
  229.             case 8:  cout << "восемьдесят "; break;
  230.             case 9:  cout << "девяносто "; break;
  231.             }
  232.             switch (number % 100 % 10)
  233.             {
  234.             case 0: break;
  235.             case 1:  cout << "один"; break;
  236.             case 2:  cout << "два"; break;
  237.             case 3:  cout << "три"; break;
  238.             case 4:  cout << "четыре"; break;
  239.             case 5:  cout << "пять"; break;
  240.             case 6:  cout << "шесть"; break;
  241.             case 7:  cout << "семь"; break;
  242.             case 8:  cout << "восемь"; break;
  243.             case 9:  cout << "девять"; break;
  244.             }
  245.         }
  246.     }
  247.     else
  248.     {
  249.         cout << "Ошибка условия";
  250.     }
  251.     cout << endl;
  252.     system("pause"); //Delay
  253. }
  254. static void TaskList()
  255. {
  256.     SetColor(9, 7);
  257.     Box(0, 9, 80, 1);
  258.  
  259.     SetCursorPosition(23, 9);
  260.     cout << "Основные задания:";
  261.     SetCursorPosition(44, 9);
  262.     cout << "Дополнительные:";
  263.  
  264.     SetColor(0, 15);
  265.     Box(23, 12, 5, 3);
  266.     Box(33, 12, 5, 3);
  267.     Box(44, 12, 5, 3);
  268.     Box(54, 12, 5, 3);
  269.  
  270.     SetCursorPosition(25, 13);
  271.     cout << '1';
  272.     SetCursorPosition(35, 13);
  273.     cout << '2';
  274.     SetCursorPosition(46, 13);
  275.     cout << '3';
  276.     SetCursorPosition(56, 13);
  277.     cout << '4';
  278.  
  279.     SetColor(9, 7);
  280.     Box(0, 17, 80, 1);
  281.  
  282.     SetCursorPosition(23, 17);
  283.     cout << "Управление:";
  284.  
  285.     SetColor(15, 0);
  286.     SetCursorPosition(23, 19); cout << "Движение курсора стрелочками влево и вправо.";
  287.     SetCursorPosition(23, 21); cout << "Выбор задания - стрелочка вверх.";
  288.     SetCursorPosition(23, 23); cout << "Выход - ESC.";
  289. }
  290. static void ChooseTask()
  291. {
  292.     int x = 23;
  293.     CursorVisible(false);
  294.     SetColor(0, 15);
  295.  
  296.     while (true)
  297.     {
  298.         SetCursorPosition(x, 12); cout << "/---\\";
  299.         SetCursorPosition(x, 13); cout << "|"; SetCursorPosition(x + 4, 13); cout << "|";
  300.         SetCursorPosition(x, 14); cout << "\\---/";
  301.         Sleep(100);
  302.         SetCursorPosition(x, 12); cout << "-\\\\\\|";
  303.         SetCursorPosition(x, 13); cout << "/"; SetCursorPosition(x + 4, 13); cout << "/";
  304.         SetCursorPosition(x, 14); cout << "|\\\\\\-";
  305.         Sleep(100);
  306.         SetCursorPosition(x, 12); cout << "\\|||/";
  307.         SetCursorPosition(x, 13); cout << "-"; SetCursorPosition(x + 4, 13); cout << "-";
  308.         SetCursorPosition(x, 14); cout << "/|||\\";
  309.         Sleep(100);
  310.         SetCursorPosition(x, 12); cout << "|///-";
  311.         SetCursorPosition(x, 13); cout << "\\"; SetCursorPosition(x + 4, 13); cout << "\\";
  312.         SetCursorPosition(x, 14); cout << "-///|";
  313.         Sleep(50);
  314.  
  315.         switch (_getch())
  316.         {
  317.         case 75: //Влево
  318.             if (x != 23)
  319.             {
  320.                 SetCursorPosition(x, 12); cout << "     ";
  321.                 SetCursorPosition(x, 13); cout << " "; SetCursorPosition(x + 4, 13); cout << " ";
  322.                 SetCursorPosition(x, 14); cout << "     ";
  323.                 if (x != 44) x -= 10;
  324.                 else x -= 11;
  325.             }
  326.             break;
  327.         case 77: //Вправо
  328.             if (x != 54)
  329.             {
  330.                 SetCursorPosition(x, 12); cout << "     ";
  331.                 SetCursorPosition(x, 13); cout << " "; SetCursorPosition(x + 4, 13); cout << " ";
  332.                 SetCursorPosition(x, 14); cout << "     ";
  333.                 if (x != 33) x += 10;
  334.                 else x += 11;
  335.             }
  336.             break;
  337.         case 72: //Вверх
  338.             SetColor(15, 0);
  339.             system("cls");
  340.             CursorVisible(true);
  341.             switch (x)
  342.             {
  343.             case 23: Task1(); break;
  344.             case 33: Task2(); break;
  345.             case 44: Task3(); break;
  346.             case 54: Task4(); break;
  347.             }
  348.             Sleep(1000);
  349.             system("cls");
  350.             return;
  351.         case 27: //Esc
  352.             exit(1);
  353.             Sleep(200);
  354.             break;
  355.         }
  356.     }
  357. }
  358. void main()
  359. {
  360.     setlocale(LC_ALL, "Russian");
  361.     system("mode con cols=80 lines=40");
  362.     //SetConsoleTitle("Laba 2"); //если ошибка, заменить на: system("title Laba 2");
  363.     system("title Laba 2");
  364.     while (true)
  365.     {
  366.         ASCIILogo();
  367.         TaskList();
  368.         ChooseTask();
  369.     }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment