Advertisement
DasShelmer

9.s.2

Dec 5th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. bool isDigits(string& s, int from, int to) {
  7.     for (int i = from; i <= to; i++)
  8.         if (!isdigit(s.at(i)))
  9.             return false;
  10.     return true;
  11. }
  12.  
  13. bool isTNumber(string& word) {
  14.     int lenght = word.length();
  15.     if (lenght < 7 || lenght > 9)
  16.         return false;
  17.  
  18.     switch (lenght) {
  19.     case 7: // xxx-xxx
  20.         return isDigits(word, 0, 2) && isDigits(word, 4, 6) && word[3] == '-';
  21.     case 9: // xxx-xx-xx
  22.         return isDigits(word, 0, 2) && isDigits(word, 4, 5) && word[3] == '-' && isDigits(word, 7, 8) && word[6] == '-';
  23.     case 8: // xx-xx-xx
  24.         return isDigits(word, 0, 1) && isDigits(word, 4, 5) && word[2] == '-' && isDigits(word, 6, 7) && word[5] == '-';
  25.     default:
  26.         return false;
  27.     }
  28. }
  29.  
  30. int main() {
  31.     ifstream in ("f1.txt");
  32.     ofstream out("f2.txt");
  33.  
  34.     string temp;
  35.  
  36.     while (in)
  37.         if (in >> temp && isTNumber(temp))
  38.             out << temp << endl;
  39.     in.close();
  40.     out.close();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement