일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 빅데이타
- 몽고디비
- probability
- Statistics
- 우리들교회
- Artificial Intelligence
- No SQL
- 빅 데이터
- 김양재 목사
- 확률
- 주일설교
- 김양재 목사님
- MongoDB
- 딥러닝
- Machine Learning
- nodeJS
- node.js
- Big Data
- openCV
- 빅데이터
- c++
- 데이터 과학
- R
- 인공지능
- data science
- Deep learning
- 김양재
- WebGL
- 통계
- 빅 데이타
Archives
- Today
- Total
Scientific Computing & Data Science
[C/C++] Example / Vector Class 본문
"CVector.h"
//
// CVector.h
//
// Created by gchoi on 2014. 5. 9..
// Copyright (c) 2014년 gchoi. All rights reserved.
//
#ifndef Test_005_CVector_h
#define Test_005_CVector_h
#include <iostream>
using std::istream;
using std::ostream;
class Vector
{
private:
double* Vec; // Elements of vector
int Len; // Length of vector
public:
Vector( int N = 1, double Value = 0.0 );
Vector( double Array[], int N );
Vector( const Vector& );
~Vector();
void reset() { Len = 0; }
unsigned length() const { return Len; }
double maximum() const;
double minimum() const;
double sum() const;
double mean() const;
double deviation() const;
Vector operator + (const Vector& f);
Vector operator - (const Vector& f);
Vector& operator = (const Vector& );
double& operator[]( int I ) { return Vec[I]; }
const double& operator[]( int I ) const { return Vec[I]; }
friend ostream& operator<<( ostream&, const Vector& );
friend istream& operator>>( istream&, Vector& );
};
#endif
"CVector.cpp"
//
// CVector.cpp
//
// Created by gchoi on 2014. 5. 9..
// Copyright (c) 2014년 gchoi. All rights reserved.
//
#include <cmath>
#include "CVector.h"
Vector::Vector(int N, double Value) // N개 원소를 value로 초기화
{
Len = N;
Vec = new double[Len];
for(int i = 0 ; i < Len ; i++)
Vec[i] = Value;
}
Vector::Vector(double Array[], int N) // Array배열 원소 이용 5개 원소로 초기화
{
Len = N;
Vec = new double[Len];
for(int i = 0 ; i < Len ; i++)
Vec[i] = Array[i];
}
Vector::Vector(const Vector& f) // 복사 생성자
{
Len = f.Len;
Vec = new double[Len];
for(int i = 0 ; i < Len ; i++)
Vec[i] = f.Vec[i];
}
Vector::~Vector() // 소멸자
{
delete[] Vec;
}
Vector& Vector::operator = (const Vector& f) // '=' 연산자 오버로딩
{
delete[] Vec;
Len = f.Len;
Vec = new double[Len];
for(int i = 0 ; i < Len ; i++)
Vec[i] = f.Vec[i];
return (*this);
}
double Vector::maximum() const // 최대값 리턴
{
double max = 0.0;
for(int i = 0 ; i < Len ; i++)
if(max < Vec[i])
max = Vec[i];
return max;
}
double Vector::minimum() const // 최소값 리턴
{
double min = Vec[0];
for(int i = 0 ; i < Len ; i++)
if(min > Vec[i])
min = Vec[i];
return min;
}
double Vector::sum() const // 원소들의 합 리턴
{
double add = 0.0;
for(int i = 0 ; i < Len ; i++)
add += Vec[i];
return add;
}
double Vector::mean() const // 평균값 리턴
{
return sum() / Len;
}
double Vector::deviation() const // 표준편차 리턴
{
double temp = 0.0;
double avg = mean();
for(int i = 0 ; i < Len ; i++)
temp += pow(Vec[i], 2);
return sqrt(temp / Len - pow(avg, 2));
}
Vector Vector::operator+( const Vector& f)
{
if(f.Len != Len)
exit(1);
Vector temp(Len, 0.0);
for(int i = 0 ; i < f.Len ; i++)
temp.Vec[i] = f.Vec[i] + Vec[i];
return temp;
}
Vector Vector::operator-( const Vector& f)
{
if(f.Len != Len)
exit(1);
Vector temp(Len, 0.0);
for(int i = 0 ; i < f.Len ; i++)
temp.Vec[i] = Vec[i] - f.Vec[i];
return temp;
}
ostream& operator << (ostream& os, const Vector& f)
{
for(int i = 0 ; i < f.Len ; i++)
os << f.Vec[i] << ' ';
return os;
}
istream& operator >> (istream& is, Vector& f)
{
for(int i = 0 ; i < f.Len ; i++)
is >> f.Vec[i];
return is;
}
"main.cpp"
//
// main.cpp
//
// Created by gchoi on 2014. 5. 9..
// Copyright (c) 2014년 gchoi. All rights reserved.
//
#include <iostream>
#include "CVector.h"
using namespace std;
int main()
{
double A[] = { 1, 2, 3, 4, 5 };
Vector B(A, 5);
Vector C(B);
Vector D = B + C;
cout << B << endl;
cout << C << endl;
cout << D << endl;
return 0;
}
'Programming > C&C++' 카테고리의 다른 글
[C/C++] Example / clockType (0) | 2014.06.12 |
---|---|
[C/C++] Example / String Append (0) | 2014.06.12 |
[C/C++] Example / 최대공약수(GCM) 알고리즘 (0) | 2014.06.12 |
[C/C++] Example / Reading & Writing to a Binary File (0) | 2014.06.12 |
[C/C++] Example / Reading a File (0) | 2014.06.12 |
Comments