fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <vector>
  6. #include <cmath>
  7.  
  8. //disables any deprecation warning
  9. #pragma warning(disable : 4996)
  10.  
  11. //usings
  12. using std::vector;
  13. using std::string;
  14. using std::cout;
  15. using std::endl;
  16. using std::stringstream;
  17.  
  18. bool try_parse(const std::string& s)
  19. {
  20. char* end = 0;
  21. double val = strtod(s.c_str(), &end);
  22. return end != s.c_str() && val != HUGE_VAL;
  23. }
  24.  
  25. char first_char(string str) {
  26. return *str.c_str();
  27. }
  28.  
  29.  
  30. vector<string> tok_type(vector<string> vec) {
  31. for (int i = 0; i < vec.size(); i++) {
  32. string &s = vec[i];
  33. bool doubly = try_parse(s);
  34. bool amp = first_char(s) == '&';
  35. if (!doubly && !amp) {
  36. s = "<unknown> " + s;
  37. continue;
  38. }
  39. else if (!doubly && amp) {
  40. s = "<operator> " + s;
  41. continue;
  42. }
  43. else if (doubly && !amp) {
  44. s = "<double> " + s;
  45. continue;
  46. }
  47. }
  48. return vec;
  49. }
  50.  
  51. long double parse(string str) {
  52. return std::stold(str);
  53. }
  54.  
  55. vector<string> split(string str, string token = " ") {
  56. vector<string>result;
  57. while (str.size()) {
  58. int index = str.find(token);
  59. if (index != string::npos) {
  60. result.push_back(str.substr(0, index));
  61. str = str.substr(index + token.size());
  62. if (str.size() == 0)result.push_back(str);
  63. }
  64. else {
  65. result.push_back(str);
  66. str = "";
  67. }
  68. }
  69. return result;
  70. }
  71.  
  72. string simplify(string expr) {
  73. string iexpr = expr;
  74. for (int i = 0; i < iexpr.length(); i++) {
  75.  
  76. char& c = iexpr[i];
  77.  
  78. if (c == '+')
  79. iexpr.replace(i, 1, " &ad ");
  80. else if (c == '-')
  81. iexpr.replace(i, 1, " &sb ");
  82. else if (c == '*')
  83. iexpr.replace(i, 1, " &mp ");
  84. else if (c == '/')
  85. iexpr.replace(i, 1, " &dv ");
  86.  
  87. }
  88. return iexpr;
  89. }
  90.  
  91. int main() {
  92. vector<string> sep_rep = tok_type(split(simplify("21+32-3*2")));
  93. for (auto str : sep_rep) {
  94. cout << str << endl;
  95. }
  96.  
  97. std::cin.get();
  98. return 0;
  99. }
Success #stdin #stdout 0s 4272KB
stdin
Standard input is empty
stdout
<double> 21
<operator> &ad
<double> 32
<operator> &sb
<double> 3
<operator> &mp
<double> 2