Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include "helpers.h"
  2. #include "item.h"
  3.  
  4. #include <iostream>
  5. #include <memory>
  6. #include <regex>
  7. #include <string>
  8.  
  9. const auto valid_regex = std::regex("^(0|-?[1-9][0-9]*|[A-Za-z][0-9A-Z_a-z]*)$");
  10.  
  11. bool is_valid_string(const std::string& str) {
  12.     return std::regex_match(str, valid_regex);
  13. }
  14.  
  15. int compare_digits(const std::string& str, const std::string& oth);
  16.  
  17. bool insert_before(const std::string& val, const Item<std::string>& oth) {
  18.     int digitcmp = compare_digits(val, oth.value);
  19.     return digitcmp < 0 ? true : (digitcmp > 0 ? false : val <= oth.value);
  20. }
  21.  
  22. bool value_equal(const Item<std::string>& item, const std::string& val) {
  23.     return item.value == val;
  24. }
  25.  
  26. int main() {
  27.     std::unique_ptr<Item<std::string>> start;
  28.  
  29.     bool begin = true;
  30.     std::string input;
  31.  
  32.     while (true) {
  33.         if (!begin) {
  34.             std::cout << "\n";
  35.         }
  36.         else {
  37.             begin = false;
  38.         }
  39.        
  40.         std::cout << "Awaiting input...\n";
  41.         std::getline(std::cin, input);
  42.  
  43.         if (input.length() == 0) {
  44.             remove_all(start);
  45.             std::cout << "\nProgram terminated!\n";
  46.             return 0;
  47.         }
  48.         else if (input.at(0) == '~') {
  49.             if (input.length() == 1) {
  50.                 std::cout << "\nDeleting list...\n";
  51.                 remove_all(start);
  52.             }
  53.             else {
  54.                 input = input.substr(1);
  55.                 if (is_valid_string(input)) {
  56.                     std::cout << "\nRemoving item...\n";
  57.                     remove_item<std::string>(&start, input, &value_equal);
  58.                 }
  59.                 else {
  60.                     std::cout << "\nCould not parse input!\n";
  61.                 }
  62.             }
  63.         }
  64.         else if (input == "l") {
  65.             std::cout << "\nList print...\n";
  66.             print_list(start.get());
  67.         }
  68.         else if (input == "i") {
  69.             std::cout << "\nIterator print...\n";
  70.             print_iterator(start.get());
  71.         }
  72.         else if (input == "a") {
  73.             std::cout << "\nArray print...\n";
  74.             print_array(start.get());
  75.         }
  76.         else if (input == "r") {
  77.             std::cout << "\nRecursive print...\n";
  78.             print_recursive(start.get());
  79.         }
  80.         else if (is_valid_string(input)) {
  81.             std::cout << "\nInserting item...\n";
  82.             insert_item<std::string>(&start, input, &insert_before);
  83.         }
  84.         else {
  85.             std::cout << "\nCould not parse input!\n";
  86.         }
  87.     }
  88. }
  89.  
  90. int compare_digits(const std::string& str, const std::string& oth) {
  91.     size_t strlen = str.length(), othlen = oth.length();
  92.     if (strlen == 0 || othlen == 0) {
  93.         return -1;
  94.     }
  95.  
  96.     char str_char = str.at(0), oth_char = oth.at(0);
  97.     bool str_minus = str_char == '-', oth_minus = oth_char == '-';
  98.     if (str_minus ^ oth_minus) {
  99.         return str_minus ? -1 : 1;
  100.     }
  101.     else if (!str_minus && (!isdigit(str_char) || !isdigit(oth_char))) {
  102.         return 0;
  103.     }
  104.     else if (strlen > othlen) {
  105.         return str_minus ? -1 : 1;
  106.     }
  107.     else if (strlen < othlen) {
  108.         return str_minus ? 1 : -1;
  109.     }
  110.  
  111.     for (size_t i = str_minus ? 1 : 0; i < strlen; ++i) {
  112.         str_char = str.at(i);
  113.         oth_char = oth.at(i);
  114.         if (str_char > oth_char) {
  115.             return str_minus ? -1 : 1;
  116.         }
  117.         else if (str_char < oth_char) {
  118.             return str_minus ? 1 : -1;
  119.         }
  120.     }
  121.  
  122.     return -1;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement