Скачиваний:
10
Добавлен:
02.05.2014
Размер:
4.19 Кб
Скачать
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

const int XSize = 8;

template <class NumType>
class MyNum 
{
public:
  MyNum(int ns = XSize);
  MyNum(const MyNum<NumType>& mn);
  ~MyNum();
  MyNum<NumType>& operator=(const MyNum<NumType>& mn);
  NumType& operator[](int index) const;
  MyNum<NumType>(NumType *arr, int array_size);
  friend MyNum<NumType> operator+(const MyNum<NumType>& N1, const MyNum<NumType>& N2);
  friend MyNum<NumType> operator-(const MyNum<NumType>& N1, const MyNum<NumType>& N2);
  friend NumType operator*(const MyNum<NumType>& N1, const MyNum<NumType>& N2);
  friend MyNum<NumType> operator*(const MyNum<NumType>& N1, const NumType n2);
  friend MyNum<NumType> operator*(const NumType n2, const MyNum<NumType>& N1);
  int GetSize() const { return size; }
  void Show();
public:
  int size;
  NumType *data;
};

template <class NumType>
MyNum<NumType>::MyNum(int ns)
{
  size = ns;
  data = new NumType[size];
  memset(data, 0, sizeof(NumType) * size);
}

template <class NumType>
MyNum<NumType>::MyNum(const MyNum& mn)
{
  size = mn.size;
  data = new NumType[size];
  if (data == NULL)
    exit(1);
  memcpy(data, mn.data, size * sizeof(NumType));
}

template <class NumType>
MyNum<NumType>& MyNum<NumType>::operator=(const MyNum<NumType>& mn)
{
  /* Убеждаемся, что не выполняется присваивание вида x=x;. 
  Если левая и правая части ссылаются на один объект, делать ничего не надо. 
  Если не перехватить этот особый случай, то следующий
  шаг уничтожит значение до того, как оно будет скопировано.  */
  if (&mn == this) return *this;
  delete [] data;
  size = mn.size;
  data = new NumType[size];
  if (data == NULL)
  {
    exit(1);
  }
  memcpy(data, mn.data, sizeof(NumType) * size);
  return *this;
}

template <class NumType>
MyNum<NumType>::~MyNum()
{
  delete [] data;
  size = 0;
}

template <class NumType>
NumType& MyNum<NumType>::operator[](int index) const
{
  if (index < size && index >= 0)
    return data[index];
  else
  {
    printf("Index error\n");   
    exit(1);
  }
}

template <class NumType>
MyNum<NumType>::MyNum(NumType *arr, int array_size)
{
  size = array_size;
  data = new NumType[size];
  if (data == NULL)
  {
    exit(1);
  }
  memcpy(data, arr, size * sizeof(NumType));
}

template <class NumType>
void MyNum<NumType>::Show()
{
  int i;
  // printf("(");
  cout << "(";
  for (i = 0; i < size - 1; i++)
  {
    // printf("%d, ", data[i]);
    cout << data[i] << ", ";
  }
  // printf("%d)\n", data[size - 1]);
  cout << data[i] << ")" << endl;
}

template <class NumType>
MyNum<NumType> operator+(const MyNum<NumType>& N1, const MyNum<NumType>& N2)
{
  int i;
  if (N1.size != N2.size)
  {
    printf("Error adding arrays\n");
    exit(1);
  }
  MyNum<NumType> R(N1.size);
  for (i = 0; i < N1.size; i++)
  {
    R.data[i] = N1.data[i] + N2.data[i];
  }
  return R;
}

template <class NumType>
MyNum<NumType> operator-(const MyNum<NumType>& N1, const MyNum<NumType>& N2)
{
  int i;
  if (N1.size != N2.size)
  {
    printf("Error substracting arrays\n");
    exit(1);
  }
  MyNum<NumType> R(N1.size);
  for (i = 0; i < N1.size; i++)
  {
    R.data[i] = N1.data[i] - N2.data[i];
  }
  return R;
}

template <class NumType>
NumType operator*(const MyNum<NumType>& N1, const MyNum<NumType>& N2)
{
  int i;
  NumType rezultat;
  if (N1.size != N2.size)
  {
    printf("Error vecor proizvedenie length\n");
    exit(1);
  }
  rezultat = 0;
  for (i = 0; i < N1.size; i++)
  {
    rezultat += N1.data[i] * N2.data[i];
  }
  return rezultat;
}

template <class NumType>
MyNum<NumType> operator*(const MyNum<NumType>& N1, const NumType n2)
{
  int i;
  MyNum<NumType> R(N1.size);
  for (i = 0; i < N1.size; i++)
  {
    R.data[i] = N1.data[i] * n2;
  }
  return R;
}

template <class NumType>
MyNum<NumType> operator*(const NumType n2, const MyNum<NumType>& N1)
{
  int i;
  MyNum<NumType> R(N1.size);
  for (i = 0; i < N1.size; i++)
  {
    R.data[i] = N1.data[i] * n2;
  }
  return R;
}
Соседние файлы в папке vector_template
  • #
    02.05.201466.56 Кб11class_vector.ncb
  • #
    02.05.201448.64 Кб11class_vector.opt
  • #
    02.05.20141.29 Кб10class_vector.plg
  • #
    02.05.20142.59 Кб11main.cpp
  • #
    02.05.201438 б10main.h
  • #
    02.05.20144.19 Кб10vm1.h