Difference between revisions of "Note de curs, probleme algoritmică, clasa IX/X, 7 noiembrie 2014"

From Algopedia
Jump to navigationJump to search
 
(No difference)

Latest revision as of 22:00, 23 June 2015

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

template <class Type>
struct Vector {
	int size;
	Type *V;

	Vector(int size, Type *data) {
		this->size = size;
		this->V = new Type[size];
		// memcpy(this->V, data, size * sizeof(Type));
		for (int i = 0; i < size; ++i) {
			this->V[i] = data[i];
		}
	}

	Vector(const Vector& arg) {
		this->size = arg.size;
		this->V = new Type[this->size];
		// memcpy(this->V, data, size * sizeof(Type));
		for (int i = 0; i < this->size; ++i) {
			this->V[i] = arg[i];
		}
	}

	Vector& operator = (const Vector& arg) {
		delete this->V;
		this->V = new Type[arg.size];
		this->size = arg.size;
		for (int i = 0; i < this->size; ++i) {
			this->V[i] = arg[i];
		}
		return *this;
	}

	Type& operator [] (int poz) const {
		return this->V[poz];
	}

	~Vector() {
		delete this->V;
	}
};

template <class Type>
Type primulElement(Vector<Type> v) {
	return v[0];
	// delete &v;
}

int main(void) {
	int N;
	int i;
	float data[5];

	// citirea datelor
	scanf("%d", &N);
	for (i = 0; i < N; ++i) {
		scanf("%f", &data[i]);
	}

	Vector<float> v(N, data);
	cout << v[3] << endl;
	v[3] = 1;
	cout << v[3] << endl;
	Vector<float> w = v;
	//Vector<float> w(v);
	w = v;
	cout << v[3] << ' ' << w[3] << ' ' << data[3] << endl;
	w[3]++;
	cout << v[3] << ' ' << w[3] << ' ' << data[3] << endl;

	cout << primulElement(v) << endl;
	Vector<int> q(1, new int[1]);
	cout << primulElement(q) << endl;

	return 0;
	// delete &v;
	// delete &w;
}