-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVektor.cpp
148 lines (122 loc) · 3.2 KB
/
Vektor.cpp
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
#include "Vektor.h"
#include <iostream>
#include <stdexcept>
#include <cmath>
using namespace std;
// Standard-Konstruktor
Vektor::Vektor(int a) {
this->sz=a;
elems = new double[a];
cout<<"\nVektor fuellen pls! "<<endl;
for (int n=0;n<sz;++n){
cout<<"Wert "<<n+1<<" pls: ";
cin>>elems[n];
cin.clear();
cin.ignore(256,'\n');
}
}
// Weiterer Konstruktor
Vektor::Vektor(const double values[]) {
this->sz=sizeof(values)/sizeof(values[0]);
elems = new double[sz];
for (int i = 0; i < sz; ++i)
elems[i] = values[i];
}
// Copy-Konstruktor
Vektor::Vektor(const Vektor& v) {
elems = NULL; // Undefinierten Wert überschreiben, damit das
// delete[] elems in operator= nicht abstuerzt
// (delete NULL oder delete[] NULL hat keine Wirkung)
*this = v; // Zuweisungsoperator nutzen vermeidet redundanten Code
}
//Getter/Setter
double Vektor::size(){
return sz;
}
//Rechenfunktionen
double Vektor::Summennorm()const{
double norm=0;
for (int i=0;i!=sz;++i){
norm+=fabs(elems[i]);
}
return norm;
}
double Vektor::Euklidnorm()const{
double norm=0;
for (int i=0;i!=sz;++i){
norm+=pow(elems[i],2);
}
return sqrt(norm);
}
double Vektor::Maximumnorm()const{
double norm=0;
for (int i=0;i!=sz;++i){
if (fabs(elems[i])>norm) norm=fabs(elems[i]);
}
return norm;
}
// Zuweisungsoperatoren
Vektor& Vektor::operator = (const Vektor& v) {
if (this == &v) return *this; // Nichts zu tun
delete[] elems;
this->sz=v.sz;
elems = new double[sz];
for (int i = 0; i < sz; ++i)
elems[i] = v.elems[i];
return *this;
}
Vektor& Vektor::operator += (const Vektor& v) {
if (this == &v) return *this; // Nichts zu tun
for (int i = 0; i < sz; ++i)
elems[i] += v.elems[i];
return *this;
}
Vektor& Vektor::operator += (const double d[]) {
Vektor v(d);
return *this += v; // operator+=(const Vektor&) nutzen vermeidet Code-Redundanz
}
// Vergleichsoperatoren
bool Vektor::operator == (const Vektor& v) const {
return *this == v.elems; // Überladenen operator== aufrufen vermeidet Redundanz
}
bool Vektor::operator == (const double d[]) const {
for (int i = 0; i < sz; ++i)
if (elems[i] != d[i])
return false;
return true;
}
// Destruktor
Vektor::~Vektor() {
delete[] elems;
}
// Weitere Operatoren
Vektor Vektor::operator + (const Vektor& v) const {
Vektor result(*this); // Copy-Konstruktor
result += v; // operator+= nutzen vermeidet Code-Redundanz
return result;
}
double& Vektor::operator [] (int index) const {
if (index < 0 || index >= sz)
throw invalid_argument("Indexfehler");
return elems[index];
}
bool operator == (const double d[], const Vektor& v) {
return v == d; // v.operator==(double[]) aufrufen
}
ostream& operator << (ostream& out, const Vektor& v) {
out << "(" << v.elems[0];
for (int i = 1; i < v.sz; ++i) {
out << ", " << v.elems[i];
}
out << ")";
return out;
}
//Version für beliebiges N:
istream& operator >> (istream& in, Vektor& v) {
double* values=new double[v.sz];
for (int i = 0; i < v.sz; ++i)
in >> values[i];
v = Vektor(values); // ruft Vektor::operator=(double[]) auf
delete[] values;
return in;
}