Advertisement
smatskevich

ReadInt

May 22nd, 2021
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. bool IsDigit(char c) {
  7.   return c >= '0' && c <= '9';
  8. }
  9.  
  10. // Возвращает число с позиции cursor
  11. int ReadInt(const string& s, int& cursor) {
  12.   // 234,6sd,98224980,
  13.   //         ^       ^
  14.   //         cursor  new_cursor
  15.   int result = 0;
  16.   while (IsDigit(s[cursor])) {
  17.     int digit = s[cursor] - '0';
  18.     result = result * 10 + digit;
  19.     ++cursor;
  20.   }
  21.   return result;
  22. }
  23.  
  24. int main() {
  25.   string s;
  26.   getline(cin, s);
  27.   int cursor = 0;
  28.   int value = ReadInt(s, cursor);
  29.   cout << "Value = " << value << endl;
  30.   cout << "Cursor = " << cursor;
  31.   return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement