Facebook
From Beefy Wigeon, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 263
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include "Tree.h"
  7. #include "RETURN.h"
  8. #include "Function.h"
  9.  
  10. template <class T> class User {
  11. public:
  12.         User();
  13.         ~User();
  14.  
  15.         void run();
  16.         void add_function (std::string _name, int _args_quantity, T(*_function)(T *args));
  17.  
  18. //private:
  19.         RETURN<bool> enter(std::vector<std::string> _args);
  20.         RETURN<bool> join(std::vector<std::string> _args) { RETURN<bool> R; return R; }
  21.         RETURN<std::string> vars(std::vector<std::string> _args);
  22.         RETURN<std::string> print(std::vector<std::string> _args);
  23.         RETURN<T> comp(std::vector<std::string> _args);
  24.  
  25.         bool is_number(std::string _number) const;
  26.         bool is_function(std::string _arg) const;
  27.         int find_function(std::string _arg) const;
  28.         std::vector<std::string> process_message(std::string _message) const;
  29.  
  30.         Tree<T> *tree;
  31.         std::vector<Function<T>> functions;
  32. };
  33.  
  34. template <class T> User<T>::User() :
  35.         tree(new Tree<T>()) {
  36. }
  37.  
  38. template <class T> User<T>::~User() {
  39.         delete tree;
  40. }
  41.  
  42. template <class T> void User<T>::run() {
  43.         std::string message;
  44.  
  45.         do {
  46.                 std::getline(std::cin, message);
  47.         } while (message.compare("") == 0);
  48.        
  49.         std::vector<std::string> args = process_message(message);
  50.         std::string command = args.at(0);
  51.  
  52.         if (command.compare("enter") == 0) {
  53.                 RETURN<bool> returned;
  54.                 returned = enter(args);
  55.  
  56.                 std::cout << returned.get_message();
  57.  
  58.                 if (returned.get_message().compare("") != 0) {
  59.                         std::cout << '\n';
  60.                 }
  61.         }
  62.         else if (command.compare("vars") == 0) {
  63.                 RETURN<std::string> returned;
  64.  
  65.                 returned = vars(args);
  66.                 if (!returned.is_error()) {
  67.                         std::cout << returned.get_value() << '\n';
  68.                 } else {
  69.                         std::cout << returned.get_message() << '\n';
  70.                 }
  71.  
  72.         } else if (command.compare("print") == 0) {
  73.                 RETURN<std::string> returned;
  74.  
  75.                 returned = print(args);
  76.                 if (!returned.is_error()) {
  77.                         std::cout << returned.get_value() << '\n';
  78.                 } else {
  79.                         std::cout << returned.get_message() << '\n';
  80.                 }
  81.  
  82.         } else if (command.compare("comp") == 0) {
  83.                 RETURN<T> returned;
  84.                 returned = comp(args);
  85.                 if (!returned.is_error()) {
  86.                         std::cout << returned.get_value() << '\n';
  87.                 } else {
  88.                         std::cout << returned.get_message() << '\n';
  89.                 }
  90.  
  91.         } else if (command.compare("join") == 0) {
  92.                 RETURN<bool> returned;
  93.  
  94.                 returned = join(args);
  95.                 if (!returned.is_error()) {
  96.  
  97.                 } else {
  98.                         std::cout << returned.get_message() << '\n';
  99.                 }
  100.  
  101.         } else if (command.compare("help") == 0) {
  102.  
  103.  
  104.         } else if (command.compare("quit") == 0) {
  105.  
  106.  
  107.         } else {
  108.  
  109.         }
  110. }
  111.  
  112. template <class T> RETURN<bool> User<T>::enter(std::vector<std::string> _args) {
  113.         RETURN<bool> _return(no_error);
  114.  
  115.         for (int i = 1; i < _args.size(); i++) {
  116.                 if (is_number(_args.at(i))) {
  117.                         tree->add_constant(std::stod(_args.at(i)));
  118.  
  119.                 } else if (is_function(_args.at(i))) {
  120.                         int index = find_function(_args.at(i));
  121.                         tree->add_function(functions.at(index).name, functions.at(index).args_quantity, functions.at(index).function);
  122.  
  123.                 } else {
  124.                         tree->add_variable(_args.at(i));
  125.                 }
  126.         }
  127.  
  128.         while (!tree->is_completed().get_value()) {
  129.                 tree->add_constant(1);
  130.                 _return.set_return_code(warning);
  131.                 _return.add_message("Expression not completed, added const 1\n");
  132.         }
  133.  
  134.         return _return;
  135. }
  136.  
  137. template <class T> RETURN<std::string> User<T>::vars(std::vector<std::string> _args) {
  138.         return tree->vars();
  139. }
  140.  
  141. template <class T> RETURN<std::string> User<T>::print(std::vector<std::string> _args) {
  142.         return tree->print();
  143. }
  144.  
  145. template <class T> RETURN<T> User<T>::comp(std::vector<std::string> _args)  {
  146.         RETURN<T> _return(no_error);
  147.         T *args = new T[_args.size() - 1];
  148.        
  149.         int i = 1;
  150.         while (i < _args.size() && is_number(_args.at(i))) {
  151.                 args[i - 1] = std::stod(_args.at(i));
  152.                 i++;
  153.         }
  154.  
  155.         if (i != _args.size()) {
  156.                 _return.set_return_code(error);
  157.                 _return.set_message("Invalid arguments");
  158.                 return _return;
  159.         }
  160.  
  161.         _return = tree->calculate(i - 1, args);
  162.         return _return;
  163. }
  164.  
  165.  
  166. template <class T> void User<T>::add_function(std::string _name, int _args_quantity, T(*_function)(T *args)) {
  167.         int i = 0;
  168.         while (i < functions.size() && functions.at(i).name != _name) {
  169.                 i++;
  170.         }
  171.  
  172.         if (i == functions.size()) {
  173.                 functions.push_back(Function<T>(_name, _args_quantity, _function));
  174.         }
  175. }
  176.  
  177. template <class T> bool User<T>::is_number(std::string _number) const {
  178.         char* p;
  179.         strtol(_number.c_str(), &p, 10);
  180.         return *p == 0;
  181. }
  182.  
  183. template <class T> bool User<T>::is_function(std::string _arg) const {
  184.         int i = 0;
  185.         while (i < functions.size() && _arg != functions.at(i).name) {
  186.                 i++;
  187.         }
  188.  
  189.         return i != functions.size();
  190. }
  191.  
  192. template <class T> int User<T>::find_function(std::string _arg) const {
  193.         int i = 0;
  194.         while (i < functions.size() && _arg != functions.at(i).name) {
  195.                 i++;
  196.         }
  197.  
  198.         return i;
  199. }
  200.  
  201. template <class T> std::vector<std::string> User<T>::process_message(std::string _message) const {
  202.         std::vector<std::string> args;
  203.  
  204.         _message += " ";
  205.  
  206.         std::string current_word = "";
  207.         for (int i = 0; i < _message.size(); i++) {
  208.  
  209.                 if (_message[i] != ' ') {
  210.                         current_word += _message[i];
  211.                 }
  212.                 else {
  213.                         if (current_word.size() > 0) {
  214.                                 args.push_back(current_word);
  215.                                 current_word = "";
  216.                         }
  217.                 }
  218.         }
  219.  
  220.         return args;
  221. }
  222. #pragma once
  223.  
  224. #include <iostream>
  225.  
  226. template <class T> class Function {
  227.         template <class T> friend class User;
  228. public:
  229.         class Function(std::string _name, int _args_quantity, T (*_function)(T *args));
  230.  
  231. //private:
  232.         int args_quantity;
  233.         std::string name;
  234.         T (*function)(T *args);
  235. };
  236.  
  237. template <class T> Function<T>::Function(std::string _name, int _args_quantity, T(*_function)(T *args)) :
  238.         name(_name),
  239.         args_quantity(_args_quantity),
  240.         function(_function) {
  241.         }