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 = "";
- 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) { cout << mess_err << repeat; }
- else if (!only_digits && negative) { cout << neg_str_err << repeat; }
- else if (!only_digits && dot_count != 0) { cout << dot_str_err << repeat; }
- else if (dot_count != 0 && negative) { cout << neg_dot_err << repeat; }
- else if (!only_digits) { cout << string_err << repeat; }
- else if (dot_count != 0) { cout << dot_err << repeat; }
- else if (negative) { cout << neg_err << repeat; }
- else if (input.length() == 0) { cout << null_err << repeat; }
- }
- 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 = "";
- cout << start_mes << ": ";
- 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)
- cout << start_mes << ": ";
- 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)
- cout << start_mes << ": ";
- 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;
- cout << "Your matrix: \n";
- 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 > signed int(vec.size())) {
- cout << "Type number that is lower or equal " << vec.size() << ", the amount of lines in your matrix, ";
- }
- else if (n == 0)
- {
- cout << "You wrote 0, did you mean first element? [Yes/No]: ";
- do
- {
- getline(cin, input);
- if (yes_no(input) == 0) cout << "Type [Yes/No]: ";
- } while (yes_no(input) == 0);
- if (yes_no(input) == 1) n = 1;
- }
- if (n == 0 || n > signed int(vec.size())) n = read_uint("type again");
- } while (n > signed int(vec.size()) || n == 0);
- }
- void edit_check_column(int& n, vector<vector<int>> vec) {
- do
- {
- if (n > signed int(vec[0].size())) {
- cout << "Type number that is lower or equal " << vec[0].size() << ", the amount of columns in your matrix, ";
- }
- else if (n == 0)
- {
- cout << "You wrote 0, did you mean first element? [Yes/No]: ";
- do
- {
- getline(cin, input);
- if (yes_no(input) == 0) cout << "Type [Yes/No]: ";
- } while (yes_no(input) == 0);
- if (yes_no(input) == 1) n = 1;
- }
- if (n == 0 || n > signed int(vec[0].size())) n = read_uint("type again");
- } while (n > signed 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 < signed 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 < signed int(vec.size()))
- {
- while (column < signed int(vec[lines].size()) - 1)
- {
- vec[lines][column] = vec[lines][column + 1];
- column++;
- }
- column = n - 1;
- lines++;
- }
- lines = 0;
- while (lines < signed 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 (signed 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 < signed 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 < signed 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 < signed int(vec.size()))
- {
- vec[lines][n - 1] = vec_2[lines];
- lines++;
- }
- }
- void choice(vector<vector<int>>& vec, bool& cycle)
- {
- if (check(input) == 1)
- {
- cout << "Do you want to erase line or column? ";
- do
- {
- getline(cin, input);
- if (check_2(input) == 0) cout << "Type [Line] or [Column] without brackets: ";
- } 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)
- {
- cout << "Do you want to add line or column? ";
- do
- {
- getline(cin, input);
- if (check_2(input) == 0) cout << "Type [Line] or [Column] without brackets: ";
- } while (check_2(input) == 0);
- if (check_2(input) == 1) add_column(vec);
- else if (check_2(input) == 2) add_line(vec);
- }
- cycle = true;
- }
- bool is_vector_empty(vector<vector<int>> vec)
- {
- bool is_line_empty = true, is_column_empty = true;
- int j = 0;
- while (j < signed 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)
- {
- int lines, columns;
- 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)
- {
- while (lines_cycle < lines)
- {
- vector<int> temp;
- while (columns_cycle < columns)
- {
- 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)
- {
- while (is_vector_empty(vec))
- {
- for (int i = 1; i <= 6; i++)
- {
- cout << ". ";
- Sleep(333);
- }
- if (count == 0)
- {
- cout << endl <<
- "Oh, you deleted all elements from vector. Or just wrote 0 lines and columns, nice job i guess :/"
- << endl;
- Sleep(2500);
- cout <<
- "So, since the developer is lazy, you will just need to write vector again."
- << endl;
- Sleep(1500);
- vec_read(vec);
- count++;
- }
- else if (count == 1)
- {
- cout << endl << "Why are you doing this? Stop making empty vector, it will break me!" << endl;
- Sleep(3000);
- cout << "I will give you one more chance to write vector." << endl;
- Sleep(2000);
- vec_read(vec);
- count++;
- }
- else if (count == 2)
- {
- cout << endl << "Good job, you made me sad for trying to break me." << endl;
- Sleep(2000);
- cout << "Here, just leave me." << endl;
- Sleep(1000);
- vec_read(vec);
- count++;
- }
- else if (count > 2)
- {
- cout << endl << "I don't want to talk with you." << endl;
- Sleep(1000);
- vec_read(vec);
- count++;
- }
- }
- }
- int main()
- {
- int count = 0;
- vector<vector<int>> matrix;
- vec_read(matrix);
- bool cycle = true;
- is_null(matrix, count);
- print(matrix);
- do
- {
- if (cycle) cout << "Type erase/add to work with matrix or exit to exit the program: ";
- cycle = false;
- do
- {
- getline(cin, input);
- if (check(input) == 0) cout << "Type [Erase], [Add] or [Exit] without brackets: ";
- else if (check(input) == 3)
- {
- cout << "Exiting the program";
- for (int k = 1; k <= 12; k++)
- {
- cout << ". ";
- Sleep(150);
- if (k % 3 == 0) cout << "\b \b\b \b\b \b\b \b\b \b\b \b";
- Sleep(100);
- }
- cout << "\r \r" << "Goodbye!";
- 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