Dont use namespace std

main
Micke Nordin 2 years ago
parent b40998f356
commit 24c8888bd3
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -2,8 +2,6 @@
#include "fractions.hpp" #include "fractions.hpp"
#include <iostream> #include <iostream>
using namespace std;
class cnumber { class cnumber {
private: private:
fraction r, i; fraction r, i;
@ -78,7 +76,7 @@ public:
return ratio; return ratio;
} }
friend ostream &operator<<(ostream &os, const cnumber &z) { friend std::ostream &operator<<(std::ostream &os, const cnumber &z) {
if (z.r != 0) { if (z.r != 0) {
os << z.r; os << z.r;
if ((z.i.get_sign() == 1) && (z.i > 0)) { if ((z.i.get_sign() == 1) && (z.i > 0)) {

@ -2,8 +2,6 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std;
class fraction { class fraction {
private: private:
signed long long n = 0; signed long long n = 0;
@ -15,7 +13,7 @@ private:
return gcd(b, a % b); return gcd(b, a % b);
} }
int get_precision(double a) { int get_precision(double a) {
string s = to_string(a); std::string s = std::to_string(a);
int i = s.length() - 1; int i = s.length() - 1;
while (s[i] == '0') { while (s[i] == '0') {
i--; i--;
@ -108,10 +106,10 @@ public:
fraction b(that.d, that.n); fraction b(that.d, that.n);
return a * b; return a * b;
} }
friend ostream &operator<<(ostream &os, const fraction &q) { friend std::ostream &operator<<(std::ostream &os, const fraction &q) {
signed long long num = q.n; signed long long num = q.n;
signed long long den = q.d; signed long long den = q.d;
string s = ""; std::string s = "";
if (q.n < 0) { if (q.n < 0) {
s = "-"; s = "-";
num = num * -1; num = num * -1;
@ -121,7 +119,7 @@ public:
den = den * -1; den = den * -1;
} }
if (q.get_sign() == 1) { if (q.get_sign() == 1) {
string s = ""; std::string s = "";
} }
if (q.d == q.n) { if (q.d == q.n) {
os << s << 1; os << s << 1;

@ -8,20 +8,20 @@ void cnumber_main() {
cnumber a(3, 2); cnumber a(3, 2);
cnumber b(4, -3); cnumber b(4, -3);
cout << "a = " << a << endl; std::cout << "a = " << a << std::endl;
cout << "a* = " << a.conjugate() << endl; std::cout << "a* = " << a.conjugate() << std::endl;
cout << "a*a* = " << a * a.conjugate() << endl; std::cout << "a*a* = " << a * a.conjugate() << std::endl;
cout << "b = " << b << endl; std::cout << "b = " << b << std::endl;
cout << "b* = " << b.conjugate() << endl; std::cout << "b* = " << b.conjugate() << std::endl;
cout << "b*b* = " << b * b.conjugate() << endl; std::cout << "b*b* = " << b * b.conjugate() << std::endl;
cout << "a + b = " << a + b << endl; std::cout << "a + b = " << a + b << std::endl;
cout << "(a + b)* = " << (a + b).conjugate() << endl; std::cout << "(a + b)* = " << (a + b).conjugate() << std::endl;
cout << "a - b = " << a - b << endl; std::cout << "a - b = " << a - b << std::endl;
cout << "(a - b)* = " << (a - b).conjugate() << endl; std::cout << "(a - b)* = " << (a - b).conjugate() << std::endl;
cout << "a * b = " << a * b << endl; std::cout << "a * b = " << a * b << std::endl;
cout << "(a * b)* = " << (a * b).conjugate() << endl; std::cout << "(a * b)* = " << (a * b).conjugate() << std::endl;
cout << "a / b = " << a / b << endl; std::cout << "a / b = " << a / b << std::endl;
cout << "(a / b)* = " << (a / b).conjugate() << endl; std::cout << "(a / b)* = " << (a / b).conjugate() << std::endl;
} }
void fraction_main() { void fraction_main() {
@ -33,20 +33,20 @@ void fraction_main() {
fraction q(a, b); fraction q(a, b);
fraction q2(c, d); fraction q2(c, d);
fraction q3(e); fraction q3(e);
cout.precision(10); std::cout.precision(10);
cout << a << "/" << b << '=' << q << endl; std::cout << a << "/" << b << '=' << q << std::endl;
cout << a << "/" << b << '=' << q.to_double() << endl; std::cout << a << "/" << b << '=' << q.to_double() << std::endl;
cout << c << "/" << d << '=' << q2 << endl; std::cout << c << "/" << d << '=' << q2 << std::endl;
cout << c << "/" << d << '=' << q2.to_double() << endl; std::cout << c << "/" << d << '=' << q2.to_double() << std::endl;
cout << q << "+" << q2 << '=' << q + q2 << endl; std::cout << q << "+" << q2 << '=' << q + q2 << std::endl;
cout << q << "+" << q2 << '=' << (q + q2).to_double() << endl; std::cout << q << "+" << q2 << '=' << (q + q2).to_double() << std::endl;
cout << q << "-" << q2 << '=' << q - q2 << endl; std::cout << q << "-" << q2 << '=' << q - q2 << std::endl;
cout << q << "-" << q2 << '=' << (q - q2).to_double() << endl; std::cout << q << "-" << q2 << '=' << (q - q2).to_double() << std::endl;
cout << q << "*" << q2 << '=' << q * q2 << endl; std::cout << q << "*" << q2 << '=' << q * q2 << std::endl;
cout << q << "*" << q2 << '=' << (q * q2).to_double() << endl; std::cout << q << "*" << q2 << '=' << (q * q2).to_double() << std::endl;
cout << q << "/" << q2 << '=' << q / q2 << endl; std::cout << q << "/" << q2 << '=' << q / q2 << std::endl;
cout << q << "/" << q2 << '=' << (q / q2).to_double() << endl; std::cout << q << "/" << q2 << '=' << (q / q2).to_double() << std::endl;
cout << e << '=' << q3 << endl; std::cout << e << '=' << q3 << std::endl;
} }
void matrix_main() { void matrix_main() {
@ -74,42 +74,42 @@ void matrix_main() {
n[0] = c; n[0] = c;
n[1] = d; n[1] = d;
cout << "The matrix m" << endl; std::cout << "The matrix m" << std::endl;
cout << m << endl; std::cout << m << std::endl;
cout << "The determinant of matrix m:" << endl; std::cout << "The determinant of matrix m:" << std::endl;
cout << m.determinant() << endl; std::cout << m.determinant() << std::endl;
cout << "The matrix n:" << endl; std::cout << "The matrix n:" << std::endl;
cout << n << endl; std::cout << n << std::endl;
cout << "The matrix n roted by one pi radian:" << endl; std::cout << "The matrix n roted by one pi radian:" << std::endl;
cout << n.rotate_by_one_pi() << endl; std::cout << n.rotate_by_one_pi() << std::endl;
cout << "The determinant of matrix n:" << endl; std::cout << "The determinant of matrix n:" << std::endl;
cout << n.determinant() << endl; std::cout << n.determinant() << std::endl;
cout << "The matrix n's transpose:" << endl; std::cout << "The matrix n's transpose:" << std::endl;
cout << n.transpose() << endl; std::cout << n.transpose() << std::endl;
cout << "The matrix n's diagonal:" << endl; std::cout << "The matrix n's diagonal:" << std::endl;
cout << n.get_diagonal() << endl; std::cout << n.get_diagonal() << std::endl;
cout << "The matrix n's off diagonal:" << endl; std::cout << "The matrix n's off diagonal:" << std::endl;
cout << n.get_off_diagonal() << endl; std::cout << n.get_off_diagonal() << std::endl;
cout << "The vector d:" << endl; std::cout << "The vector d:" << std::endl;
cout << d << endl; std::cout << d << std::endl;
cout << "The matrix n * the vector d" << endl; std::cout << "The matrix n * the vector d" << std::endl;
cout << n * d << endl; std::cout << n * d << std::endl;
cout << "The vector d * the matrix n" << endl; std::cout << "The vector d * the matrix n" << std::endl;
cout << d * n << endl; std::cout << d * n << std::endl;
cout << "The matrix m * n" << endl; std::cout << "The matrix m * n" << std::endl;
cout << m * n << endl; std::cout << m * n << std::endl;
cout << "The matrix m + n" << endl; std::cout << "The matrix m + n" << std::endl;
cout << m + n << endl; std::cout << m + n << std::endl;
cout << "Is 2 an eigenvalue of m?" << endl; std::cout << "Is 2 an eigenvalue of m?" << std::endl;
if (m.is_eigenvalue(2)) { if (m.is_eigenvalue(2)) {
cout << "Yes!" << endl; std::cout << "Yes!" << std::endl;
} else { } else {
cout << "No!" << endl; std::cout << "No!" << std::endl;
} }
if (n.is_hermitian()) { if (n.is_hermitian()) {
cout << "The matrix n is hermitian, here is the hermitian conjugate:" std::cout << "The matrix n is hermitian, here is the hermitian conjugate:"
<< endl; << std::endl;
cout << n.hermitian_conjugate() << endl; std::cout << n.hermitian_conjugate() << std::endl;
} }
} }
void vector_main() { void vector_main() {
@ -128,30 +128,30 @@ void vector_main() {
w[0] = three; w[0] = three;
w[1] = four; w[1] = four;
w[2] = five; w[2] = five;
cout << "v:" << endl; std::cout << "v:" << std::endl;
cout << v << endl; std::cout << v << std::endl;
cout << "w:" << endl; std::cout << "w:" << std::endl;
cout << w << endl; std::cout << w << std::endl;
cout << "v + w:" << endl; std::cout << "v + w:" << std::endl;
cout << v + w << endl; std::cout << v + w << std::endl;
cout << "v - w:" << endl; std::cout << "v - w:" << std::endl;
cout << v - w << endl; std::cout << v - w << std::endl;
cout << "v * w:" << endl; std::cout << "v * w:" << std::endl;
cout << v * w << endl; std::cout << v * w << std::endl;
cout << "w * i:" << endl; std::cout << "w * i:" << std::endl;
cout << w * i << endl; std::cout << w * i << std::endl;
for (long long j = 0; j < v.get_dimention(); j++) { for (long long j = 0; j < v.get_dimention(); j++) {
cout << "Element " << j << " of v:" << endl; std::cout << "Element " << j << " of v:" << std::endl;
cout << v[j] << endl; std::cout << v[j] << std::endl;
} }
cout << "A temp vector of dimension 3" << endl << vector(3) << endl; std::cout << "A temp vector of dimension 3" << std::endl << vector(3) << std::endl;
vector k = vector(3); vector k = vector(3);
k = w; k = w;
cout << "Assignment of w to k" << endl << k << endl; std::cout << "Assignment of w to k" << std::endl << k << std::endl;
cout << "Manually set elements of k to elements of v" << endl; std::cout << "Manually set elements of k to elements of v" << std::endl;
for (long long j = 0; j < v.get_dimention(); j++) for (long long j = 0; j < v.get_dimention(); j++)
k[j] = v[j]; k[j] = v[j];
cout << k << endl; std::cout << k << std::endl;
} }
int main() { int main() {
fraction_main(); fraction_main();

@ -3,7 +3,6 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
using namespace std;
class matrix { class matrix {
private: private:
long long num_entries; long long num_entries;
@ -153,27 +152,27 @@ public:
return false; return false;
return *this == this->hermitian_conjugate(); return *this == this->hermitian_conjugate();
} }
friend ostream &operator<<(ostream &os, const matrix &m) { friend std::ostream &operator<<(std::ostream &os, const matrix &m) {
char last = '\0'; char last = '\0';
int longest = 0; int longest = 0;
for (long long i = 0; i < m.num_entries; i++) { for (long long i = 0; i < m.num_entries; i++) {
for (long long j = 0; j < m.entry_dimension; j++) { for (long long j = 0; j < m.entry_dimension; j++) {
ostringstream oss; std::ostringstream oss;
oss << m.entries[i][j]; oss << m.entries[i][j];
string s = oss.str(); std::string s = oss.str();
if (longest < s.length()) if (longest < s.length())
longest = s.length(); longest = s.length();
} }
} }
for (long long i = 0; i < m.num_entries; i++) { for (long long i = 0; i < m.num_entries; i++) {
for (long long j = 0; j < m.entry_dimension; j++) { for (long long j = 0; j < m.entry_dimension; j++) {
ostringstream iss; std::ostringstream iss;
iss << m.entries[i][j]; iss << m.entries[i][j];
string s = iss.str(); std::string s = iss.str();
int padding = longest - s.length() +1; int padding = longest - s.length() +1;
string symbols[3]; std::string symbols[3];
symbols[0] = "|"; symbols[0] = "|";
ostringstream oss; std::ostringstream oss;
oss << m.entries[i][j] ; oss << m.entries[i][j] ;
oss << std::setw(padding) << "|"; oss << std::setw(padding) << "|";
symbols[1] = oss.str(); symbols[1] = oss.str();
@ -188,7 +187,7 @@ public:
} }
} }
if (i != m.num_entries - 1) if (i != m.num_entries - 1)
os << endl << "|"; os << std::endl << "|";
} }
return os; return os;
} }

@ -2,7 +2,6 @@
#include "cnumber.hpp" #include "cnumber.hpp"
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
using namespace std;
class matrix; class matrix;
class vector { class vector {
private: private:
@ -59,23 +58,23 @@ public:
// outside of the index // outside of the index
cnumber &operator[](const long long index) { return this->entries[index]; } cnumber &operator[](const long long index) { return this->entries[index]; }
friend ostream &operator<<(ostream &os, const vector &v) { friend std::ostream &operator<<(std::ostream &os, const vector &v) {
int longest = 0; int longest = 0;
for (long long i = 0; i < v.get_dimention(); i++) { for (long long i = 0; i < v.get_dimention(); i++) {
ostringstream oss; std::ostringstream oss;
oss << v[i]; oss << v[i];
string s = oss.str(); std::string s = oss.str();
if (longest < s.length()) if (longest < s.length())
longest = s.length(); longest = s.length();
} }
for (long long i = 0; i < v.get_dimention(); i++) { for (long long i = 0; i < v.get_dimention(); i++) {
ostringstream oss; std::ostringstream oss;
oss << v[i]; oss << v[i];
string s = oss.str(); std::string s = oss.str();
int padding = longest - s.length() +1; int padding = longest - s.length() +1;
os << "|" << v[i] << std::setw(padding) << "|"; os << "|" << v[i] << std::setw(padding) << "|";
if (i != v.get_dimention() - 1) if (i != v.get_dimention() - 1)
os << endl; os << std::endl;
} }
return os; return os;
} }

Loading…
Cancel
Save