bool Double::isNan() { return this->nan; } void Double::NaN(string s) { int pos;
ID: 3556539 • Letter: B
Question
bool Double::isNan()
{
return this->nan;
}
void Double::NaN(string s)
{
int pos;
pos = s.find(" . ", 0);
if (pos != string::npos)
{
pos = s.find(" . ", pos + 1);
if (pos != string::npos)
{
this->nan = true;
return;
}
}
string::iterator p;
for (p = s.begin(); p < s.end(); p++)
{
if (!isdigit(*p) && *p != ' . ')
{
this->nan = true;
return;
}
}
this->nan = false;
return;
}
}
modify function to use recursion to check if a string contains a valid number
Explanation / Answer
Pastebin:
http://pastebin.com/cy3JVNGw
bool Double::isNan()
{
return this->nan;
}
void Double::NaN(string s)
{
// Makes sure a string is not sized 0 (for recursion)
if (s.size() == 0) {
this->nan = false;
return;
}
// Checks for 0 or 1 dots
int pos;
pos = s.find(".", 0);
if (pos != string::npos)
{
pos = s.find(".", pos + 1);
if (pos != string::npos)
{
this->nan = true;
return;
}
}
// Recursion step
if (!isdigit(s[0]) && s[0] != '.')
{
this->nan = true;
return;
} else {
return NaN(s.substr(1, string::npos));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.