Advertisement
informaticage

Iterazioni e validazione numeri C++ bribro

Dec 21st, 2020
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. bool isDigit(char c) {
  5.     if( c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '0' )
  6.         return true;
  7.     return false;
  8. }
  9.  
  10. // Pensata come l'albero sintattico (AST)
  11. bool isNumberRec(std::string s) {
  12.     if (s.length() == 1 && isDigit(s[0]))
  13.         return true;
  14.  
  15.     // Number ::= NumberCif | Cif
  16.     std::string cutString = s.substr(0, s.size() - 1);
  17.     return (isNumberRec(cutString) && isDigit(s[s.size() - 1]));
  18. }
  19.  
  20. // Pensata come definizione naturale di numero (ovvero sequenza di cifre una dopo l'altra)
  21. bool isNumber (std::string s) {
  22.     for(int i = 0; i < s.length(); i++)
  23.         if (!isDigit(s[i]))
  24.             return false;
  25.  
  26.     return true;
  27. }
  28.  
  29. int main() {
  30.     using namespace std;
  31.     string age;
  32.     cout << "Inserisci eta (tra 0 e 130)";
  33.     cin >> age;
  34.     cout << stoi(age) << endl;
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement