Advertisement
chevengur

Подготовительный курс | Урок 3: Получение результата из функции: return 1/2

Aug 5th, 2023 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. ==========
  2. main.cpp||
  3. ==========
  4.  
  5.  
  6. // Реализуйте функции в functions.cpp.
  7. // Не меняйте содержимое этого файла.
  8.  
  9. // Тестируем версию int
  10. cout << "|100| = "s << ComputeAbs(100) << endl;
  11. cout << "|0| = "s << ComputeAbs(0) << endl;
  12. cout << "|-50| = "s << ComputeAbs(-50) << endl;
  13.  
  14. // Тестируем версию float. Можно задать значение float
  15. // добавив после цифр букву f
  16. cout << "|3.141592f| = "s << ComputeAbs(3.141592f) << endl;
  17. cout << "|0.f| = "s << ComputeAbs(0.f) << endl;
  18. cout << "|-3.141592f| = "s << ComputeAbs(-3.141592f) << endl;
  19.  
  20. // Тестируем версию double
  21. cout << "|3.141592| = "s << ComputeAbs(3.141592) << endl;
  22. cout << "|0| = "s << ComputeAbs(0) << endl;
  23. cout << "|-3.141592| = "s << ComputeAbs(-3.141592) << endl;
  24.  
  25. // Тестируем версию vector
  26. int idx = 0;
  27. vector input_data = {10, 20, -30, -50, 100};
  28. for (int x : ComputeAbs(input_data)) {
  29.     // Операция ++ увеличивает значение целочисленной переменной на 1.
  30.     // Результатом операции ++ становится значение до увеличения.
  31.     cout << "|"s << input_data[idx++] << "| = "s << x << endl;
  32. }
  33.  
  34. ----------------------------------------------------------------------------------------------------------------------------
  35.  
  36. ===============
  37. functions.cpp||
  38. ===============
  39.  
  40.  
  41. #include <iostream>
  42. #include <string>
  43. #include <vector>
  44.  
  45. template<typename K>
  46. K ComputeAbs(K num){
  47.     return abs(num);
  48. }
  49.  
  50. std::vector<int> ComputeAbs(std::vector<int> num){
  51.     std::vector<int>next_vec;
  52.     for(const auto& n: num){
  53.         next_vec.push_back(abs(n));
  54.     }
  55.     return next_vec;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement