#include typedef enum VarType { vtInteger, vtComplex, vtCompMatrix }; using namespace std; class Variable { protected: VarType type; bool fiction; public: Variable() { fiction = false; } Variable(Variable * v1) { } virtual ~Variable() { } protected: void SetFiction(bool fl) { fiction = fl; } virtual Variable * Create() { return new Variable(); } virtual Variable * Copy(Variable * v) { return this; } void ChooseOperands(Variable * p2, Variable ** op1, Variable ** op2) { if (fiction) { (* op1) = this; (* op2) = p2; } else { (* op1) = (Create())->Copy(this); (* op1)->SetFiction(true); (* op2) = p2; } } void CheckFiction(Variable * op2) { if (op2->fiction) delete op2; } public: virtual void Add(Variable * op1) { }; virtual void Sub(Variable * op1) { }; virtual void Mul(Variable * op1) { }; virtual void Div(Variable * op1) { }; Variable & operator+(Variable & p1) { Variable * op1 = NULL, * op2 = NULL; ChooseOperands(&p1, &op1, &op2); op1->Add(op2); CheckFiction(op2); return (* op1); } Variable & operator-(Variable & p1) { Variable * op1 = NULL, * op2 = NULL; ChooseOperands(&p1, &op1, &op2); op1->Sub(op2); CheckFiction(op2); return (* op1); } Variable & operator*(Variable & p1) { Variable * op1 = NULL, * op2 = NULL; ChooseOperands(&p1, &op1, &op2); op1->Mul(op2); CheckFiction(op2); return (* op1); } Variable & operator/(Variable & p1) { Variable * op1 = NULL, * op2 = NULL; ChooseOperands(&p1, &op1, &op2); op1->Div(op2); CheckFiction(op2); return (* op1); } Variable & operator=(Variable & p1) { Copy(&p1); CheckFiction(&p1); return (* this); } }; class Integer: public Variable { public: int value; Integer() { type = vtInteger; value = 0; } Integer(int nv) { type = vtInteger; value = nv; } Integer(Integer * v1) { type = vtInteger; value = v1->value; } virtual ~Integer() { } virtual Variable * Create() { return new Integer(); } virtual Variable * Copy(Variable * v) { value = ((Integer *) v)->value; return this; } virtual void Add(Variable * op1) { value += ((Integer *) op1)->value; } virtual void Sub(Variable * op1) { value -= ((Integer *) op1)->value; } virtual void Mul(Variable * op1) { value *= ((Integer *) op1)->value; } virtual void Div(Variable * op1) { value /= ((Integer *) op1)->value; } Integer & operator=(Variable & p1) { Variable::operator=(p1); return (* this); } }; class Complex: public Variable { protected: double a; double b; public: Complex() { type = vtComplex; a = 0; b = 0; } Complex(double na, double nb) { type = vtComplex; a = na; b = nb; } Complex(Complex * v1) { type = vtComplex; a = v1->a; b = v1->b; } virtual ~Complex() { } virtual Complex * Create() { return new Complex(); } virtual Complex * Copy(Variable * v) { a = ((Complex *) v)->a; b = ((Complex *) v)->b; return this; } virtual void Add(Variable * op1) { a += ((Complex *) op1)->a; b += ((Complex *) op1)->b; cout << "a: " << a << endl << "b: " << b << endl; } virtual void Sub(Variable * op1) { a -= ((Complex *) op1)->a; b -= ((Complex *) op1)->b; cout << "a: " << a << endl << "b: " << b << endl; } virtual void Mul(Variable * op1) { double a1 = a, b1 = b, a2 = ((Complex *) op1)->a, b2 = ((Complex *) op1)->b; a = (a1 * a2) - (b1 * b2); b = (a1 * b2) + (b1 * a2); cout << "a: " << a << endl << "b: " << b << endl; } virtual void Div(Variable * op1) { double a1 = a, b1 = b, a2 = ((Complex *) op1)->a, b2 = ((Complex *) op1)->b; double d = (a2*a2 + b2*b2); a = (a1*a2 + b1*b2) / d; b = (a2*b1 - b2*a1) / d; cout << "a: " << a << endl << "b: " << b << endl; } Complex & operator=(Variable & p1) { Variable::operator=(p1); Complex * p2 = (Complex *) &p1; cout << "a: " << p2->GetA() << endl << "b: " << p2->GetB() << endl; return (* this); } double GetA() { return a; } double GetB() { return b; } void Clear() { a = 0; b = 0; } void SetA(double na) { a = na; } void SetB(double nb) { b = nb; } void SetAB(double na, double nb) { a = na; b = nb; } }; struct CompBlock { unsigned nColumn; Complex value; CompBlock() { nColumn = 0; } }; typedef CompBlock * pCompBlock ; class CompMatrix: public Variable { protected: CompBlock * * index; CompBlock * vector; unsigned m; unsigned n; int curQty; int maxQty; unsigned brQty; unsigned br[2]; public: CompMatrix() { type = vtCompMatrix; index = NULL; vector = NULL; m = 0; n = 0; maxQty = 0; curQty = 0; brQty = 0; br[0] = 0; br[1] = 0; } void Init(int m0, int n0, int mq0) { if (vector) delete vector; if (index) delete index; m = m0; n = n0; maxQty = mq0; index = new pCompBlock[m]; memset(index, 0, sizeof(CompBlock)*m); vector = new CompBlock[maxQty]; } Complex GetValue() { int m0 = br[0], n0 = br[1]; br[0] = 0; br[1] = 0; brQty = 0; printf("%d | %d\n", m0, n0); CompBlock * cp = index[m0]; for (unsigned i=0; ivalue; if (curQty < maxQty) { memcpy(newBlock, newBlock + 1, ((curQty - n0) * sizeof(CompBlock))); for (unsigned i=m0+1; inColumn = n0; newBlock->value.Clear(); return newBlock->value; } else { Blank.Clear(); return Blank; } } else //28.05.08 21:00 { if (curQty < maxQty) { if (nextBlock) { index[m0] = nextBlock; memcpy(nextBlock, nextBlock + 1, ((curQty - m0) * sizeof(CompBlock))); index[m0]->value.Clear(); for (unsigned i=m0+1; ivalue; } else { index[m0] = vector + curQty; curQty++; vector[curQty].nColumn = n0; return vector[curQty].value; } } else { Blank.Clear(); return Blank; } } } CompMatrix & operator[](unsigned p1) { br[brQty++] = p1; return (* this); } }; class VirtualArray { protected: VirtualArray * next; Complex value; int position; public: VirtualArray() { next = NULL; position = 0; } VirtualArray(Complex v, int p) { //Complex } void Add(Complex v, int p) { if (position != p) { if (next) next->Add(v, p); else next = new VirtualArray(v, p); } else { value.Copy(&v); } } void Clear() { } Complex & operator[](int p) { return value; } }; int main() { Complex v1(1,2), v2(3,4), v3(6,7), v4(8,2), v5(7,3); CompMatrix vm; vm.Init(5, 5, 5); vm[1][1].SetValue() = v1; vm[2][2].SetValue() = v2; vm[3][3].SetValue() = v3; vm[4][4].SetValue() = v4; //vm[5][5].SetValue() = v5; for (int i=0; i<5; i++) for (int j=0; j<5; j++) printf("v[%d][%d] = %f | %f\n", i, j, vm[i][j].GetValue().GetA(), vm[i][j].GetValue().GetB()); //v5 = ((v1 + v2) / (v3 - v4)); //cout << v5.GetA() << endl <