Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "helpers.h"
- #include "item.h"
- #include <iostream>
- #include <memory>
- #include <regex>
- #include <string>
- const auto valid_regex = std::regex("^(0|-?[1-9][0-9]*|[A-Za-z][0-9A-Z_a-z]*)$");
- bool is_valid_string(const std::string& str) {
- return std::regex_match(str, valid_regex);
- }
- int compare_digits(const std::string& str, const std::string& oth);
- bool insert_before(const std::string& val, const Item<std::string>& oth) {
- int digitcmp = compare_digits(val, oth.value);
- return digitcmp < 0 ? true : (digitcmp > 0 ? false : val <= oth.value);
- }
- bool value_equal(const Item<std::string>& item, const std::string& val) {
- return item.value == val;
- }
- int main() {
- std::unique_ptr<Item<std::string>> start;
- bool begin = true;
- std::string input;
- while (true) {
- if (!begin) {
- std::cout << "\n";
- }
- else {
- begin = false;
- }
- std::cout << "Awaiting input...\n";
- std::getline(std::cin, input);
- if (input.length() == 0) {
- remove_all(start);
- std::cout << "\nProgram terminated!\n";
- return 0;
- }
- else if (input.at(0) == '~') {
- if (input.length() == 1) {
- std::cout << "\nDeleting list...\n";
- remove_all(start);
- }
- else {
- input = input.substr(1);
- if (is_valid_string(input)) {
- std::cout << "\nRemoving item...\n";
- remove_item<std::string>(&start, input, &value_equal);
- }
- else {
- std::cout << "\nCould not parse input!\n";
- }
- }
- }
- else if (input == "l") {
- std::cout << "\nList print...\n";
- print_list(start.get());
- }
- else if (input == "i") {
- std::cout << "\nIterator print...\n";
- print_iterator(start.get());
- }
- else if (input == "a") {
- std::cout << "\nArray print...\n";
- print_array(start.get());
- }
- else if (input == "r") {
- std::cout << "\nRecursive print...\n";
- print_recursive(start.get());
- }
- else if (is_valid_string(input)) {
- std::cout << "\nInserting item...\n";
- insert_item<std::string>(&start, input, &insert_before);
- }
- else {
- std::cout << "\nCould not parse input!\n";
- }
- }
- }
- int compare_digits(const std::string& str, const std::string& oth) {
- size_t strlen = str.length(), othlen = oth.length();
- if (strlen == 0 || othlen == 0) {
- return -1;
- }
- char str_char = str.at(0), oth_char = oth.at(0);
- bool str_minus = str_char == '-', oth_minus = oth_char == '-';
- if (str_minus ^ oth_minus) {
- return str_minus ? -1 : 1;
- }
- else if (!str_minus && (!isdigit(str_char) || !isdigit(oth_char))) {
- return 0;
- }
- else if (strlen > othlen) {
- return str_minus ? -1 : 1;
- }
- else if (strlen < othlen) {
- return str_minus ? 1 : -1;
- }
- for (size_t i = str_minus ? 1 : 0; i < strlen; ++i) {
- str_char = str.at(i);
- oth_char = oth.at(i);
- if (str_char > oth_char) {
- return str_minus ? -1 : 1;
- }
- else if (str_char < oth_char) {
- return str_minus ? 1 : -1;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement