fork(1) download
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. // file: sleep.h
  5. void sleep_milli(unsigned milliseconds);
  6.  
  7. // file: sleep.cpp
  8. #ifdef _WIN32
  9. #include <windows.h>
  10.  
  11. void sleep_milli(unsigned milliseconds)
  12. {
  13. Sleep(milliseconds);
  14. }
  15. #else
  16. #include <unistd.h>
  17.  
  18. void sleep_milli(unsigned milliseconds)
  19. {
  20. usleep(milliseconds * 1000);
  21. }
  22. #endif
  23.  
  24. // file: task.h
  25. class Task
  26. {
  27. public:
  28. virtual void Execute() = 0;
  29. };
  30.  
  31. // file: factorialtask.h
  32. class FactorialTask : public Task
  33. {
  34. public:
  35. FactorialTask(unsigned int n) : _n(n) { }
  36. void Execute();
  37.  
  38. private:
  39. unsigned int _n;
  40. };
  41.  
  42. // file: fibonaccitask.h
  43. class FibonacciTask : public Task
  44. {
  45. public:
  46. FibonacciTask(unsigned int n) : _n(n) { }
  47. void Execute();
  48.  
  49. private:
  50. unsigned int _n;
  51. };
  52.  
  53. // file: loggingtask.h
  54. template <class T>
  55. class LoggingTask : public T
  56. {
  57. public:
  58. LoggingTask(unsigned int n) : T(n) { }
  59. void Execute();
  60. };
  61.  
  62. template <class T>
  63. void LoggingTask<T>::Execute()
  64. {
  65. time_t now;
  66. struct tm *current;
  67.  
  68. now = time(0);
  69. current = localtime(&now);
  70. std::cout << "[" << current->tm_hour << ":" << current->tm_min << ":" << current->tm_sec << "] Logging started" << std::endl;
  71.  
  72. T::Execute();
  73.  
  74. now = time(0);
  75. current = localtime(&now);
  76. std::cout << "[" << current->tm_hour << ":" << current->tm_min << ":" << current->tm_sec << "] Logging ended" << std::endl;
  77. }
  78.  
  79. using namespace std;
  80.  
  81. // file: factorialtask.cpp
  82. void FactorialTask::Execute()
  83. {
  84. unsigned int result = 1;
  85. for(int i = 1; i <= _n; ++i)
  86. {
  87. result *= i;
  88. sleep_milli(100);
  89. }
  90. cout << _n << "!= " << result << endl;
  91. }
  92.  
  93. // file: fibonaccitask.cpp
  94. void FibonacciTask::Execute()
  95. {
  96. unsigned int a = 1, b = 1;
  97. for(int i = 3; i <= _n; ++i)
  98. {
  99. b = a + b;
  100. a = b - a;
  101. sleep_milli(100);
  102. }
  103. cout << "Fibonacci(" << _n << ") = " << b << endl;
  104. }
  105.  
  106. // file: main.cpp
  107. int main()
  108. {
  109. FactorialTask facTask(20);
  110. facTask.Execute();
  111.  
  112. FibonacciTask fibTask(20);
  113. fibTask.Execute();
  114.  
  115. LoggingTask<FactorialTask> logFacTask(20);
  116. logFacTask.Execute();
  117.  
  118. LoggingTask<FibonacciTask> logFibTask(20);
  119. logFibTask.Execute();
  120.  
  121. return 0;
  122. }
  123.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
20!= 2192834560
Fibonacci(20) = 6765
[8:34:24] Logging started
20!= 2192834560
[8:34:26] Logging ended
[8:34:26] Logging started
Fibonacci(20) = 6765
[8:34:28] Logging ended