Advertisement
Mike_be

Gaidel lab

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