SHARE
TWEET

Untitled

a guest May 16th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef LABA9_DECLARATION_H
  2. #define LABA9_DECLARATION_H
  3.  
  4. #include <iostream>
  5.  
  6. template <typename T, int N>
  7.  
  8. class SumSeq {
  9. private:
  10.     int len;
  11.     T data[N], results[N];
  12. public:
  13.     SumSeq<T, N> operator+=(SumSeq &x) {
  14.         for (int i = 0; i < N; i++) {
  15.             data[i] += x.data[i];
  16.             for (int j = 0; j <= i; j++) results[i] += x.data[j];
  17.         }
  18.         return *this;
  19.     }
  20.     SumSeq<T, N> operator*=(T x) {
  21.         for (int i = 0; i < N; i++) {
  22.             data[i] *= x;
  23.             results[i] *= x;
  24.         }
  25.         return *this;
  26.     }
  27.     T operator()(int a, int b) {
  28.         return (a == 0) ? results[b] : results[b] - results[a - 1];
  29.     }
  30.     SumSeq(T dat[N]) {
  31.         for (int i = 0; i < N; i++) data[i] = dat[i];
  32.         len = N;
  33.         results[0] = data[0];
  34.         for (int i = 1; i < N; i++) results[i] = results[i - 1] + data[i];
  35.     }
  36.     void print() {
  37.         for (int i = 0; i < len; i++) std::cout << data[i] << ' ';
  38.         std::cout << std::endl;
  39.     }
  40. };
  41.  
  42. #endif
  43.  
  44. ----------------------------------------------
  45.  
  46. #include <iostream>
  47. #include "declaration.h"
  48.  
  49. using namespace std;
  50.  
  51. int main() {
  52.     int xx[3] = {1, 2, 3};
  53.     SumSeq<int, 3> x(xx);
  54.     int yy[3] = {4, 5, 6};
  55.     SumSeq<int, 3> y(yy);
  56.     x += y;
  57.     x *= 10;
  58.     x.print();
  59.     cout << x(1, 2) << endl;
  60.     return 0;
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top