Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- bool isDigit(const string& str)
- {
- return all_of(str.begin(), str.end(), ::isdigit);
- }
- string getIntegerFromExpression(string str) {
- bool hasFloatingPoint = false, hasExp = false, hasDigit = false;
- string output;
- for (string::iterator it = str.begin(); it != str.end(); ++it) {
- const string tkn = string(1, *it);
- if (isDigit(tkn) || (tkn == "E" && !hasExp) || (tkn == "." && !hasFloatingPoint)) {
- if (tkn == "E") hasExp = true;
- if (tkn == ".") hasFloatingPoint = true;
- if (isDigit(tkn)) hasDigit = true;
- output += tkn;
- }
- else if (hasDigit) {
- break;
- }
- }
- if (hasDigit || hasExp) {
- return output;
- }
- return "";
- }
- int main() {
- string a = "4E53GGGH";
- string b = "3.4E53=B";
- cout << getIntegerFromExpression(a) << endl;
- cout << getIntegerFromExpression(b) << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement