Skip to main content
added 37 characters in body
Source Link
Caleth
  • 1.5k
  • 9
  • 11

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Parse from left to right

It is much simpler for code to match the way that a human would do things. You are trying to simulate the left-to-right rule for addition and subtraction, but are doing it right-to-left. AllTo handle leading -, remember that all the operators that you care about evaluating follow a number, so you can start looking from the second character in the string.

idx_add_subtract_symbol = input.find_first_of("+-"sv, 1);

Use more substrings, and hold them in std::string_view when appropriate.

If, once you have identified the position of the operator you are evaluating, you split the input into before and after, it is much easier to find the numbers.

std::string_view left = input_view.substr(0, idx_add_subtract_symbol - 1);
std::string_view remaining = input_view.substr(idx_add_subtract_symbol + 1)
int next_symbol = remaining.find_first_not_of("0123456789");
std::string_view right = remaining.substr(0, next_symbol - 1);

doArithmetic should operate on std::string_views as input.

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Parse from left to right

It is much simpler for code to match the way that a human would do things. You are trying to simulate the left-to-right rule for addition and subtraction, but are doing it right-to-left. All the operators that you care about evaluating follow a number, so you can start looking from the second character in the string.

idx_add_subtract_symbol = input.find_first_of("+-"sv, 1);

Use more substrings, and hold them in std::string_view when appropriate.

If, once you have identified the position of the operator you are evaluating, you split the input into before and after, it is much easier to find the numbers.

std::string_view left = input_view.substr(0, idx_add_subtract_symbol - 1);
std::string_view remaining = input_view.substr(idx_add_subtract_symbol + 1)
int next_symbol = remaining.find_first_not_of("0123456789");
std::string_view right = remaining.substr(0, next_symbol - 1);

doArithmetic should operate on std::string_views as input.

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Parse from left to right

It is much simpler for code to match the way that a human would do things. You are trying to simulate the left-to-right rule for addition and subtraction, but are doing it right-to-left. To handle leading -, remember that all the operators that you care about evaluating follow a number, so you can start looking from the second character in the string.

idx_add_subtract_symbol = input.find_first_of("+-"sv, 1);

Use more substrings, and hold them in std::string_view when appropriate.

If, once you have identified the position of the operator you are evaluating, you split the input into before and after, it is much easier to find the numbers.

std::string_view left = input_view.substr(0, idx_add_subtract_symbol - 1);
std::string_view remaining = input_view.substr(idx_add_subtract_symbol + 1)
int next_symbol = remaining.find_first_not_of("0123456789");
std::string_view right = remaining.substr(0, next_symbol - 1);

doArithmetic should operate on std::string_views as input.

added 1123 characters in body
Source Link
Caleth
  • 1.5k
  • 9
  • 11

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Parse from left to right

It is much simpler for code to match the way that a human would do things. You are trying to simulate the left-to-right rule for addition and subtraction, but are doing it right-to-left. All the operators that you care about evaluating follow a number, so you can start looking from the second character in the string.

idx_add_subtract_symbol = input.find_first_of("+-"sv, 1);

Use more substrings, and hold them in std::string_view when appropriate.

If, once you have identified the position of the operator you are evaluating, you split the input into before and after, it is much easier to find the numbers.

std::string_view left = input_view.substr(0, idx_add_subtract_symbol - 1);
std::string_view remaining = input_view.substr(idx_add_subtract_symbol + 1)
int next_symbol = remaining.find_first_not_of("0123456789");
std::string_view right = remaining.substr(0, next_symbol - 1);

doArithmetic should operate on std::string_views as input.

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.

Parse from left to right

It is much simpler for code to match the way that a human would do things. You are trying to simulate the left-to-right rule for addition and subtraction, but are doing it right-to-left. All the operators that you care about evaluating follow a number, so you can start looking from the second character in the string.

idx_add_subtract_symbol = input.find_first_of("+-"sv, 1);

Use more substrings, and hold them in std::string_view when appropriate.

If, once you have identified the position of the operator you are evaluating, you split the input into before and after, it is much easier to find the numbers.

std::string_view left = input_view.substr(0, idx_add_subtract_symbol - 1);
std::string_view remaining = input_view.substr(idx_add_subtract_symbol + 1)
int next_symbol = remaining.find_first_not_of("0123456789");
std::string_view right = remaining.substr(0, next_symbol - 1);

doArithmetic should operate on std::string_views as input.

Source Link
Caleth
  • 1.5k
  • 9
  • 11

Use the tools the library provides

You can greatly simplify your string manipulations by using std::string::replace, e.g.

std::string inside_parentheses{input.substr(idx_open_bracket+1, idx_close_bracket-idx_open_bracket-1)};

// perform operations inside parentheses ( )
parseMultDivMod(inside_parentheses);
parseAddSubtract(inside_parentheses);

input.replace(idx_open_bracket, idx_close_bracket-idx_open_bracket, inside_parentheses);

Similarly, you can simplify argvParser by using std::regex_replace to remove any whitespace.

std::string argvParser(const int argc, char** argv) {
  std::string output;
  std::regex whitespace { "\w" };

  // parse input word by word ...
  for (auto word_index=1; word_index<argc; ++word_index)
    output.append(std::regex_replace(argv[wordindex], whitespace, ""));
  }

  return output;
}

Also note that neither operators nor numbers have uppercase, so boost::algorithm::to_upper seems superfluous.