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 <iostream>
using namespace std;
class cnumber {
private:
fraction r, i;
@ -78,7 +76,7 @@ public:
return ratio;
}
friend ostream &operator<<(ostream &os, const cnumber &z) {
friend std::ostream &operator<<(std::ostream &os, const cnumber &z) {
if (z.r != 0) {
os << z.r;
if ((z.i.get_sign() == 1) && (z.i > 0)) {

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

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

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

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

Loading…
Cancel
Save