Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. void task1()
  7. {
  8.     char symbol;
  9.     cout << "Enter words (@ to abort): ";
  10.     while (cin >> symbol)
  11.     {
  12.         if (symbol == '@') break;
  13.  
  14.         if (isalpha(symbol))
  15.         {
  16.             if (isupper(symbol))
  17.                 symbol = tolower(symbol);
  18.             else
  19.                 symbol = toupper(symbol);
  20.             cout << symbol;
  21.         }
  22.     }
  23. }
  24.  
  25. void task2()
  26. {
  27.     double mas[10];
  28.     int mascount = 0;
  29.     cout << "Enter up to 10 numbers: ";
  30.     for (mascount; !cin.fail() && mascount < 10; mascount++)
  31.     {
  32.         cin >> mas[mascount];
  33.     }
  34.     if (mascount < 10)
  35.     {
  36.         cin.clear();
  37.         cin.ignore();
  38.     }
  39.     double average = 0;
  40.     for (int i = 0; i < mascount; i++)
  41.     {
  42.         average += mas[i];
  43.     }
  44.     average /= --mascount;
  45.     int count = 0;
  46.     for (int i = 0; i < mascount; i++)
  47.     {
  48.         if (mas[i] > average) count++;
  49.     }
  50.     cout << "Average: " << average << endl;
  51.     cout << "Count of numbers that > average: " << count << endl;
  52. }
  53.  
  54. void task3()
  55. {
  56.     string word;
  57.     int vowel = 0, consonants = 0, other = 0;;
  58.     cout << "Enter words(q to quit): ";
  59.     while (cin >> word)
  60.     {
  61.         if (word == "q") break;
  62.         if (isalpha(word[0]))
  63.         {
  64.             switch (word[0]) {
  65.             case 'a': case'e': case'i': case 'o': case 'u': case 'A': case'E': case'I': case 'O': case 'U':
  66.                 vowel++;
  67.                 break;
  68.             default: consonants++;
  69.             }
  70.         }
  71.         else other++;
  72.     }
  73.     cout << vowel << " words beginning with vowels\n";
  74.     cout << consonants << " words beginning with consonants\n";
  75.     cout << other << " other\n";
  76. }
  77.  
  78. void task4()
  79. {
  80.     fstream fin;
  81.     fin.open("C:/Users/Student.DESKTOP-9N1N9O3/Desktop/file.txt");
  82.     while (!fin)
  83.     {
  84.         cerr << "File doesn't open!\n";
  85.         cout << "Enter new file adress or \"e\" to abort: ";
  86.         string new_file;
  87.         cin >> new_file;
  88.         if (new_file == "e") return;
  89.         fin.open(new_file);
  90.     }
  91.     char stuff;
  92.     int count = 0;
  93.     while (fin >> stuff)
  94.         count++;
  95.     fin.close();
  96.     cout << count;
  97. }
  98.  
  99.  
  100.  
  101. double add(double lval, double rval)
  102. {
  103.     return lval + rval;
  104. }
  105.  
  106. double remove(double lval, double rval)
  107. {
  108.     return lval - rval;
  109. }
  110.  
  111. double multi(double lval, double rval)
  112. {
  113.     return lval * rval;
  114. }
  115.  
  116. double ost(double lval, double rval)
  117. {
  118.     if (int(lval) != lval || int(rval) != rval)
  119.     {
  120.         cerr << "Numbers aren't integer!\n";
  121.         return 0;
  122.     }
  123.     return int(lval) % int(rval);
  124. }
  125.  
  126. double calculate(double lval, double rval, double (*f)(double, double))
  127. {
  128.     return f(lval, rval);
  129. }
  130.  
  131.  
  132. void task5()
  133. {
  134.     double lval, rval;
  135.     string key;
  136.     cout << "Non-value input will end the program.\n";
  137.     while (true)
  138.     {
  139.         cout << "Enter 2 values: ";
  140.         cin >> lval >> rval;
  141.         if (cin.fail())
  142.         {
  143.             cout << "ended.";
  144.             break;
  145.         }
  146.  
  147.         const size_t arr_size = 4;
  148.         double((*arr[arr_size]))(double lval, double rval);
  149.         string func_name[arr_size];
  150.        
  151.         arr[0] = add; arr[1] = remove; arr[2] = multi; arr[3] = ost;
  152.         func_name[0] = "add"; func_name[1] = "remove"; func_name[2] = "multi"; func_name[3] = "ost";
  153.        
  154.         for (int i = 0; i < arr_size; i++)
  155.             cout << endl << "Result of " << func_name[i] << ": " << calculate(lval, rval, arr[i]) << endl;
  156.         cout << endl;
  157.     }
  158. }
  159.  
  160. int main()
  161. {
  162.     task2();
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement