[Add] completed projects

This commit is contained in:
tiennm99 committed 2023-03-02 21:08:12 +07:00
1 parent df15f4da0b
commit 61473f02eb
12 files changed
+883

No files matched your search

+467
View File
@@ -0,0 +1,467 @@
#ifndef DSTT_H_INCLUDED
#define DSTT_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
using namespace std;
class ma_tran
{
public:
int hang, cot;
double **phan_tu;
ma_tran()
{
this->hang = 0;
this->cot = 0;
this->phan_tu = NULL;
}
ma_tran(int m, int n)
{
this->hang = m;
this->cot = n;
phan_tu = new double*[this->hang];
for (int i = 0; i < this->hang; i++)
{
phan_tu[i] = new double[this->cot];
}
}
friend istream &operator >> (istream &input, ma_tran &m)
{
m.huy();
input >> m.hang >> m.cot;
m.phan_tu = new double* [m.hang];
for (int i = 0; i < m.hang; i++)
{
m.phan_tu[i] = new double[m.cot];
}
for (int i = 0; i < m.hang; i++)
{
for (int j = 0; j < m.cot; j++)
{
input >> m.phan_tu[i][j];
}
}
return input;
}
friend ostream &operator << (ostream &output, ma_tran &m)
{
for (int i = 0; i < m.hang; i++)
{
for (int j = 0; j < m.cot; j++)
{
output << setw(5) << m.phan_tu[i][j];
}
output << endl;
}
return output;
}
bool operator == (const ma_tran &m)
{
if (this->hang != m.hang || this->cot != m.cot)
{
return 0;
}
else
{
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this-> cot; j++)
{
if (this->phan_tu[i][j] != m.phan_tu[i][j])
{
return 0;
}
}
}
}
return 1;
}
ma_tran operator + (const ma_tran &m)
{
ma_tran mt(this->hang, this->cot);
if (this->hang == m.hang && this->cot == m.cot)
{
for (int i = 0; i < mt.hang; i++)
{
for (int j = 0; j < mt.cot; j++)
{
mt.phan_tu[i][j] = this->phan_tu[i][j] + m.phan_tu[i][j];
}
}
}
else
{
mt.huy();
}
return mt;
}
ma_tran operator - (const ma_tran &m)
{
ma_tran mt(this->hang, this->cot);
if (this->hang == m.hang && this->cot == m.cot)
{
for (int i = 0; i < mt.hang; i++)
{
for (int j = 0; j < mt.cot; j++)
{
mt.phan_tu[i][j] = this->phan_tu[i][j] - m.phan_tu[i][j];
}
}
}
else
{
mt.huy();
}
return mt;
}
ma_tran operator * (const ma_tran &m)
{
ma_tran mt(this->hang, m.cot);
if (this->cot == m.hang)
{
for (int i = 0; i < mt.hang; i++)
{
for (int j = 0; j < mt.cot; j++)
{
mt.phan_tu[i][j] = 0;
for (int k = 0; k < this->cot; k++)
{
mt.phan_tu[i][j] += this->phan_tu[i][k] * m.phan_tu[k][j];
}
}
}
}
else
{
mt.huy();
}
return mt;
}
ma_tran duong_cheo_chinh()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this->cot; j++)
{
if (i == j)
{
m.phan_tu[i][j] = this->phan_tu[i][j];
}
else
{
m.phan_tu[i][j] = 0;
}
}
}
return m;
}
ma_tran tam_giac_tren()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this->cot; j++)
{
if (i <= j)
{
m.phan_tu[i][j] = this->phan_tu[i][j];
}
else
{
m.phan_tu[i][j] = 0;
}
}
}
return m;
}
ma_tran tam_giac_duoi()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this->cot; j++)
{
if (i >= j)
{
m.phan_tu[i][j] = this->phan_tu[i][j];
}
else
{
m.phan_tu[i][j] = 0;
}
}
}
return m;
}
ma_tran don_vi()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this->cot; j++)
{
if (i == j)
{
m.phan_tu[i][j] = 1;
}
else
{
m.phan_tu[i][j] = 0;
}
}
}
return m;
}
ma_tran chuyen_vi()
{
ma_tran m(this->cot, this->hang);
for (int i = 0; i < this->hang; i++)
{
for (int j = 0; j < this->cot; j++)
{
m.phan_tu[j][i] = this->phan_tu[i][j];
}
}
return m;
}
bool la_ma_tran_vuong()
{
if (this->hang == this->cot)
{
return 1;
}
else
{
return 0;
}
}
bool la_ma_tran_bac_thang()
{
int phan_tu_co_so = -1;
for (int i = 0; i < this->hang; i++)
{
int j = 0;
while (this->phan_tu[i][j] == 0)
{
j++;
}
if (j <= phan_tu_co_so && j != this->cot)
{
return 0;
}
else
{
phan_tu_co_so = j;
}
}
return 1;
}
bool la_ma_tran_doi_xung()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < m.hang; i++)
{
for (int j = 0; j < m.cot; j++)
{
m.phan_tu[i][j] = this->phan_tu[i][j];
}
}
if (m == this->chuyen_vi())
{
return 1;
}
else
{
return 0;
}
}
bool la_ma_tran_phan_doi_xung()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < m.hang; i++)
{
for (int j = 0; j < m.cot; j++)
{
m.phan_tu[i][j] = -this->phan_tu[i][j];
}
}
if (m == this->chuyen_vi())
{
return 1;
}
else
{
return 0;
}
}
double vet()
{
if (this->la_ma_tran_vuong())
{
double v = 0;
for (int i = 0; i < this->hang; i++)
{
v += this->phan_tu[i][i];
}
return v;
}
else
{
return 0;
}
}
void hang_x_nhan_y(int x, double y)
{
if (x >= 1 && x <= this->hang)
{
for (int j = 0; j < this->cot; j++)
{
this->phan_tu[x - 1][j] *= y;
}
}
}
void hang_x_cong_y_lan_hang_z(int x, double y, int z)
{
if (x >= 1 && x <= this->hang && z >= 1 && z <= this->hang)
{
for (int j = 0; j < this->cot; j++)
{
this->phan_tu[x - 1][j] += y*this->phan_tu[z - 1][j];
}
}
}
void doi_cho_2_hang(int x, int y)
{
if (x >= 1 && x <= this->hang && y >= 1 && y <= this->hang)
{
for (int j = 0; j < this->cot; j++)
{
int t = this->phan_tu[x - 1][j];
this->phan_tu[x - 1][j] = this->phan_tu[y - 1][j];
this->phan_tu[y - 1][j] = t;
}
}
}
void cot_x_nhan_y(int x, double y)
{
if (x >= 1 && x <= this->cot)
{
for (int i = 0; i < this->hang; i++)
{
this->phan_tu[i][x - 1] *= y;
}
}
}
void cot_x_cong_y_lan_cot_z(int x, double y, int z)
{
if (x >= 1 && x <= this->cot && z >= 1 && z <= this->cot)
{
for (int i = 0; i < this->hang; i++)
{
this->phan_tu[i][x - 1] += y*this->phan_tu[i][z - 1];
}
}
}
void doi_cho_2_cot(int x, int y)
{
if (x >= 1 && x <= this->cot && y >= 1 && y <= this->cot)
{
for (int i = 0; i < this->hang; i++)
{
int t = this->phan_tu[i][x - 1];
this->phan_tu[i][x - 1] = this->phan_tu[i][y - 1];
this->phan_tu[i][y - 1] = t;
}
}
}
ma_tran bac_thang()
{
ma_tran m(this->hang, this->cot);
for (int i = 0; i < m.hang; i++)
{
for (int j = 0; j < m.cot; j++)
{
m.phan_tu[i][j] = this->phan_tu[i][j];
}
}
int k = 0;
while (k < m.hang)
{
if (m.phan_tu[k][k] == 0)
{
for (int i = 0; i < this->hang; i++)
{
if (m.phan_tu[i][k] != 0)
{
m.doi_cho_2_hang(k, i);
break;
}
}
}
for (int i = k + 1; i < m.hang; i++)
{
for (int j = k + 1; j < m.cot; j++)
{
m.phan_tu[i][j] = m.phan_tu[i][j]*m.phan_tu[k][k] - m.phan_tu[i][k]*m.phan_tu[k][j];
}
m.phan_tu[i][k] = 0;
}
k++;
}
return m;
}
void huy()
{
if (phan_tu != NULL)
{
for (int i = 0; i < this->hang; i++)
{
if (phan_tu[i] != NULL)
{
delete[] phan_tu[i];
phan_tu[i] = NULL;
}
}
delete[] phan_tu;
phan_tu = NULL;
}
}
~ma_tran(){}
};
#endif // DSTT_H_INCLUDED
+13
View File
@@ -0,0 +1,13 @@
#include "DSTT.h"
int main()
{
fstream f;
f.open("ma_tran.txt");
ma_tran mt;
f >> mt;
ma_tran m = mt.bac_thang();
cout << mt << endl;
cout << m << endl;
return 0;
}
BIN
View File
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
#include <iostream>
#define MAX_SIZE 10
using namespace std;
int main()
{
int n;
cout << "Nhap n = ";
cin >> n;
while (cin.fail() || n < 0 || n > MAX_SIZE)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap khong thoa yeu cau! Vui long nhap lai: n = ";
cin >> n;
}
cout << endl;
int i, j, a = 0, chi_so_i = 0, chi_so_j = 0, chieu_dai = n - 1, mang[MAX_SIZE][MAX_SIZE];
while (a < n*n)
{
if (chieu_dai == 0)
{
mang[n/2][n/2] = ++a;
}
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
mang[chi_so_i][j] = ++a;
}
chi_so_j = j;
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
mang[i][chi_so_j] = ++a;
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
mang[chi_so_i][j] = ++a;
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
mang[i][chi_so_j] = ++a;
}
chi_so_j = j + 1;
chi_so_i = i + 1;
chieu_dai -= 2;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << mang[i][j] << '\t';
}
cout << endl;
}
return 0;
}
BIN
View File
Binary file not shown.
+95
View File
@@ -0,0 +1,95 @@
#include <iostream>
#include <cmath>
using namespace std;
void xu_ly(int n)
{
int i, j, m = 0, vong = ceil(n/2), chi_so_i = 0, chi_so_j = 0, chieu_dai = n - 1, a[n][n], b[n][n];
bool chan = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
a[i][j] = ++m;
}
}
while (vong)
{
if (chan)
{
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
b[chi_so_i][j] = a[chi_so_i][j + 1];
}
chi_so_j = j;
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
b[i][chi_so_j] = a[i + 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
b[chi_so_i][j] = a[chi_so_i][j - 1];
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
b[i][chi_so_j] = a[i - 1][chi_so_j];
}
}
else
{
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
b[i][chi_so_j] = a[i + 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
b[chi_so_i][j] = a[chi_so_i][j + 1];
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
b[i][chi_so_j] = a[i - 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
b[chi_so_i][j] = a[chi_so_i][j - 1];
}
}
chi_so_i = i + 1;
chi_so_j = j + 1;
chieu_dai -= 2;
chan = !chan;
vong--;
if (chieu_dai == 0)
{
b[n/2][n/2] = a[n/2][n/2];
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << b[i][j] << '\t';
}
cout << endl;
}
}
int main()
{
int n;
cin >> n;
while (cin.fail() || n < 0)
{
cin.clear();
cin.ignore(999, '\n');
cin >> n;
}
xu_ly(n);
return 0;
}
BIN
View File
Binary file not shown.
+155
View File
@@ -0,0 +1,155 @@
#include <iostream>
#include <cmath>
using namespace std;
typedef struct
{
int tu, mau;
} phan_so;
void nhap(phan_so &x)
{
cout << "Tu = ";
cin >> x.tu;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap loi! Tu = ";
cin >> x.tu;
}
cout << "Mau = ";
cin >> x.mau;
while (cin.fail() || x.mau == 0)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap loi! Mau = ";
cin >> x.mau;
}
}
int gcd(int a, int b)
{
a = abs(a);
b = abs(b);
if (a%b == 0)
{
return b;
}
else
{
if (b > a)
{
return gcd(b, a);
}
else
{
return gcd(b, a%b);
}
}
}
void toi_gian(phan_so &x)
{
int t = gcd(x.tu, x.mau);
x.tu /= t;
x.mau /= t;
if (x.mau < 0)
{
x.tu *= -1;
x.mau *= -1;
}
}
void xuat(phan_so x)
{
cout << x.tu;
if (x.mau != 1)
{
cout << "/" << x.mau;
}
}
phan_so cong(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau + a.mau*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so tru(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau - a.mau*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so nhan(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so chia(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau;
t.mau = a.mau*b.tu;
return t;
}
int main()
{
phan_so a, b, tong, hieu, tich, thuong;
cout << "Nhap tu va mau cua phan so a:" << endl;
nhap(a);
toi_gian(a);
cout << "Nhap tu va mau cua phan so b:" << endl;
nhap(b);
toi_gian(b);
cout << endl << "a = ";
xuat(a);
cout << endl << "b = ";
xuat(b);
cout << endl;
tong = cong(a, b);
toi_gian(tong);
cout << "a + b = ";
xuat(tong);
cout << endl;
hieu = tru(a, b);
toi_gian(hieu);
cout << "a - b = ";
xuat(hieu);
cout << endl;
tich = nhan(a, b);
toi_gian(tich);
cout << "a*b = ";
xuat(tich);
cout << endl;
if (b.tu != 0)
{
thuong = chia(a, b);
toi_gian(thuong);
cout << "a/b = ";
xuat(thuong);
cout << endl;
}
else
{
cout << "Phan so b = 0 nen khong the thuc hien phep chia!" << endl;
}
return 0;
}
BIN
View File
Binary file not shown.
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <assert.h>
void DrawSpiralLine(const int n, const int line)
{
// Throw an assertion when faulty parameters are supplied.
assert(line >= 0 && line < n && n > 0);
const int square = n * n;
int i = 0;
// Simple case: The first line of a spiral.
if (line == 0)
{
for (i = 0; i < n; i++)
{
printf("%02d ", (square - 1) - i);
}
}
// Simple case: The last line of a spiral.
else if (line == (n - 1))
{
for (i = 0; i < n; i++)
{
printf("%02d ", square - (3 * n) + 2 + i);
}
}
// Complex case: Line with other inner spirals.
else
{
// Print the first character.
printf("%02d ", square - (4 * n) + 4 + (line - 1));
// Print a line of the rest
DrawSpiralLine(n - 2, line - 1);
// Print the last character
printf("%02d ", (square - 1) - n - (line - 1));
}
}
void DrawSpiral(const int n)
{
int i = 0;
// Draw the lines of the spiral.
for (i = 0; i < n; i++)
{
DrawSpiralLine(n, i);
printf("\n");
}
}
int main(void)
{
DrawSpiral(5);
printf("\n");
DrawSpiralLine(6, 2);
return 0;
}
BIN
View File
Binary file not shown.
+41
View File
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Nhap so phan tu cua mang: ";
cin >> n;
int arr[n][n] = {0};
for (int i = 0; i < n; i++)
{
cin >> arr[0][i];
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
arr[i][j] = arr[i - 1][j] + arr[0][i + j];
}
}
int max = arr[0][0], chi_so_i = 0, chi_so_j = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (arr[i][j] >= max)
{
max = arr[i][j];
chi_so_i = i;
chi_so_j = j;
}
}
}
for (int i = chi_so_j; i <= chi_so_i + chi_so_j; i++)
{
cout << arr[0][i] << "\t";
}
cout << endl << max << endl;
return 0;
}