fcamuso

Corso C++ 23 - lezione 34 (funzioni quarta parte)

Jun 1st, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #pragma region IntelliSense_Fix
  2. #ifdef __INTELLISENSE__
  3. #include <print>
  4. #include <string>
  5. #include <chrono>
  6. #endif
  7. #pragma endregion
  8. import std;
  9. template <typename T>
  10. concept SignedNumeric = std::signed_integral<T> || std::floating_point<T>;
  11.  
  12. //template <typename T>
  13. //requires std::signed_integral<T> || std::floating_point<T>
  14. //template <SignedNumeric T>
  15. auto valore_assoluto(SignedNumeric auto n);
  16.  
  17. int stima_ampiezza();
  18. void riga_divisoria(char simbolo, int lunghezza=stima_ampiezza());
  19.  
  20.  
  21.  
  22. //template <typename T>
  23. //requires std::signed_integral<T> || std::floating_point<T>
  24. //template <SignedNumeric T>
  25. //T valore_assoluto(T n)
  26.  
  27. auto valore_assoluto(SignedNumeric auto n)
  28. {
  29.     return n >= 0 ? n : -n;
  30. }
  31.  
  32. // Funzione generica per tutti i numeri con segno
  33. template <SignedNumeric T>
  34. void elabora(T n) { /* algoritmo standard */ }
  35.  
  36. // Funzione super-ottimizzata SOLO per i numeri in virgola mobile
  37. template <std::floating_point T>
  38. void elabora(T n) { /* algoritmo ultra-veloce */ }
  39.  
  40.  
  41.  
  42. //un overload NON puo' differire solo per il tipo restituito
  43. //double  valore_assoluto(double n)
  44. //{
  45. //    return n >= 0.0 ? (double)n : (double) - n;
  46. //}
  47.  
  48. void print(int n, char simbolo='-') {
  49.     std::string n_str = std::format("{}", n);
  50.     std::string bordo = std::string(n_str.length(), simbolo);
  51.  
  52.     std::println("{}", bordo);
  53.     std::println("{}", n);
  54.     std::println("{}", bordo);
  55. }
  56.  
  57. // Stampa di un contenitore: richiede un ciclo e un iteratore
  58. void print(const std::vector<int>& v, char simbolo='=') {
  59.     std::string bordo = std::string(50, simbolo);
  60.    
  61.     std::println("\n\n{}\n{}", bordo, bordo);
  62.     for (int n : v) {
  63.         std::println("*{}*",n);
  64.     }
  65.     std::println("{}\n{}", bordo, bordo);
  66. }
  67.  
  68. // API C sottostante
  69. void send_data_i(int d){};
  70. void send_data_f(float d){};
  71. void send_data_s(const char* d){};
  72.  
  73. // Wrapper C++
  74. void send(int d) { send_data_i(d); }
  75. void send(float d) { send_data_f(d); }
  76. void send(const char* d) { send_data_s(d); }
  77.  
  78.  
  79. int stima_ampiezza() { return 60; }
  80.  
  81. void riga_divisoria(char simbolo, int lunghezza)
  82. {
  83.     std::println("{}", std::string(lunghezza, simbolo));
  84.     return;
  85. }
  86.  
  87. auto main() -> int
  88. {
  89.     int x {};
  90.     std::println("{}", valore_assoluto<double>(- 34.8));
  91.  
  92.     //std::println("{}", valore_assoluto<std::string>("paperino"));
  93.  
  94.  
  95.     x = -5;
  96.     std::println("{}", valore_assoluto(x));
  97.  
  98.  
  99.    
  100.     //print(23567);
  101.  
  102.     auto v = std::vector<int>({13,5757,123454,35,345});
  103.     //print(v);
  104.    
  105.    
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment