#include #include #include #include #include #include int cnts(string str, char ch)//counts the occurances of a specified characters in a string { int n = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == ch) { n++; } } return n; } int stoe(string str)//string to integer if the first character is a digit, trailing non-digits and digits thereafter are ignored, otherwise returns -1 { string buf; for (int i = 0; i < str.size(); i++) { if (isdigit(str[i]) != 0) { buf.push_back(str[i]); } else { break; } } if (buf.size() > 0) { return stoi(buf); } else { return -1; } } double stoE(string str)//string to double if all characters constitute a single number, otherwise returns -1 { string buf; for (int i = 0; i < str.size(); i++) { if (isdigit(str[i]) != 0) { buf.push_back(str[i]); } else { break; } } if (buf.size() == str.size() && buf.size() > 0) { return stod(buf); } else { return -1; } } int stoiv(string str)//string to integer value, trailing non-digits and digits thereafter are ignored, returns -1 if empty { string buf; int i; for (i = 0; i < str.size(); i++) { if (isdigit(str[i]) != 0) { break; } } for (int j = i; j < str.size(); j++) { if (isdigit(str[j]) != 0) { buf.push_back(str[j]); } else { break; } } if (buf.size() > 0) { return stoi(buf); } else { return -1; } } int par(string str)//returns 1 if the first sign is +, -1 if it is -, and 0 if neither is found { for (int i = 0; i < str.size(); i++) { if (str[i] == '+') { return 1; } if (str[i] == '-') { return -1; } } return 0; } double stodv(string str)//string to double value; captures the first number in the string, trailing non-digits and numbers are ignored, returns -1 if empty { string buf; int i; for (i = 0; i < str.size(); i++) { if (isdigit(str[i]) != 0) { break; } } for (int j = i; j < str.size(); j++) { if (isdigit(str[j]) != 0 || str[j] == '.') { buf.push_back(str[j]); } else { break; } } if (buf.size() > 0) { return stod(buf); } else { return -1; } } int level(string str)//confidence level in the sign; returns 2 is the sign is without brackets, 1 if in brackets, 0 is no sign or digit is found { for (int i = 0; i < str.size(); i++) { if (isdigit(str[i]) != 0) { return 0; } if (str[i] == '+' || str[i] == '-') { if (str[i+1] == ')') { return 1; } else { return 2; } } } return 0; } double tail(string str)//captures the first number in round brackets and digitizes it with respect to the decimal point { string buf, tail; int i, j; int p = -1; for (i = 0; i < str.size(); i++)//counts all non-digits { if (isdigit(str[i]) != 0) { break; } } for (j = i; j < str.size(); j++)//writes all digits and dots in continuous sequence; the dot should be maximum one { if (isdigit(str[j]) != 0 || str[j] == '.') { buf.push_back(str[j]); } else { break; } } if (j < str.size() - 1 && str[j] == '(') { for (int k = j + 1; k < str.size(); k++)//extracts the sequence of digits after the round left bracket { if (isdigit(str[k]) != 0) { tail.push_back(str[k]); } else { if (k != j + 1)//halts breaking for the first symbol after the bracker in case it is not a digit, this is for asymmetric errors etc. { break; } } } } else { return 0; } if (tail.size() > 0)//flips all digits on the buffer to zero { for (int k = 0; k < buf.size(); k++) { if (buf[k] != '.') { buf[k] = '0'; } else { p = k; } } } else { return 0; } if (p == -1) { return stod(tail); } int M = buf.size(); int N = tail.size(); if (M - p - 1 >= N) { for (int k = 0; k < N; k++) { buf[M - N + k] = tail[k]; } return stod(buf); } else { tail.insert(N - M + p + 1, "."); return stod(tail); } } //************************************************************************************* //***////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*** //**((((((((((( MAIN ))))))))))** //***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////////*** //************************************************************************************* void test() { vector spin; spin.push_back("-1 2.5"); //spin.push_back("#-0.87689()*"); //spin.push_back("+1(555)/2-"); //spin.push_back("(-)($^@^#^#-2.9248(76)502409n3.14lakn"); //spin.push_back("(+)2.6(7809707)"); //spin.push_back("-2.33( "); //spin.push_back("-2.33 ( "); //spin.push_back("-2.33("); //spin.push_back("1.1(22)"); //spin.push_back("1.1(22)("); //spin.push_back(""); //spin.push_back(" "); //spin.push_back("3.435( 32534ljhl;"); //TString mu = spin4.c_str(); //cout << mu.Data() << endl; //cout << mu.Atoi() << endl; cout.precision(10); for (int i = 0; i < spin.size(); i++) { cout << spin[i] << endl; cout << stod(spin[i]) << "\n" << endl; } }