Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LABA9_DECLARATION_H
- #define LABA9_DECLARATION_H
- #include <iostream>
- template <typename T, int N>
- class SumSeq {
- private:
- int len;
- T data[N], results[N];
- public:
- SumSeq<T, N> operator+=(SumSeq &x) {
- for (int i = 0; i < N; i++) {
- data[i] += x.data[i];
- for (int j = 0; j <= i; j++) results[i] += x.data[j];
- }
- return *this;
- }
- SumSeq<T, N> operator*=(T x) {
- for (int i = 0; i < N; i++) {
- data[i] *= x;
- results[i] *= x;
- }
- return *this;
- }
- T operator()(int a, int b) {
- return (a == 0) ? results[b] : results[b] - results[a - 1];
- }
- SumSeq(T dat[N]) {
- for (int i = 0; i < N; i++) data[i] = dat[i];
- len = N;
- results[0] = data[0];
- for (int i = 1; i < N; i++) results[i] = results[i - 1] + data[i];
- }
- void print() {
- for (int i = 0; i < len; i++) std::cout << data[i] << ' ';
- std::cout << std::endl;
- }
- };
- #endif
- ----------------------------------------------
- #include <iostream>
- #include "declaration.h"
- using namespace std;
- int main() {
- int xx[3] = {1, 2, 3};
- SumSeq<int, 3> x(xx);
- int yy[3] = {4, 5, 6};
- SumSeq<int, 3> y(yy);
- x += y;
- x *= 10;
- x.print();
- cout << x(1, 2) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement