Skip to content

Commit

Permalink
090319 - update temp
Browse files Browse the repository at this point in the history
  • Loading branch information
quanhuynhUmb committed Mar 9, 2019
1 parent 489ab4e commit 72ec61b
Show file tree
Hide file tree
Showing 13 changed files with 10,948 additions and 0 deletions.
88 changes: 88 additions & 0 deletions matrix/calculateMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
ifstream fileInput("input1.txt");

if (fileInput.fail())
{
cout << "Failed to open this file!" << std::endl;
return -1;
}
int n;
fileInput >> n;
vector<vector<float>> a;
a.resize(n);
for (int i = 0; i < n; i++)
{
a[i].resize(n);
}
for(int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
float buf;
fileInput >> buf;
a[j][i] = buf;
}
}

// Print matrix test
cout << "Matrix A" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
std::cout << std::endl;
/////////////////////////////////////////////
vector<float> b;
b.resize(n);
cout << "Vector B" << endl;
for (int i = 0; i < n; i++)
{
b[i] = 1.0/n;
cout << b[i] << endl;
}
//
cout << endl << "A x B" << endl;
vector<float> ab;
ab.resize(n);
for (int k = 0; k < 500; k++)
{
for (int i = 0; i < n; i++)
{
ab[i] = 0;
for(int j = 0; j < n; j++)
{
ab[i] += a[i][j]*b[j];
}
}
// back : b = ab;
for (int i = 0; i < n; i++)
{
b[i] = ab[i];
}

// print axb
cout << "time " << (k + 1) << endl;
for (int i = 0; i < n; i++)
{
cout << setw(1) << right << setprecision(5);
cout << b[i] << endl;
}
cout << endl;

}

fileInput.close();

return 0;
}
107 changes: 107 additions & 0 deletions matrix/fractionOperator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "fractionOperator.h"
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction::Fraction(int num, int den)
{
if (den < 0)
{
num = -num;
den = -den;
}
if (den == 0)
{
den = 1; num = 0;
}
numerator = num;
denominator = den;
compact();
}
void Fraction::compact()
{
if (denominator == 0)
{
cout << "denominator = 0!!!" << endl;
numerator = 0;
denominator = 1;
return;
}
int num = abs(numerator);
int den = abs(denominator);
int rec = 0;
while (den != 0)
{
rec = num % den;
num = den;
den = rec;
}
//
numerator = (int)numerator/num;
denominator = (int)denominator/num;

if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
}

const Fraction Fraction::operator+(const Fraction& src) const
{
Fraction temp;
temp.numerator = numerator * src.denominator + src.numerator * denominator;
temp.denominator = denominator * src.denominator;
temp.compact();
return temp;
}

const Fraction Fraction::operator*(const Fraction& src) const
{
Fraction temp;
temp.numerator = numerator * src.numerator;
temp.denominator = denominator * src.denominator;
temp.compact();
return temp;
}

bool Fraction::operator==(const Fraction& src) const
{
// assumption: after each calculate, Fraction is very simple
return (((numerator == src.numerator)) && ((denominator == src.denominator)));
}

// operator overloading <<
ostream& operator<<(ostream &out, const Fraction& src)
{
out << src.numerator;
if (src.denominator != 1)
{
out << "/" << src.denominator;
}
return out;
}

istream& operator>> (istream &is, Fraction& src)
{
string buf;
fflush(stdin); // remove cache
is >> buf;
int pos = buf.find('/');
int num, den;
if (pos > 0)
{
num = stoi(buf.substr(0, pos));
den = stoi(buf.substr(pos + 1, buf.length() - pos));
}
else
{
num = stoi(buf);
den = 1;
}
src.numerator = num;
src.denominator = den;
src.compact();
return is;
}
34 changes: 34 additions & 0 deletions matrix/fractionOperator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _Fraction_h
#define _Fraction_h

#include <iostream>
#include <math.h>
#include <fstream>
#include <vector>
#include <iomanip>
#include <string>
#define FLT_EPSILON (float)1.19209e-07
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction();
Fraction(int, int);
float getF()
{
return ((float)numerator/denominator);
}
void compact();
const Fraction operator+(const Fraction&) const;
const Fraction operator*(const Fraction&) const;
bool operator==(const Fraction&) const;

// operator overloading <<
friend ostream& operator<<(ostream &out, const Fraction& src);

friend istream& operator>> (istream &is, Fraction& src);
};
#endif
6 changes: 6 additions & 0 deletions matrix/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
0.045 0.32 0.295 0.295 0.045
0.17 0.32 0.17 0.17 0.17
0.845 0.02 0.095 0.02 0.02
0.02 0.02 0.02 0.095 0.845
0.17 0.17 0.17 0.245 0.245
6 changes: 6 additions & 0 deletions matrix/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
0.2
0.2
0.2
0.2
0.2
23 changes: 23 additions & 0 deletions matrix/mainFractionOperator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "fractionOperator.h"

int main()
{
cout << endl << "mainFractionOperator.cpp + fractionOperator.cpp + fractionOperator.h" << endl;
Fraction a(1, -3), b(0, 6), c(6, 2);
cout << "c = " << c << endl;
c = a + b;
cout << "c = " << c << ", float = " << c.getF() << endl;
cout << "a = " << a << ", float = " << a.getF() << endl;
cout << "b = " << b << ", float = " << b.getF() << endl;
Fraction d(-2, 6);
cout << "d = " << d << ", float = " << d.getF() << endl;
cout << "a * b = " << a*b << endl;
bool check = (a == c);
cout << "check a == c = " << check << endl;
check = (a == d);
cout << "check a == d = " << check << endl;
Fraction test;
cin >> test;
cout << test;
return 0;
}
84 changes: 84 additions & 0 deletions matrix/matrixOperator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "matrixOperator.h"
Matrix::Matrix()
{
numerator = 0;
denominator = 1;
}
Matrix::Matrix(int num, int den)
{
if (den < 0)
{
num = -num;
den = -den;
}
if (den == 0)
{
den = 1; num = 0;
}
numerator = num;
denominator = den;
compact();
}
void Matrix::compact()
{
if (denominator == 0)
{
cout << "denominator = 0!!!" << endl;
numerator = 0;
denominator = 1;
return;
}
int num = abs(numerator);
int den = abs(denominator);
int rec = 0;
while (den != 0)
{
rec = num % den;
num = den;
den = rec;
}
//
numerator = (int)numerator/num;
denominator = (int)denominator/num;

if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
}

const Matrix Matrix::operator+(const Matrix& src) const
{
Matrix temp;
temp.numerator = numerator * src.denominator + src.numerator * denominator;
temp.denominator = denominator * src.denominator;
temp.compact();
return temp;
}

const Matrix Matrix::operator*(const Matrix& src) const
{
Matrix temp;
temp.numerator = numerator * src.numerator;
temp.denominator = denominator * src.denominator;
temp.compact();
return temp;
}

bool Matrix::operator==(const Matrix& src) const
{
// assumption: after each calculate, Matrix is very simple
return (((numerator == src.numerator)) && ((denominator == src.denominator)));
}

// operator overloading <<
ostream& operator<<(ostream &out, const Matrix& src)
{
out << src.numerator;
if (src.denominator != 1)
{
out << "/" << src.denominator;
}
return out;
}
26 changes: 26 additions & 0 deletions matrix/matrixOperator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _Matrix_h
#define _Matrix_h
#include "fractionOperator.h"

class Matrix
{
private:
int numerator;
int denominator;
public:
Matrix();
Matrix(int, int);
float getF()
{
return ((float)numerator/denominator);
}
void compact();
const Matrix operator+(const Matrix&) const;
const Matrix operator*(const Matrix&) const;
bool operator==(const Matrix&) const;

// operator overloading <<
friend ostream& operator<<(ostream &out, const Matrix& src);
};

#endif
Loading

0 comments on commit 72ec61b

Please sign in to comment.