Advertisement
andruhovski

Prog-20160419

Apr 19th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // ConsoleApplication6.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <fstream>
  6. #include <vector>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <algorithm>
  10. #include <functional>
  11.  
  12. using namespace std;
  13. void show(double x);
  14. double fn01(double x);
  15. double fn02(double x);
  16. int _tmain(int argc, _TCHAR* argv[])
  17. {
  18.     vector <double> *vect_a;
  19.     vector <double>::iterator p;
  20.     ifstream ifs("numbers.txt");
  21.     int n;
  22.     if (ifs.bad())
  23.     {
  24.         cerr << "File open error";
  25.     }
  26.    
  27.     ifs >> n;
  28.     vect_a = new vector<double>(n);
  29.     p = vect_a->begin();
  30.     while (!ifs.eof())
  31.     {
  32.         double x;
  33.         ifs >> x;
  34.         *p = x;
  35.         p++;
  36.     }
  37.  
  38.     for_each(vect_a->begin(), vect_a->end(), show);
  39.     p = find_if(vect_a->begin(), vect_a->end(), bind2nd(less<double>(), -3));
  40.     if (p == vect_a->end())
  41.     {
  42.         // не знайшли
  43.         // в іншому випадку помножити всі члени на 0,1
  44.         transform(vect_a->begin(), vect_a->end(), vect_a->begin(), fn02);
  45.     }
  46.     else
  47.     {
  48.         //всі від’ємні члени замінити їх квадратами, залишивши решту членів без змін
  49.         transform(vect_a->begin(), vect_a->end(), vect_a->begin(), fn01);
  50.     }
  51.     for_each(vect_a->begin(), vect_a->end(), show);
  52.     return 0;
  53. }
  54.  
  55. void show(double x)
  56. {
  57.     cout << setw(8) << x << endl;
  58. }
  59.  
  60. double fn01(double x)
  61. {
  62.     if (x < 0)
  63.         return x*x;
  64.     else
  65.         return  x;
  66. }
  67. double fn02(double x)
  68. {
  69.     return  0.1*x;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement