Advertisement
Mike_be

New gaidel lab 3

Nov 10th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <windows.h>
  8. using namespace std;
  9.  
  10. string input = "";
  11. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  12.  
  13.  
  14. inline void print_str(string str, int k, int n)
  15. {
  16.     SetConsoleTextAttribute(hConsole, k);
  17.     for (int l = 0; l < int(str.size()); l++)
  18.     {
  19.         cout << str[l];
  20.         Sleep(n);
  21.     }
  22.     SetConsoleTextAttribute(hConsole, 14);
  23. }
  24.  
  25. void read_error(bool& only_digits, bool& negative, int& dot_count)
  26. {
  27.     const string null_err = "You wrote nothing, stop it";
  28.     const string mess_err = "S-stop testing me, you baka! >/////<";
  29.     const string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
  30.     const string neg_dot_err = "You are not supposed to write negative fractional number here";
  31.     const string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
  32.     const string string_err = "This is not a number, do you think i can read it? Please";
  33.     const string dot_err = "This number can't be fractional, you silly. Please";
  34.     const string neg_err = "You number is way too negative";
  35.     const string repeat = ", type again: ";
  36.     if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 15); print_str(repeat, 13, 15); }
  37.     else if (!only_digits && negative) { print_str(neg_str_err, 4, 15); print_str(repeat, 4, 15); }
  38.     else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 15); print_str(repeat, 4, 15); }
  39.     else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 15); print_str(repeat, 4, 15); }
  40.     else if (!only_digits) { print_str(string_err, 4, 15); print_str(repeat, 4, 15); }
  41.     else if (dot_count != 0) { print_str(dot_err, 4, 15); print_str(repeat, 4, 15); }
  42.     else if (negative) { print_str(neg_err, 4, 15); print_str(repeat, 4, 15); }
  43.     else if (input.length() == 0) { print_str(null_err, 4, 15); print_str(repeat, 4, 15); }
  44. }
  45.  
  46. bool is_digit(char s)
  47. {
  48.     if (s == '0' || s == '1' || s == '2' || s == '3' || s == '4' || s == '5' || s == '6' || s == '7' || s == '8' || s == '9') return true;
  49.     else return false;
  50. }
  51.  
  52. vector<int> read_vec_int(const string start_mes, int n) { //Checks variable for correct input (number)
  53.     vector<int> vec;
  54.     string input = "";
  55.     print_str(start_mes + ": ", 15, 15);
  56.     getline(cin, input);
  57.     int dot_count = 0, k = 0;
  58.     bool only_digits = true, negative = false;
  59.     while (vec.size() != n + 1)
  60.     {
  61.         istringstream iss(input);
  62.         if (input[0] == '-') { k = 1; }
  63.         while ((!only_digits || dot_count == 0) && iss)
  64.         {
  65.             string temp;
  66.             iss >> temp;
  67.             for (int i = int(temp.length()) - 1; i >= k; i--) {
  68.                 if (!is_digit(temp[i])) {
  69.                     if (temp[i] == '.' || temp[i] == ',') dot_count += 1;
  70.                     else only_digits = false;
  71.                 }
  72.             }
  73.             if (only_digits && dot_count == 0 && input.length() != 0) {
  74.                 vec.push_back(strtol(temp.c_str(), NULL, 0));
  75.             }
  76.             else {
  77.                 read_error(only_digits, negative, dot_count);
  78.                 getline(cin, input);
  79.                 only_digits = true;
  80.                 dot_count = 0;
  81.                 k = 0;
  82.             }
  83.         }
  84.         if (vec.size() != n + 1)
  85.         {
  86.             cout << "You wrote " << vec.size() - 1 << " numbers, you need to write " << n << " numbers, type again: ";
  87.             vec.clear();
  88.             getline(cin, input);
  89.         }
  90.     }
  91.     vec.pop_back();
  92.     return vec;
  93. }
  94.  
  95. int read_uint(const string start_mes) { //Checks variable for correct input (number)
  96.     print_str(start_mes + ": ", 15, 15);
  97.     getline(cin, input);
  98.     int dot_count = 0, k = 0;
  99.     bool only_digits = true, negative = false;
  100.     while (!only_digits || dot_count == 0 || input.length() == 0) {
  101.         if (input[0] == '-') { k = 1; negative = true; }
  102.         for (int i = input.length() - 1; i >= k; i--) {
  103.             if (!is_digit(input[i])) {
  104.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  105.                 else only_digits = false;
  106.             }
  107.         }
  108.         if (only_digits && dot_count == 0 && !negative && input.length() != 0) {
  109.             return strtol(input.c_str(), NULL, 0);
  110.         }
  111.         else {
  112.             read_error(only_digits, negative, dot_count);
  113.             getline(cin, input);
  114.             only_digits = true;
  115.             dot_count = 0;
  116.             negative = false;
  117.             k = 0;
  118.         }
  119.     }
  120.     return 0;
  121. }
  122.  
  123. int read_int(const string start_mes) { //Checks variable for correct input (number)
  124.     print_str(start_mes + ": ", 15, 15);
  125.     getline(cin, input);
  126.     int dot_count = 0, k = 0;
  127.     bool only_digits = true, negative = false;
  128.     while (!only_digits || dot_count == 0) {
  129.         if (input[0] == '-') { k = 1; }
  130.         for (int i = int(input.length()) - 1; i >= k; i--) {
  131.             if (!is_digit(input[i])) {
  132.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  133.                 else only_digits = false;
  134.             }
  135.         }
  136.         if (only_digits && dot_count == 0 && input.length() != 0) {
  137.             return strtol(input.c_str(), NULL, 0);
  138.         }
  139.         else {
  140.             read_error(only_digits, negative, dot_count);
  141.             getline(cin, input);
  142.             only_digits = true;
  143.             dot_count = 0;
  144.             k = 0;
  145.         }
  146.     }
  147.     return 0;
  148. }
  149.  
  150. void print(vector<vector<int>> vec)
  151. {
  152.     unsigned int i = 0, j = 0;
  153.     print_str("Your matrix: \n", 15, 15);
  154.     while (i < vec.size())
  155.     {
  156.         while (j < vec[i].size())
  157.         {
  158.             cout << setw(6) << vec[i][j];
  159.             j++;
  160.         }
  161.         cout << endl;
  162.         j = 0;
  163.         i++;
  164.     }
  165. }
  166.  
  167. int check_2(string temp) //Checks input to decide between line and column
  168. {
  169.     if (temp == "Column" || temp == "column") return 1; // Number for doing stuff with column
  170.     else if (temp == "Line" || temp == "line") return 2; // Number for doing stuff with line
  171.     else return 0; // Number if input is incorrect
  172. }
  173.  
  174. int check(string temp)
  175. {
  176.     if (temp == "Erase" || temp == "erase") return 1; // Number for returning result for Erase function
  177.     else if (temp == "Add" || temp == "add") return 2; // Number for Add function
  178.     else if (temp == "Exit" || temp == "exit") return 3; // Number for exiting program
  179.     else return 0; // Number if input is incorrect
  180. }
  181.  
  182. int yes_no(string temp)
  183. {
  184.     if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return 1; // Number for returning result for yes answer
  185.     else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return 2; // Number for no answer
  186.     else return 0; // Number if input is incorrect
  187. }
  188.  
  189. void edit_check_line(int& n, vector<vector<int>> vec){
  190.     do
  191.     {
  192.         if (n > int(vec.size())) {
  193.             print_str("Type number that is lower or equal ", 15, 15);
  194.             cout << vec.size();
  195.             print_str(", the amount of lines in your matrix, ", 15, 15);
  196.         }
  197.         else if (n == 0)
  198.         {
  199.             print_str("You wrote 0, did you mean first element? [Yes/No]: ", 15, 15);
  200.             do
  201.             {
  202.                 getline(cin, input);
  203.                 if (yes_no(input) == 0) print_str("Type [Yes/No]: ", 15, 15);
  204.             } while (yes_no(input) == 0);
  205.             if (yes_no(input) == 1) n = 1;
  206.         }
  207.         if (n == 0 || n > int(vec.size())) n = read_uint("type again");
  208.     } while (n > int(vec.size()) || n == 0);
  209. }
  210.  
  211. void edit_check_column(int& n, vector<vector<int>> vec) {
  212.     do
  213.     {
  214.         if (n > int(vec[0].size())) {
  215.             print_str("Type number that is lower or equal ", 15, 15);
  216.             cout << vec[0].size();
  217.             print_str(", the amount of columns in your matrix, ", 15, 15);
  218.         }
  219.         else if (n == 0)
  220.         {
  221.             print_str("You wrote 0, did you mean first element? [Yes/No]: ", 15, 15);
  222.             do
  223.             {
  224.                 getline(cin, input);
  225.                 if (yes_no(input) == 0) print_str("Type [Yes/No]: ", 15, 15);
  226.             } while (yes_no(input) == 0);
  227.             if (yes_no(input) == 1) n = 1;
  228.         }
  229.         if (n == 0 || n > int(vec[0].size())) n = read_uint("type again");
  230.     } while (n > int(vec[0].size()) || n == 0);
  231. }
  232.  
  233. void erase_line(vector<vector<int>>& vec)
  234. {
  235.     int n = read_uint("What line do you want to erase?");
  236.     edit_check_line(n, vec);
  237.     int lines = n - 1;
  238.     while (lines < int(vec.size()) - 1)
  239.     {
  240.         vec[lines] = vec[lines + 1];
  241.         lines++;
  242.     }
  243.     vec.pop_back();
  244. }
  245.  
  246. void erase_column(vector<vector<int>>& vec)
  247. {
  248.     int n = read_uint("What column do you want to erase?"), lines = 0;
  249.     edit_check_column(n, vec);
  250.     int column = n - 1;
  251.     while (lines < int(vec.size()))
  252.     {
  253.         while (column < int(vec[lines].size()) - 1)
  254.         {
  255.             vec[lines][column] = vec[lines][column + 1];
  256.             column++;
  257.         }
  258.         column = n - 1;
  259.         lines++;
  260.  
  261.     }
  262.     lines = 0;
  263.     while (lines < int(vec.size()))
  264.     {
  265.         vec[lines].pop_back();
  266.         lines++;
  267.     }
  268. }
  269.  
  270. void add_line(vector<vector<int>>& vec)
  271. {
  272.     int n = read_uint("Where do you want to add line?"), i = 1;
  273.     vec.push_back(vec[vec.size() - i]);
  274.     edit_check_line(n, vec);
  275.     vector<int> vec_2 = read_vec_int("Write the new numbers", vec[0].size());
  276.     i++;
  277.     while ( int(vec.size()) - i >= n-1)
  278.     {
  279.         vec[vec.size() - i + 1] = vec[vec.size() - i];
  280.         i++;
  281.     }
  282.     vec[n - 1] = vec_2;
  283. }
  284.  
  285. void add_column(vector<vector<int>>& vec)
  286. {
  287.     int n = read_uint("Where do you want to add column?"), lines = 0;
  288.     while (lines < int(vec.size()))
  289.     {
  290.         vec[lines].push_back(vec[lines][vec[lines].size() - 1]);
  291.         lines++;
  292.     }
  293.     lines = 0;
  294.     int column = vec[0].size() - 2;
  295.     edit_check_column(n, vec);
  296.     vector<int> vec_2 = read_vec_int("Write the new numbers", vec.size());
  297.     while (lines < int(vec.size()))
  298.     {
  299.         while (column >= n - 1)
  300.         {
  301.             vec[lines][column + 1] = vec[lines][column];
  302.             column--;
  303.         }
  304.         column = vec[0].size() - 2;
  305.         lines++;
  306.        
  307.     }
  308.     lines = 0;
  309.     while (lines < int(vec.size()))
  310.     {
  311.         vec[lines][n - 1] = vec_2[lines];
  312.         lines++;
  313.     }
  314. }
  315.  
  316. void choice(vector<vector<int>>& vec, bool& cycle) //Function for choosing path of actions
  317. {
  318.     if (check(input) == 1) //Path if user decided to erase
  319.     {
  320.         print_str("Do you want to erase line or column? ", 15, 15);
  321.         do
  322.         {
  323.             getline(cin, input);
  324.             if (check_2(input) == 0) print_str("Type [Line] or [Column] without brackets: ", 15, 15);
  325.         } while (check_2(input) == 0);
  326.         if (check_2(input) == 1) erase_column(vec);
  327.         else if (check_2(input) == 2) erase_line(vec);
  328.     }
  329.     else if (check(input) == 2) //Path if user decided to add
  330.     {
  331.         print_str("Do you want to add line or column? ", 15, 15);
  332.         do
  333.         {
  334.             getline(cin, input);
  335.             if (check_2(input) == 0) print_str("Type [Line] or [Column] without brackets: ", 15, 15);
  336.         } while (check_2(input) == 0);
  337.         if (check_2(input) == 1) add_column(vec);
  338.         else if (check_2(input) == 2) add_line(vec);
  339.     }
  340.     cycle = true; //Variable to control out message
  341. }
  342.  
  343. bool is_vector_empty(vector<vector<int>>& vec) //Checks if vector is empty based on his size
  344. {
  345.     bool is_line_empty = true, is_column_empty = true;
  346.     int j = 0;
  347.     while (j < int(vec.size()))
  348.     {
  349.         if (vec[j].size() != 0) is_column_empty = false;
  350.         j++;
  351.     }
  352.     if (!vec.empty()) is_line_empty = false;
  353.     if (is_line_empty == false && is_column_empty == false) return false;
  354.     else return true;
  355. }
  356.  
  357. void vec_read(vector<vector<int>>& vec) //Filling vector from users input
  358. {
  359.     const int lines = read_uint("Write number of lines of your matrix"), columns = read_uint(
  360.         "Write number of columns of your matrix");;
  361.     int lines_cycle = 0, columns_cycle = 0;
  362.     if (lines != 0 && columns != 0) //If columns or lines equal to zero, it doesn't fill matrix
  363.     {
  364.         while (lines_cycle < lines)
  365.         {
  366.             vector<int> temp;
  367.             while (columns_cycle < columns)
  368.             {
  369.                 cout << "[" << lines_cycle + 1 << "]" << "[" << columns_cycle + 1 << "] "; //Prints current matrix coords
  370.                 temp.push_back(read_int("Write a number into matrix"));
  371.                 columns_cycle++;
  372.             }
  373.             vec.push_back(temp);
  374.             columns_cycle = 0;
  375.             if (vec[lines_cycle].empty()) vec.erase(vec.begin() + lines_cycle);
  376.             lines_cycle++;
  377.         }
  378.     }
  379. }
  380.  
  381. void is_null(vector<vector<int>>& vec, int& count) // Long program for empty vector problem
  382. {
  383.     while (is_vector_empty(vec))
  384.     {
  385.         for (int i = 1; i <= 6; i++)
  386.         {
  387.             cout << ". ";
  388.             Sleep(333);
  389.         }
  390.         if (count == 0)
  391.         {
  392.             print_str(
  393.                 "\nOh, you deleted all elements from vector. Or just wrote 0 lines and columns,    nice job i guess :/ \n", 2, 15);
  394.             Sleep(2000);
  395.             print_str("So, since the developer is lazy, you will just need to write vector again.\n", 2, 15);
  396.             Sleep(1000);
  397.             vec_read(vec);
  398.             count++;
  399.         }
  400.         else if (count == 1)
  401.         {
  402.             print_str("\nWhy are you doing this? Stop making empty vector, it will break me!\n", 6, 15);
  403.             Sleep(1500);
  404.             print_str("I will give you one more chance to write vector.\n", 6, 15);
  405.             Sleep(500);
  406.             vec_read(vec);
  407.             count++;
  408.         }
  409.         else if (count == 2)
  410.         {
  411.             print_str("\nGood job, you made me sad for trying to break me.\n", 5, 15);
  412.             Sleep(1000);
  413.             print_str("Here, just leave me.\n", 4, 15);
  414.             Sleep(500);
  415.             vec_read(vec);
  416.             count++;
  417.         }
  418.         else if (count > 2)
  419.         {
  420.             print_str("\nI don't want to talk with you. \n", 8, 15);
  421.             Sleep(1000);
  422.             vec_read(vec);
  423.             count++;
  424.         }
  425.     }
  426. }
  427.  
  428. int main()
  429. {
  430.     print_str(
  431.         "Hello, i am AI called EXTERMINATUS-9000,this is my matrix redactor, you can add,erase lines and columns of your matrix! Also it has some fun features. And the  last thing, don't type too fast, i'm fancy, but slow.", 10, 10);
  432.     cout << endl;
  433.     int count = 0;
  434.     vector<vector<int>> matrix;
  435.     vec_read(matrix);
  436.     bool cycle = true;
  437.     is_null(matrix, count);
  438.     print(matrix);
  439.     do
  440.     {
  441.         if (cycle) print_str("Type erase/add to work with matrix or exit to exit the program: ", 15, 15);
  442.         cycle = false;
  443.         do
  444.         {
  445.             getline(cin, input);
  446.             if (check(input) == 0) print_str("Type [Erase], [Add] or [Exit] without brackets: ", 15, 15);
  447.             else if (check(input) == 3)
  448.             {
  449.                 print_str("Exiting the program", 11, 15);
  450.                 for (int k = 1; k <= 12; k++)
  451.                 {
  452.                     cout << ". ";
  453.                     Sleep(150);
  454.                     if (k % 3 == 0) print_str("\b \b\b \b\b \b\b \b\b \b\b \b", 15, 15);
  455.                     Sleep(100);
  456.                 }
  457.                 print_str("\r                    \rGoodbye!", 15, 15);
  458.                 Sleep(1000);
  459.                 return 0;
  460.             }
  461.         }
  462.         while (check(input) == 0);
  463.         choice(matrix, cycle);
  464.         is_null(matrix, count);
  465.         print(matrix);
  466.     }
  467.     while (check(input) == 0);
  468.     return 0;
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement