- #pragma once
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include "Tree.h"
- #include "RETURN.h"
- #include "Function.h"
- template <class T> class User {
- public:
- User();
- ~User();
- void run();
- void add_function (std::string _name, int _args_quantity, T(*_function)(T *args));
- //private:
- RETURN<bool> enter(std::vector<std::string> _args);
- RETURN<bool> join(std::vector<std::string> _args) { RETURN<bool> R; return R; }
- RETURN<std::string> vars(std::vector<std::string> _args);
- RETURN<std::string> print(std::vector<std::string> _args);
- RETURN<T> comp(std::vector<std::string> _args);
- bool is_number(std::string _number) const;
- bool is_function(std::string _arg) const;
- int find_function(std::string _arg) const;
- std::vector<std::string> process_message(std::string _message) const;
- Tree<T> *tree;
- std::vector<Function<T>> functions;
- };
- template <class T> User<T>::User() :
- tree(new Tree<T>()) {
- }
- template <class T> User<T>::~User() {
- delete tree;
- }
- template <class T> void User<T>::run() {
- std::string message;
- do {
- std::getline(std::cin, message);
- } while (message.compare("") == 0);
- std::vector<std::string> args = process_message(message);
- std::string command = args.at(0);
- if (command.compare("enter") == 0) {
- RETURN<bool> returned;
- returned = enter(args);
- std::cout << returned.get_message();
- if (returned.get_message().compare("") != 0) {
- std::cout << '\n';
- }
- }
- else if (command.compare("vars") == 0) {
- RETURN<std::string> returned;
- returned = vars(args);
- if (!returned.is_error()) {
- std::cout << returned.get_value() << '\n';
- } else {
- std::cout << returned.get_message() << '\n';
- }
- } else if (command.compare("print") == 0) {
- RETURN<std::string> returned;
- returned = print(args);
- if (!returned.is_error()) {
- std::cout << returned.get_value() << '\n';
- } else {
- std::cout << returned.get_message() << '\n';
- }
- } else if (command.compare("comp") == 0) {
- RETURN<T> returned;
- returned = comp(args);
- if (!returned.is_error()) {
- std::cout << returned.get_value() << '\n';
- } else {
- std::cout << returned.get_message() << '\n';
- }
- } else if (command.compare("join") == 0) {
- RETURN<bool> returned;
- returned = join(args);
- if (!returned.is_error()) {
- } else {
- std::cout << returned.get_message() << '\n';
- }
- } else if (command.compare("help") == 0) {
- } else if (command.compare("quit") == 0) {
- } else {
- }
- }
- template <class T> RETURN<bool> User<T>::enter(std::vector<std::string> _args) {
- RETURN<bool> _return(no_error);
- for (int i = 1; i < _args.size(); i++) {
- if (is_number(_args.at(i))) {
- tree->add_constant(std::stod(_args.at(i)));
- } else if (is_function(_args.at(i))) {
- int index = find_function(_args.at(i));
- tree->add_function(functions.at(index).name, functions.at(index).args_quantity, functions.at(index).function);
- } else {
- tree->add_variable(_args.at(i));
- }
- }
- while (!tree->is_completed().get_value()) {
- tree->add_constant(1);
- _return.set_return_code(warning);
- _return.add_message("Expression not completed, added const 1\n");
- }
- return _return;
- }
- template <class T> RETURN<std::string> User<T>::vars(std::vector<std::string> _args) {
- return tree->vars();
- }
- template <class T> RETURN<std::string> User<T>::print(std::vector<std::string> _args) {
- return tree->print();
- }
- template <class T> RETURN<T> User<T>::comp(std::vector<std::string> _args) {
- RETURN<T> _return(no_error);
- T *args = new T[_args.size() - 1];
- int i = 1;
- while (i < _args.size() && is_number(_args.at(i))) {
- args[i - 1] = std::stod(_args.at(i));
- i++;
- }
- if (i != _args.size()) {
- _return.set_return_code(error);
- _return.set_message("Invalid arguments");
- return _return;
- }
- _return = tree->calculate(i - 1, args);
- return _return;
- }
- template <class T> void User<T>::add_function(std::string _name, int _args_quantity, T(*_function)(T *args)) {
- int i = 0;
- while (i < functions.size() && functions.at(i).name != _name) {
- i++;
- }
- if (i == functions.size()) {
- functions.push_back(Function<T>(_name, _args_quantity, _function));
- }
- }
- template <class T> bool User<T>::is_number(std::string _number) const {
- char* p;
- strtol(_number.c_str(), &p, 10);
- return *p == 0;
- }
- template <class T> bool User<T>::is_function(std::string _arg) const {
- int i = 0;
- while (i < functions.size() && _arg != functions.at(i).name) {
- i++;
- }
- return i != functions.size();
- }
- template <class T> int User<T>::find_function(std::string _arg) const {
- int i = 0;
- while (i < functions.size() && _arg != functions.at(i).name) {
- i++;
- }
- return i;
- }
- template <class T> std::vector<std::string> User<T>::process_message(std::string _message) const {
- std::vector<std::string> args;
- _message += " ";
- std::string current_word = "";
- for (int i = 0; i < _message.size(); i++) {
- if (_message[i] != ' ') {
- current_word += _message[i];
- }
- else {
- if (current_word.size() > 0) {
- args.push_back(current_word);
- current_word = "";
- }
- }
- }
- return args;
- }
- #pragma once
- #include <iostream>
- template <class T> class Function {
- template <class T> friend class User;
- public:
- class Function(std::string _name, int _args_quantity, T (*_function)(T *args));
- //private:
- int args_quantity;
- std::string name;
- T (*function)(T *args);
- };
- template <class T> Function<T>::Function(std::string _name, int _args_quantity, T(*_function)(T *args)) :
- name(_name),
- args_quantity(_args_quantity),
- function(_function) {
- }