Advertisement
DasShelmer

7.4.19

Nov 28th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int& replace(string& line, char from, char to) {
  6.     int count = 0, lenght = line.length();
  7.     for (int i = 0; i < lenght; i++) {
  8.         if (line[i] == from) {
  9.             line[i] = to;
  10.             count++;
  11.         }
  12.     }
  13.     return count;
  14. }
  15.  
  16. int main() {
  17.     string text = "";
  18.     int lenght, i;
  19.     bool isNumber = true, containPoint = false;
  20.  
  21.     setlocale(LC_ALL, "Russian");
  22.     cout << "Введите вещественное число: ";
  23.     getline(cin, text);
  24.  
  25.     replace(text, '.', ','); // '.' -> ','
  26.     lenght = text.length();
  27.     if (lenght == 0)
  28.         isNumber = false;
  29.     isNumber = isNumber && (isdigit(text[0]) || text[0] == '+' || text[0] == '-' || text[0] == ',');
  30.     containPoint == isNumber && text[0] == ',';
  31.  
  32.     for (i = 1; i < lenght && isNumber; i++) {
  33.         isNumber = isdigit(text[i]) || text[i] == ',';
  34.         if (text[i] == ',') {
  35.             isNumber = !containPoint && isNumber;
  36.             containPoint = true;
  37.         }
  38.     }
  39.     cout << (isNumber ? "Является вещ. числом!" : "Не является вещ. числом!") << endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement