1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include"pch.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
class Vector
{
private:
double *v;
int len;
public:
Vector(int size);
Vector(double *, int);
~Vector();
double &operator[](int i);
Vector & operator =(Vector &);
friend Vector operator +(Vector &, Vector &);
friend Vector operator -(Vector &, Vector &);
friend double operator *(Vector &, Vector &);
friend ostream & operator <<(ostream &output, Vector &);
friend istream & operator >>(istream &input, Vector &);
};
Vector::Vector(int size)
{
if (size <= 0 || size >= 2147483647)
{
cout << "The size of " << size << "is overflow!\n";
abort();
}
v = new double[size];
for (int i = 0; i < size; i++) v[i] = 0;
len = size;
}
Vector::Vector(double *C, int size)
{
if (size <= 0 || size >= 2147483647)
{
cout << "The size of" << size << "is overflow!\n" << endl;
abort();
}
v = new double[size];
len = size;
for (int i = 0; i < len; i++) v[i] = C[i];
}
Vector::~Vector()
{
delete[]v;
v = NULL; len = 0;
}
double &Vector::operator[](int i)
{
if (i >= 0 && i < len)
return v[i];
else
{
cout << "The size of" << i << "is overflow!\n";
abort();
}
}
Vector &Vector::operator =(Vector &C)
{
if (len == C.len)
{
for (int i = 0; i < len; i++)
v[i] = C[i];
return *this;
}
else
{
cout << "Operator = fail!\n";
abort();
}
}
Vector operator +(Vector &A, Vector &B)
{
int size = A.len;
double *T = new double[size];
if (size == B.len)
{
for (int i = 0; i < size; i++)
T[i] = A[i] + B[i];
return Vector(T, size);
}
else
{
cout << "Operator + fail!\n";
abort();
}
}
Vector operator -(Vector &A, Vector &B)
{
int size = A.len;
double *T = new double[size];
if (size == B.len)
{
for (int i = 0; i < size; i++)
T[i] = A[i] - B[i];
return Vector(T, size);
}
else
{
cout << "Operator - fail!\n";
abort();
}
}
double operator *(Vector &A, Vector &B)
{
int size = A.len;
double s = 0;
if (size == B.len)
{
for (int i = 0; i < size; i++)
s += A[i] * B[i];
return s;
}
else
{
cout << "Operator * fail!\n";
abort();
}
}
ostream & operator <<(ostream &output, Vector &A)
{
int i;
output << '(';
for (i = 0; i < A.len - 1; i++)
output << A[i] << ',';
output << A[i] << ')';
return output;
}
istream & operator >>(istream &input, Vector &A)
{
for (int i = 0; i < A.len; i++)
input >> A[i];
return input;
}
int main()
{
int k1, k2, k3; double t;
cout << "Input the length of Vector A:\n";
cin >> k1;
Vector A(k1);
cout << "Input the elements of Vector A:\n";
cin >> A;
cout << "Input the length of Vector B:\n";
cin >> k2;
Vector B(k2);
cout << "Input the elements of Vector B:\n";
cin >> B;
cout << "Input the length of Vector C:\n";
cin >> k3;
Vector C(k3);
cout << "A=" << A << endl;
cout << "B=" << B << endl;
C = A + B;
cout << "A+B=" << A << "+" << B << "=" << C << endl;
C = A - B;
cout << "A-B=" << A << "-" << B << "=" << C << endl;
t = A * B;
cout << "A*B=" << A << "*" << B << "=" << t << endl;
}
|