Привести функции в более читаемый вид
Написал перевод в польскую нотацию, тут только часть кода с конкретными функциями. Глобальные чисто, чтобы побыстрее рабочую версию части кода сделать (в основном коде это приватные переменные класса).
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
std::vector<std::string> polish_;
std::vector<std::string> vector_of_str_ = {"2","+","3","/","4"};
bool error_ = false;
bool MaxNumber(std::string &str) {
double max_number = 900719925474099;
bool return_value = false;
if (str[0] >= '0' && str[0] <='9' && stod(str) > max_number) {
return_value = true;
}
return return_value;
}
void ComparePriority(std::stack<std::pair<std::string, int>> &symbol_stack, std::string ¤t_symbol,
const std::string &operation, int priority, int &check_if_pushed) {
if (!current_symbol.compare(operation) && check_if_pushed == false) {
while (symbol_stack.size() > 0 && symbol_stack.top().second >= priority) {
polish_.push_back(symbol_stack.top().first);
symbol_stack.pop();
}
check_if_pushed = true;
symbol_stack.push({current_symbol, priority});
} else {
check_if_pushed = false;
}
}
void ConvertToPolishNotation() {
std::map<std::string, int> oper_with_prior = {{"-",1}, {"+",1}, {"*",2}, {"/",2}, {"^",4}, {"~",3},
{"cos",5}, {"sin",5}, {"tg",5}, {"abs",5}, {"ln",5}, {"sqrt",5},
{"acos",5}, {"asin",5}, {"atan",5}, {"log",5}, {"%",2}};
int str_len = vector_of_str_.size();
int i = 0, left_brackets_counter = 0, check_if_pushed = false;
std::stack<std::pair<std::string, int>> symbol_stack;
while (str_len && error_ == false) {
if (MaxNumber(vector_of_str_[i])) {
error_ = true;
} else {
--str_len;
if (vector_of_str_[i].compare("(") == 0) {
symbol_stack.push({vector_of_str_[i], 0});
++left_brackets_counter;
check_if_pushed = true;
} else if (vector_of_str_[i].compare(")") == 0) {
if (left_brackets_counter == 0) {
error_ = true;
} else {
while (symbol_stack.top().first.compare("(") != 0) {
polish_.push_back(symbol_stack.top().first);
symbol_stack.pop();
}
check_if_pushed = true;
symbol_stack.pop();
}
--left_brackets_counter;
} else {
for (std::map<std::string, int>::iterator it = oper_with_prior.begin(); it != oper_with_prior.end() && check_if_pushed != true; ++it) {
ComparePriority(symbol_stack, vector_of_str_[i], it->first, it->second, check_if_pushed);
}
}
if (error_ == false) {
if (check_if_pushed == false) {
polish_.push_back(vector_of_str_[i]);
}
check_if_pushed = false;
++i;
}
}
}
while (symbol_stack.size() != 0) {
polish_.push_back(symbol_stack.top().first);
symbol_stack.pop();
}
if (left_brackets_counter > 0) {
error_ = true;
}
}
int main() {
ConvertToPolishNotation();
for (int i = 0; i < polish_.size(); i++) {
std::cout<<polish_[i];
}
return 0;
}
Функции MaxNumber, ComparePriority, ConvertToPolishNotation
. Можно их как-то причесать, чтобы стало более понятно и читабельно? Мне то самому вроде и все понятно и читабельно, но вот препод сказал, что не очень и надо что-то исправить в этом плане.
Источник: Stack Overflow на русском