Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- #include <sstream>
- #include <windows.h>
- using namespace std;
- string input = "";
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- inline void print_str(string str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(str.size()); l++)
- {
- cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- }
- void read_error(bool& only_digits, bool& negative, int& dot_count)
- {
- const string null_err = "You wrote nothing, stop it";
- const string mess_err = "S-stop testing me, you baka! >/////<";
- const string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
- const string neg_dot_err = "You are not supposed to write negative fractional number here";
- const string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
- const string string_err = "This is not a number, do you think i can read it? Please";
- const string dot_err = "This number can't be fractional, you silly. Please";
- const string neg_err = "You number is way too negative";
- const string repeat = ", type again: ";
- if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 15); print_str(repeat, 13, 15); }
- else if (!only_digits && negative) { print_str(neg_str_err, 4, 15); print_str(repeat, 4, 15); }
- else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 15); print_str(repeat, 4, 15); }
- else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 15); print_str(repeat, 4, 15); }
- else if (!only_digits) { print_str(string_err, 4, 15); print_str(repeat, 4, 15); }
- else if (dot_count != 0) { print_str(dot_err, 4, 15); print_str(repeat, 4, 15); }
- else if (negative) { print_str(neg_err, 4, 15); print_str(repeat, 4, 15); }
- else if (input.length() == 0) { print_str(null_err, 4, 15); print_str(repeat, 4, 15); }
- }
- bool is_digit(char s)
- {
- if (s == '0' || s == '1' || s == '2' || s == '3' || s == '4' || s == '5' || s == '6' || s == '7' || s == '8' || s == '9') return true;
- else return false;
- }
- vector<int> read_vec_int(const string start_mes, int n) { //Checks variable for correct input (number)
- vector<int> vec;
- string input = "";
- print_str(start_mes + ": ", 15, 15);
- getline(cin, input);
- int dot_count = 0, k = 0;
- bool only_digits = true, negative = false;
- while (vec.size() != n + 1)
- {
- istringstream iss(input);
- if (input[0] == '-') { k = 1; }
- while ((!only_digits || dot_count == 0) && iss)
- {
- string temp;
- iss >> temp;
- for (int i = int(temp.length()) - 1; i >= k; i--) {
- if (!is_digit(temp[i])) {
- if (temp[i] == '.' || temp[i] == ',') dot_count += 1;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && input.length() != 0) {
- vec.push_back(strtol(temp.c_str(), NULL, 0));
- }
- else {
- read_error(only_digits, negative, dot_count);
- getline(cin, input);
- only_digits = true;
- dot_count = 0;
- k = 0;
- }
- }
- if (vec.size() != n + 1)
- {
- cout << "You wrote " << vec.size() - 1 << " numbers, you need to write " << n << " numbers, type again: ";
- vec.clear();
- getline(cin, input);
- }
- }
- vec.pop_back();
- return vec;
- }
- int read_uint(const string start_mes) { //Checks variable for correct input (number)
- print_str(start_mes + ": ", 15, 15);
- getline(cin, input);
- int dot_count = 0, k = 0;
- bool only_digits = true, negative = false;
- while (!only_digits || dot_count == 0 || input.length() == 0) {
- if (input[0] == '-') { k = 1; negative = true; }
- for (int i = input.length() - 1; i >= k; i--) {
- if (!is_digit(input[i])) {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && !negative && input.length() != 0) {
- return strtol(input.c_str(), NULL, 0);
- }
- else {
- read_error(only_digits, negative, dot_count);
- getline(cin, input);
- only_digits = true;
- dot_count = 0;
- negative = false;
- k = 0;
- }
- }
- return 0;
- }
- int read_int(const string start_mes) { //Checks variable for correct input (number)
- print_str(start_mes + ": ", 15, 15);
- getline(cin, input);
- int dot_count = 0, k = 0;
- bool only_digits = true, negative = false;
- while (!only_digits || dot_count == 0) {
- if (input[0] == '-') { k = 1; }
- for (int i = int(input.length()) - 1; i >= k; i--) {
- if (!is_digit(input[i])) {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && input.length() != 0) {
- return strtol(input.c_str(), NULL, 0);
- }
- else {
- read_error(only_digits, negative, dot_count);
- getline(cin, input);
- only_digits = true;
- dot_count = 0;
- k = 0;
- }
- }
- return 0;
- }
- void print(vector<vector<int>> vec)
- {
- unsigned int i = 0, j = 0;
- print_str("Your matrix: \n", 15, 15);
- while (i < vec.size())
- {
- while (j < vec[i].size())
- {
- cout << setw(6) << vec[i][j];
- j++;
- }
- cout << endl;
- j = 0;
- i++;
- }
- }
- int check_2(string temp) //Checks input to decide between line and column
- {
- if (temp == "Column" || temp == "column") return 1; // Number for doing stuff with column
- else if (temp == "Line" || temp == "line") return 2; // Number for doing stuff with line
- else return 0; // Number if input is incorrect
- }
- int check(string temp)
- {
- if (temp == "Erase" || temp == "erase") return 1; // Number for returning result for Erase function
- else if (temp == "Add" || temp == "add") return 2; // Number for Add function
- else if (temp == "Exit" || temp == "exit") return 3; // Number for exiting program
- else return 0; // Number if input is incorrect
- }
- int yes_no(string temp)
- {
- if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return 1; // Number for returning result for yes answer
- else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return 2; // Number for no answer
- else return 0; // Number if input is incorrect
- }
- void edit_check_line(int& n, vector<vector<int>> vec){
- do
- {
- if (n > int(vec.size())) {
- print_str("Type number that is lower or equal ", 15, 15);
- cout << vec.size();
- print_str(", the amount of lines in your matrix, ", 15, 15);
- }
- else if (n == 0)
- {
- print_str("You wrote 0, did you mean first element? [Yes/No]: ", 15, 15);
- do
- {
- getline(cin, input);
- if (yes_no(input) == 0) print_str("Type [Yes/No]: ", 15, 15);
- } while (yes_no(input) == 0);
- if (yes_no(input) == 1) n = 1;
- }
- if (n == 0 || n > int(vec.size())) n = read_uint("type again");
- } while (n > int(vec.size()) || n == 0);
- }
- void edit_check_column(int& n, vector<vector<int>> vec) {
- do
- {
- if (n > int(vec[0].size())) {
- print_str("Type number that is lower or equal ", 15, 15);
- cout << vec[0].size();
- print_str(", the amount of columns in your matrix, ", 15, 15);
- }
- else if (n == 0)
- {
- print_str("You wrote 0, did you mean first element? [Yes/No]: ", 15, 15);
- do
- {
- getline(cin, input);
- if (yes_no(input) == 0) print_str("Type [Yes/No]: ", 15, 15);
- } while (yes_no(input) == 0);
- if (yes_no(input) == 1) n = 1;
- }
- if (n == 0 || n > int(vec[0].size())) n = read_uint("type again");
- } while (n > int(vec[0].size()) || n == 0);
- }
- void erase_line(vector<vector<int>>& vec)
- {
- int n = read_uint("What line do you want to erase?");
- edit_check_line(n, vec);
- int lines = n - 1;
- while (lines < int(vec.size()) - 1)
- {
- vec[lines] = vec[lines + 1];
- lines++;
- }
- vec.pop_back();
- }
- void erase_column(vector<vector<int>>& vec)
- {
- int n = read_uint("What column do you want to erase?"), lines = 0;
- edit_check_column(n, vec);
- int column = n - 1;
- while (lines < int(vec.size()))
- {
- while (column < int(vec[lines].size()) - 1)
- {
- vec[lines][column] = vec[lines][column + 1];
- column++;
- }
- column = n - 1;
- lines++;
- }
- lines = 0;
- while (lines < int(vec.size()))
- {
- vec[lines].pop_back();
- lines++;
- }
- }
- void add_line(vector<vector<int>>& vec)
- {
- int n = read_uint("Where do you want to add line?"), i = 1;
- vec.push_back(vec[vec.size() - i]);
- edit_check_line(n, vec);
- vector<int> vec_2 = read_vec_int("Write the new numbers", vec[0].size());
- i++;
- while ( int(vec.size()) - i >= n-1)
- {
- vec[vec.size() - i + 1] = vec[vec.size() - i];
- i++;
- }
- vec[n - 1] = vec_2;
- }
- void add_column(vector<vector<int>>& vec)
- {
- int n = read_uint("Where do you want to add column?"), lines = 0;
- while (lines < int(vec.size()))
- {
- vec[lines].push_back(vec[lines][vec[lines].size() - 1]);
- lines++;
- }
- lines = 0;
- int column = vec[0].size() - 2;
- edit_check_column(n, vec);
- vector<int> vec_2 = read_vec_int("Write the new numbers", vec.size());
- while (lines < int(vec.size()))
- {
- while (column >= n - 1)
- {
- vec[lines][column + 1] = vec[lines][column];
- column--;
- }
- column = vec[0].size() - 2;
- lines++;
- }
- lines = 0;
- while (lines < int(vec.size()))
- {
- vec[lines][n - 1] = vec_2[lines];
- lines++;
- }
- }
- void choice(vector<vector<int>>& vec, bool& cycle) //Function for choosing path of actions
- {
- if (check(input) == 1) //Path if user decided to erase
- {
- print_str("Do you want to erase line or column? ", 15, 15);
- do
- {
- getline(cin, input);
- if (check_2(input) == 0) print_str("Type [Line] or [Column] without brackets: ", 15, 15);
- } while (check_2(input) == 0);
- if (check_2(input) == 1) erase_column(vec);
- else if (check_2(input) == 2) erase_line(vec);
- }
- else if (check(input) == 2) //Path if user decided to add
- {
- print_str("Do you want to add line or column? ", 15, 15);
- do
- {
- getline(cin, input);
- if (check_2(input) == 0) print_str("Type [Line] or [Column] without brackets: ", 15, 15);
- } while (check_2(input) == 0);
- if (check_2(input) == 1) add_column(vec);
- else if (check_2(input) == 2) add_line(vec);
- }
- cycle = true; //Variable to control out message
- }
- bool is_vector_empty(vector<vector<int>>& vec) //Checks if vector is empty based on his size
- {
- bool is_line_empty = true, is_column_empty = true;
- int j = 0;
- while (j < int(vec.size()))
- {
- if (vec[j].size() != 0) is_column_empty = false;
- j++;
- }
- if (!vec.empty()) is_line_empty = false;
- if (is_line_empty == false && is_column_empty == false) return false;
- else return true;
- }
- void vec_read(vector<vector<int>>& vec) //Filling vector from users input
- {
- const int lines = read_uint("Write number of lines of your matrix"), columns = read_uint(
- "Write number of columns of your matrix");;
- int lines_cycle = 0, columns_cycle = 0;
- if (lines != 0 && columns != 0) //If columns or lines equal to zero, it doesn't fill matrix
- {
- while (lines_cycle < lines)
- {
- vector<int> temp;
- while (columns_cycle < columns)
- {
- cout << "[" << lines_cycle + 1 << "]" << "[" << columns_cycle + 1 << "] "; //Prints current matrix coords
- temp.push_back(read_int("Write a number into matrix"));
- columns_cycle++;
- }
- vec.push_back(temp);
- columns_cycle = 0;
- if (vec[lines_cycle].empty()) vec.erase(vec.begin() + lines_cycle);
- lines_cycle++;
- }
- }
- }
- void is_null(vector<vector<int>>& vec, int& count) // Long program for empty vector problem
- {
- while (is_vector_empty(vec))
- {
- for (int i = 1; i <= 6; i++)
- {
- cout << ". ";
- Sleep(333);
- }
- if (count == 0)
- {
- print_str(
- "\nOh, you deleted all elements from vector. Or just wrote 0 lines and columns, nice job i guess :/ \n", 2, 15);
- Sleep(2000);
- print_str("So, since the developer is lazy, you will just need to write vector again.\n", 2, 15);
- Sleep(1000);
- vec_read(vec);
- count++;
- }
- else if (count == 1)
- {
- print_str("\nWhy are you doing this? Stop making empty vector, it will break me!\n", 6, 15);
- Sleep(1500);
- print_str("I will give you one more chance to write vector.\n", 6, 15);
- Sleep(500);
- vec_read(vec);
- count++;
- }
- else if (count == 2)
- {
- print_str("\nGood job, you made me sad for trying to break me.\n", 5, 15);
- Sleep(1000);
- print_str("Here, just leave me.\n", 4, 15);
- Sleep(500);
- vec_read(vec);
- count++;
- }
- else if (count > 2)
- {
- print_str("\nI don't want to talk with you. \n", 8, 15);
- Sleep(1000);
- vec_read(vec);
- count++;
- }
- }
- }
- int main()
- {
- print_str(
- "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);
- cout << endl;
- int count = 0;
- vector<vector<int>> matrix;
- vec_read(matrix);
- bool cycle = true;
- is_null(matrix, count);
- print(matrix);
- do
- {
- if (cycle) print_str("Type erase/add to work with matrix or exit to exit the program: ", 15, 15);
- cycle = false;
- do
- {
- getline(cin, input);
- if (check(input) == 0) print_str("Type [Erase], [Add] or [Exit] without brackets: ", 15, 15);
- else if (check(input) == 3)
- {
- print_str("Exiting the program", 11, 15);
- for (int k = 1; k <= 12; k++)
- {
- cout << ". ";
- Sleep(150);
- if (k % 3 == 0) print_str("\b \b\b \b\b \b\b \b\b \b\b \b", 15, 15);
- Sleep(100);
- }
- print_str("\r \rGoodbye!", 15, 15);
- Sleep(1000);
- return 0;
- }
- }
- while (check(input) == 0);
- choice(matrix, cycle);
- is_null(matrix, count);
- print(matrix);
- }
- while (check(input) == 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement