Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. bool isDigit(const string& str)
  8. {
  9.     return all_of(str.begin(), str.end(), ::isdigit);
  10. }
  11.  
  12.  
  13. string getIntegerFromExpression(string str) {
  14.     bool hasFloatingPoint = false, hasExp = false, hasDigit = false;
  15.     string output;
  16.  
  17.     for (string::iterator it = str.begin(); it != str.end(); ++it) {
  18.         const string tkn = string(1, *it);
  19.         if (isDigit(tkn) || (tkn == "E" && !hasExp) || (tkn == "." && !hasFloatingPoint)) {
  20.             if (tkn == "E") hasExp = true;
  21.             if (tkn == ".") hasFloatingPoint = true;
  22.             if (isDigit(tkn)) hasDigit = true;
  23.             output += tkn;
  24.         }
  25.         else if (hasDigit) {
  26.             break;
  27.         }
  28.     }
  29.     if (hasDigit || hasExp) {
  30.         return output;
  31.     }
  32.     return "";
  33. }
  34.  
  35. int main() {
  36.  
  37.  
  38.  
  39.     string a = "4E53GGGH";
  40.     string b = "3.4E53=B";
  41.     cout << getIntegerFromExpression(a) << endl;
  42.     cout << getIntegerFromExpression(b) << endl;
  43.  
  44.     system("pause");
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement