This commit is contained in:
2018-10-27 23:57:01 +07:00
parent 5d3c489daa
commit 2e290a9d3f
99 changed files with 5340 additions and 0 deletions
BIN
View File
Binary file not shown.
+61
View File
@@ -0,0 +1,61 @@
#include <iostream>
using namespace std;
typedef struct
{
int r, c;
double **m;
} matrix;
matrix sum(matrix x, matrix y);
int main()
{
return 0;
}
matrix sum(matrix x, matrix y)
{
static matrix s;
if (x.r > y.r)
{
s.r = x.r;
}
else
{
s.r = y.r;
}
if (x.c > y.c)
{
s.c = x.c;
}
else
{
s.c = y.c;
}
double new_x[s.r][s.c] = {0};
double new_y[s.r][s.c] = {0};
for (int i = 0; i < x.r; i++)
{
for (int j = 0; j < x.c; j++)
{
new_x[i][j] = x.m[i][j];
}
}
for (int i = 0; i < y.r; i++)
{
for (int j = 0; j < y.c; j++)
{
new_y[i][j] = y.m[i][j];
}
}
for (int i = 0; i < s.r; i++)
{
for (int j = 0; j < s.c; j++)
{
s.m[i][j] = new_x[i][j] + new_y[i][j];
}
}
return s;
}
+12
View File
@@ -0,0 +1,12 @@
#include <iostream>
using namespace std;
int main()
{
const char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};
const char **cp[] = {c + 3, c + 2, c + 1, c};
const char ***cpp = cp;
cout << c[2] << " " << (char*) &c[0][3] << " " << c[3][3] << " " << (char*) &c[1][1] << endl;
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
+116
View File
@@ -0,0 +1,116 @@
#include <iostream>
using namespace std;
double **sum(double **x, double **y, int r_x, int c_x, int r_y, int c_y, int &result_x, int &result_y);
int main()
{
double **x = new double*[3];
for (int i = 0; i < 3; i++)
{
x[i] = new double[4];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
x[i][j] = i + j;
cout << x[i][j] << "\t";
}
cout << endl;
}
cout << endl;
double **y = new double*[4];
for (int i = 0; i < 34; i++)
{
y[i] = new double[3];
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
y[i][j] = i*j;
cout << y[i][j] << "\t";
}
cout << endl;
}
cout << endl;
int result_x, result_y;
double **z;
z = sum(x, y, 3, 4, 4, 3, result_x, result_y);
for (int i = 0; i < result_x; i++)
{
for (int j = 0; j < result_y; j++)
{
cout << z[i][j] << "\t";
}
cout << endl;
}
delete[] z;
z = NULL;
for (int i = 0; i < 3; i++)
{
delete[] x[i];
x[i] = NULL;
}
delete[] x;
x = NULL;
for (int i = 0; i < 4; i++)
{
delete[] y[i];
y[i] = NULL;
}
delete[] y;
y = NULL;
return 0;
}
double **sum(double **x, double **y, int r_x, int c_x, int r_y, int c_y, int &result_x, int &result_y)
{
if (r_x > r_y)
{
result_x = r_x;
}
else
{
result_x = r_y;
}
if (c_x > c_y)
{
result_y = c_x;
}
else
{
result_y = c_y;
}
static double **s = new double*[result_x];
double new_x[result_x][result_y] = {0};
double new_y[result_x][result_y] = {0};
for (int i = 0; i < r_x; i++)
{
for (int j = 0; j < c_x; j++)
{
new_x[i][j] = x[i][j];
}
}
for (int i = 0; i < r_y; i++)
{
for (int j = 0; j < c_y; j++)
{
new_y[i][j] = y[i][j];
}
}
for (int i = 0; i < result_x; i++)
{
s[i] = new double[result_y];
}
for (int i = 0; i < result_x; i++)
{
for (int j = 0; j < result_y; j++)
{
s[i][j] = new_x[i][j] + new_y[i][j];
}
}
return s;
}
+31
View File
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;
int result(int n);
int main()
{
int n = 12345;
cout << result(n);
return 0;
}
int result(int n)
{
if (n == 0)
{
return 0;
}
else
{
if (n%2 == 1)
{
return n%10 + result(n/10);
}
else
{
return result(n/10);
}
}
}
+21
View File
@@ -0,0 +1,21 @@
#include <iostream>
#include <string>
using namespace std;
void out(char* c, int length);
int main()
{
char c[] = "ab";
cout << sizeof(c);
return 0;
}
string* out(string* s, char[] c, int size_of_s, int length)
{
if (length == 1)
{
return s;
}
}
+220
View File
@@ -0,0 +1,220 @@
#include <iostream>
#include <cmath>
#define EPSILON 1.0E-10
using namespace std;
int ucln(int a, int b)
{
a = abs(a);
b = abs(b);
if (a < b)
{
return ucln(b, a);
}
else if (b == 0)
{
return a;
}
else if (a%b == 0)
{
return b;
}
else
{
return ucln(b, a%b);
}
}
class phan_so
{
private:
int tu, mau;
public:
phan_so()
{
this->tu = 0;
this->mau = 1;
}
phan_so(phan_so& x)
{
this->tu = x.tu;
this->mau = x.mau;
}
phan_so(int tu, int mau)
{
this->tu = tu;
this->mau = mau;
}
void set(int tu, int mau)
{
this->tu = tu;
this->mau = mau;
}
int get_tu()
{
return this->tu;
}
int get_mau()
{
return this->mau;
}
bool la_phan_so_toi_gian()
{
if (ucln(this->tu, this->mau) == 1)
{
return true;
}
else
{
return false;
}
}
void toi_gian()
{
if (this->mau < 0)
{
this->tu *= -1;
this->mau *= -1;
}
int uoc = ucln(this->tu, this->mau);
this->tu /= uoc;
this->mau /= uoc;
}
void xuat_hon_so()
{
int uoc = ucln(this->tu, this->mau);
this->tu /= uoc;
this->mau /= uoc;
cout << this->tu/this->mau << "(" << this->tu%this->mau << "/" << this->mau << ")";
}
void xuat_phan_so()
{
int uoc = ucln(this->tu, this->mau);
this->tu /= uoc;
this->mau /= uoc;
cout << this->tu << "/" << this->mau;
}
~phan_so()
{
};
};
bool operator> (phan_so l, phan_so r)
{
double left = l.get_tu()/l.get_mau();
double right = r.get_tu()/r.get_mau();
if (left - right > EPSILON)
{
return true;
}
else
{
return false;
}
}
bool operator< (phan_so l, phan_so r)
{
double left = l.get_tu()/l.get_mau();
double right = r.get_tu()/r.get_mau();
if (right - left > EPSILON)
{
return true;
}
else
{
return false;
}
}
bool operator>= (phan_so l, phan_so r)
{
double left = l.get_tu()/l.get_mau();
double right = r.get_tu()/r.get_mau();
if (left - right > EPSILON || fabs(left - right) < EPSILON)
{
return true;
}
else
{
return false;
}
}
bool operator<= (phan_so l, phan_so r)
{
double left = l.get_tu()/l.get_mau();
double right = r.get_tu()/r.get_mau();
if (right - left > EPSILON || fabs(left - right) < EPSILON)
{
return true;
}
else
{
return false;
}
}
bool operator== (phan_so l, phan_so r)
{
double left = l.get_tu()/l.get_mau();
double right = r.get_tu()/r.get_mau();
if (fabs(left - right) < EPSILON)
{
return true;
}
else
{
return false;
}
}
phan_so operator- (phan_so l, phan_so r)
{
int mau = l.get_mau()*r.get_mau();
int tu = l.get_tu()*r.get_mau() - l.get_mau()*r.get_tu();
phan_so phanso(tu, mau);
phanso.toi_gian();
return phanso;
}
void psac(phan_so ps)
{
ps.toi_gian();
int tu = ps.get_tu();
int mau = ps.get_mau();
phan_so phanso;
if (tu > mau)
{
cout << tu/mau << " + ";
phanso.set(tu%mau, mau);
psac(phanso);
}
else if (tu != 1)
{
int mau_1 = mau/tu + 1;
cout << "1/" << mau_1 << " + ";
phan_so phanso_1(1, mau_1);
phanso = ps - phanso_1;
psac(phanso);
}
else
{
cout << "1/" << mau;
}
}
int main()
{
int tu, mau;
cout << "Nhap tu so: ";
cin >> tu;
cout << "Nhap mau so: ";
cin >> mau;
phan_so ps(tu, mau);
cout << ps.get_tu() << "/" << ps.get_mau() << " = ";
psac(ps);
return 0;
}
+88
View File
@@ -0,0 +1,88 @@
#include <iostream>
#include <cmath>
using namespace std;
class da_thuc
{
public:
int bac;
int *he_so;
da_thuc(int bac)
{
this->bac = bac;
he_so = new int[bac];
}
void xuat()
{
if (this->he_so[0] != 0)
{
cout << this->he_so[0];
}
if (this->he_so[1] != 0)
{
cout << " + " << this->he_so[1] << "*x";
}
for (int i = 2; i < this->bac; i++)
{
if (he_so[i] > 0)
{
cout << " + " << this->he_so[i] << "*x^" << i;
}
else
{
cout << " - " << abs(this->he_so[i]) << "*x^" << i;
}
}
}
~da_thuc()
{
delete[] he_so;
he_so = NULL;
}
};
da_thuc hieu(const da_thuc &dt1, const da_thuc &dt2)
{
if (dt1.bac > dt2.bac)
{
static da_thuc dt(dt1.bac);
for (int i = 0; i < dt2.bac; i++)
{
dt.he_so[i] = dt1.he_so[i] - dt2.he_so[i];
}
for (int i = dt2.bac; i < dt1.bac; i++)
{
dt.he_so[i] = dt1.he_so[i];
}
return dt;
}
else
{
static da_thuc dt(dt2.bac);
dt = hieu(dt2, dt1);
for (int i = 0; i < dt2.bac; i++)
{
dt.he_so[i] *= -1;
}
return dt;
}
}
int main()
{
int ma[3] = {1, 3, 5};
int mb[4] = {2, 4, 6, 8};
da_thuc a(3), b(4);
for (int i = 0; i < 3; i++)
{
a.he_so[i] = ma[i];
}
for (int i = 0; i < 4; i++)
{
b.he_so[i] = mb[i];
}
da_thuc c = hieu(b, a);
c.xuat();
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
//Cau_2
#include <iostream>
using namespace std;
void dao_mang(int a[], int n)
{
for (int i = 0; i < n/2; i++)
{
int t = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = t;
}
}
int main()
{
int a[4] = {2, 3, 5, 7};
for (int i = 0; i < 4; i++)
{
cout << a[i] << "\t";
}
cout << endl;
dao_mang(a, 4);
for (int i = 0; i < 4; i++)
{
cout << a[i] << "\t";
}
cout << endl;
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
//Cau_3
#include <iostream>
using namespace std;
int chu_so_nho_nhat(int n)
{
if (n < 10)
{
return n;
}
else
{
int a = n%10;
int b = chu_so_nho_nhat(n/10);
if (a < b)
{
return a;
}
else
{
return b;
}
}
}
int main()
{
cout << chu_so_nho_nhat(24353) << endl;
return 0;
}
+88
View File
@@ -0,0 +1,88 @@
//Cau_4
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
public:
int r, c;
int **m;
Matrix(int r, int c)
{
this->r = r;
this->c = c;
m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
}
}
Matrix(const Matrix &m)
{
this->r = m.r;
this->c = m.c;
this->m = m.m;
}
friend ostream &operator<< (ostream &output, const Matrix &m)
{
for (int i = 0; i < m.r; i++)
{
for (int j = 0; j < m.c; j++)
{
output << setw(5) << m.m[i][j];
}
output << endl;
}
return output;
}
/*
~Matrix()
{
for (int i = 0; i < r; i++)
{
delete[] m[i];
m[i] = NULL;
}
delete[] m;
m = NULL;
}
*/
};
Matrix diagonal_matrix(Matrix mat)
{
static Matrix new_mat(mat.r, mat.c);
for (int i = 0; i < mat.r; i++)
{
for (int j = 0; j < mat.c; j++)
{
if (i == j)
{
new_mat.m[i][j] = mat.m[i][j];
}
else
{
new_mat.m[i][j] = 0;
}
}
}
return new_mat;
}
int main()
{
Matrix m(4, 4);
for (int i = 0; i < m.r; i++)
{
for (int j = 0; j < m.c; j++)
{
m.m[i][j] = i + j;
}
}
cout << m << endl;
Matrix dm(diagonal_matrix(m));
cout << dm << endl;
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
//Cau_5
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
class Date
{
private:
int mday;
int mon;
int year;
public:
Date()
{
time_t now = time(0);
tm *lct = localtime(&now);
this->mday = lct->tm_mday;
this->mon = 1 + lct->tm_mon;
this->year = 1900 + lct->tm_year;
}
Date(int mday, int mon, int year)
{
this->mday = mday;
this->mon = mon;
this->year = year;
}
friend ostream &operator<< (ostream &output, const Date &D)
{
output << setw(2) << setfill('0') << D.mday << "/" << setw(2) << setfill('0') << D.mon << "/" << setw(4) << D.year;
return output;
}
~Date() {}
};
int main()
{
Date d(14,7,2017);
cout << d << endl;
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
#include <iostream>
#include <string>
using namespace std;
class ExpCalculator
{
public:
string s;
int a, b, c;
ExpCalculator()
{
this->s = "";
this->a = 0;
this->b = 0;
this->c = 0;
}
void set(string s, int n)
{
if (s == "a")
{
this->a = n;
}
else if (s == "b")
{
this->b = n;
}
else if (s == "c")
{
this->c = n;
}
}
~ExpCalculator();
};
int main()
{
string s = "abcdefgh";
int a = 12;
s.replace()
cout << s;
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#define A 0.5556
#define B 32
//#define F 200.0
int main()
{
float f;
scanf("%f", &f);
//f=200;
//printf("Nhiet do trong thang do F: %.2f\n", F);
//printf("Nhiet do trong thang do C: %.2f\n", A*(F-B));
printf("Nhiet do trong thang do F: %.2f\n", f);
//printf("Nhiet do trong thang do C: %.2f\n", A*(f-B));
float c = A*(f-B);
printf("Nhiet do trong thang do C: %.2f\n", c);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c, delta, x1, x2;
cout << "Nhap he so a, b, c: ";
cin >> a >> b >> c;
delta = b*b - 4*a*c;
x1 = (-b + sqrt(delta))/(2*a);
x2 = (-b - sqrt(delta))/(2*a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
int n, nam, tuan, ngay;
cout << "Nhap so ngay: ";
cin >> n;
nam = n / 365;
ngay = n % 365;
tuan = ngay / 7;
ngay = ngay % 7;
cout << n << " ngay = " << nam << " nam + " << tuan << " tuan + " << ngay << " ngay";
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
float a1, a2, b1, b2, x, y;
cout << "Nhap he so a1, b1 cho duong thang thu nhat: ";
cin >> a1 >> b1;
cout << "Nhap he so a2, b2 cho duong thang thu hai: ";
cin >> a2 >> b2;
x = (b2 - b1)/(a1 - a2);
y = a1*x + b1;\
cout << "Giao cua hai duong thang la (x, y) = (" << x << ", " << y << ")";
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int i;
cout << "Type\t\tSize\tMinValue\tMaxValue" << endl;
for(i=0; i<52; i++)
cout << "=";
cout << endl;
cout << "char\t\t" << sizeof(char) << "\t" << CHAR_MIN << "\t\t" << CHAR_MAX << endl;
cout << "unsigned char\t" << sizeof(unsigned char) << "\t" << 0 << "\t\t" << UCHAR_MAX << endl;
cout << "short\t\t" << sizeof(short) << "\t" << SHRT_MIN << "\t\t" << SHRT_MAX << endl;
cout << "unsigned short\t" << sizeof(unsigned short) << "\t" << 0 << "\t\t" << USHRT_MAX << endl;
cout << "int\t\t" << sizeof(int) << "\t" << INT_MIN << "\t" << INT_MAX << endl;
cout << "unsigned int\t" << sizeof(unsigned int) << "\t" << 0 << "\t\t" << UINT_MAX << endl;
cout << "long\t\t" << sizeof(long) << "\t" << LONG_MIN << "\t" << LONG_MAX << endl;
cout << "unsigned long\t" << sizeof(unsigned long) << "\t" << 0 << "\t\t" << ULONG_MAX << endl;
for(i=0; i<52; i++)
cout << "=";
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265358979323846
int main()
{
//float a, b, c, angleA, angleB, angleC;
double a, b, c, angleA, angleB, angleC;
cout << "Nhap vao 3 canh tam giac: ";
cin >> a >> b >> c;
angleA = acos((b*b + c*c - a*a)/(2*b*c))*180/PI;
angleB = acos((a*a + c*c - b*b)/(2*a*c))*180/PI;
angleC = acos((a*a + b*b - c*c)/(2*a*b))*180/PI;
cout << "Goc A = " << angleA << endl;
cout << "Goc B = " << angleB << endl;
cout << "Goc C = " << angleC << endl;
cout << "Goc A + Goc B + Goc C = " << angleA + angleB + angleC << endl;
return 0;
}
BIN
View File
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double oX, oY, R, aX, aY, d;
cout << "Nhap toa do O(oX, oY): ";
cin >> oX >> oY;
cout << "Nhap ban kinh R: ";
cin >> R;
if (R>=0)
{
cout << "Nhap toa do diem A(aX, aY): ";
cin >> aX >> aY;
d = sqrt((aX-oX)*(aX-oX)+(aY-oY)*(aY-oY));
if (d>R)
cout << "A nam ngoai duong tron." << endl;
else
cout << "A nam trong duong tron." << endl;
}
else
cout << "Loi: R phai khong am. Ban vua nhap R = " << R << endl;
return 0;
}
+125
View File
@@ -0,0 +1,125 @@
#include <iostream>
using namespace std;
int main()
{
int nam, tc, dc;
cout << "Nhap nam can tinh thien can dia chi: ";
cin >> nam;
tc = nam%10;
dc = nam%12;
cout << "Nam " << nam << " la nam ";
switch(tc)
{
case 1:
{
cout << "Tan";
break;
}
case 2:
{
cout << "Nham";
break;
}
case 3:
{
cout << "Quy";
break;
}
case 4:
{
cout << "Giap";
break;
}
case 5:
{
cout << "At";
break;
}
case 6:
{
cout << "Binh";
break;
}
case 7:
{
cout << "Dinh";
break;
}
case 8:
{
cout << "Mau";
break;
}
case 9:
{
cout << "Ky";
break;
}
default:
cout << "Canh";
}
cout << " ";
switch(dc)
{
case 1:
{
cout << "Dau";
break;
}
case 2:
{
cout << "Tuat";
break;
}
case 3:
{
cout << "Hoi";
break;
}
case 4:
{
cout << "Ty";
break;
}
case 5:
{
cout << "Suu";
break;
}
case 6:
{
cout << "Dan";
break;
}
case 7:
{
cout << "Mao";
break;
}
case 8:
{
cout << "Thin";
break;
}
case 9:
{
cout << "Ty";
break;
}
case 10:
{
cout << "Ngo";
break;
}
case 11:
{
cout << "Mui";
break;
}
default:
cout << "Than";
}
return 0;
}
+134
View File
@@ -0,0 +1,134 @@
#include <iostream>
#include <cmath>
#define EPSILON 1.0E-13
using namespace std;
int main()
{
double a, b, c;
enum tam_giac
{
TAM_GIAC_THUONG,
TAM_GIAC_VUONG,
TAM_GIAC_CAN,
TAM_GIAC_DEU,
TAM_GIAC_VUONG_CAN
};
char* loai_tam_giac[] =
{
"tam giac thuong.",
"tam giac vuong.",
"tam giac can.",
"tam giac deu.",
"tam giac vuong can."
};
tam_giac tg;
cout << "Nhap do dai ba canh cua tam giac: ";
cin >> a >> b >> c;
if(
((a + b - c) > EPSILON) &&
((b + c - a) > EPSILON) &&
((c + a - b) > EPSILON)
)
{
if (fabs(a - b) <= EPSILON)
{
if (fabs(a - c) <= EPSILON)
{
tg=TAM_GIAC_DEU;
}
else
{
if (fabs(a*a + b*b - c*c) <= EPSILON)
{
tg=TAM_GIAC_VUONG_CAN;
}
else
{
tg=TAM_GIAC_CAN;
}
}
}
else
{
if (fabs(a*a + b*b - c*c) <= EPSILON)
{
tg=TAM_GIAC_VUONG;
}
else
{
tg=TAM_GIAC_THUONG;
}
}
if (tg==TAM_GIAC_THUONG)
{
if (fabs(b - c) <= EPSILON)
{
if (fabs(b - a) <= EPSILON)
{
tg=TAM_GIAC_DEU;
}
else
{
if (fabs(b*b + c*c - a*a) <= EPSILON)
{
tg=TAM_GIAC_VUONG_CAN;
}
else
{
tg=TAM_GIAC_CAN;
}
}
}
else
{
if (fabs(b*b + c*c - a*a) <= EPSILON)
{
tg=TAM_GIAC_VUONG;
}
else
{
tg=TAM_GIAC_THUONG;
}
}
}
if (tg==TAM_GIAC_THUONG)
{
if (fabs(c - a <= EPSILON))
{
if (fabs(c - b) <= EPSILON)
{
tg=TAM_GIAC_DEU;
}
else
{
if (fabs(c*c + a*a - b*b) <= EPSILON)
{
tg=TAM_GIAC_VUONG_CAN;
}
else
{
tg=TAM_GIAC_CAN;
}
}
}
else
{
if (fabs(c*c + a*a - b*b) <= EPSILON)
{
tg=TAM_GIAC_VUONG;
}
else
{
tg=TAM_GIAC_THUONG;
}
}
}
cout << "Tam giac da cho la " << loai_tam_giac[tg] << endl;
}
else
{
cout << "Do dai ba canh vua nhap khong phai la tam giac.";
}
return 0;
}
+119
View File
@@ -0,0 +1,119 @@
#include <iostream>
using namespace std;
int main()
{
int ngay, thang, nam, gt_nam, gt_thang, gia_tri, tong_gia_tri;
bool kt_nam_nhuan;
char* thu[]=
{
"Chu nhat",
"Thu hai",
"Thu ba",
"Thu tu",
"Thu nam",
"Thu sau",
"Thu bay"
};
cout << "Nhap ngay, thang, nam: ";
cin >> ngay >> thang >> nam;
gt_nam = nam/100;
if ((nam%4 == 0 && nam%100 != 0) || (nam%400 ==0))
{
kt_nam_nhuan = 1;
}
else
{
kt_nam_nhuan = 0;
}
switch (gt_nam)
{
case 19:
case 23:
{
gt_nam = 0;
break;
}
case 18:
case 22:
case 26:
{
gt_nam = 2;
break;
}
case 17:
case 21:
case 25:
{
gt_nam = 4;
break;
}
case 20:
case 24:
{
gt_nam = 6;
break;
}
}
switch (thang)
{
case 1:
{
if (kt_nam_nhuan)
{
gt_thang = 6;
break;
}
}
case 10:
{
gt_thang = 0;
break;
}
case 5:
{
gt_thang = 1;
break;
}
case 8:
{
gt_thang = 2;
}
case 2:
{
if (kt_nam_nhuan)
{
gt_thang = 2;
break;
}
}
case 3:
case 11:
{
gt_thang = 3;
break;
}
case 6:
{
gt_thang = 4;
break;
}
case 12:
{
gt_thang = 5;
break;
}
case 4:
case 7:
{
gt_thang = 6;
break;
}
}
tong_gia_tri = nam%100;
gia_tri = tong_gia_tri/4;
tong_gia_tri = tong_gia_tri + gia_tri + gt_nam + gt_thang + ngay;
tong_gia_tri = tong_gia_tri%7;
cout << "Ngay " << ngay << " thang " << thang << " nam " << nam << " la " << thu[tong_gia_tri];
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#include <iostream>
using namespace std;
int main()
{
unsigned int chi_so;
float tong_tien;
cout << "Nhap chi so dien nang tieu thu: ";
cin >> chi_so;
if (chi_so <= 50)
{
tong_tien = chi_so*1484;
}
else
{
if (chi_so <= 100)
{
tong_tien = 74200 + (chi_so - 50)*1533;
}
else
{
if (chi_so <= 200)
{
tong_tien = 150850 + (chi_so - 100)*1786;
}
else
{
if (chi_so <= 300)
{
tong_tien = 329450 + (chi_so - 200)*2242;
}
else
{
if (chi_so <= 400)
{
tong_tien = 553650 + (chi_so - 300)*2503;
}
else
{
tong_tien = 803950 + (chi_so - 400)*2587;
}
}
}
}
}
tong_tien = tong_tien*110/100;
cout << "Tien dien sinh hoat ung voi " << chi_so << "kWh la " << tong_tien << "dong.";
return 0;
}
+87
View File
@@ -0,0 +1,87 @@
#include <iostream>
using namespace std;
enum danhsachU23
{
BUI_TIEN_DUNG = 1,
BUI_TIEN_DUNG_2 = 4,
DOAN_VAN_HAU = 5,
TRAN_DINH_TRONG = 21,
VU_VAN_THANH = 17,
DO_DUY_MANH = 11,
PHAM_DUC_HUY = 8,
NGUYEN_QUANG_HAI = 19,
LUONG_XUAN_TRUONG = 6,
PHAN_VAN_DUC = 14,
PHAM_XUAN_MANH = 2,
NGUYEN_CONG_PHUONG = 10,
HA_DUC_CHINH = 13,
NGUYEN_VAN_TOAN = 9,
BUI_TIEN_DUNG_3 = 20,
NGUYEN_THANH_CHUNG = 16,
NGUYEN_PHONG_HONG_DUY = 7
};
int main()
{
int so_ao;
cout << "Nhap vao so ao cua cau thu ban can lay thong tin: ";
cin >> so_ao;
switch (so_ao)
{
case BUI_TIEN_DUNG:
cout << "Ho va ten: Bui Tien Dung, Sinh nhat: 28/02/1997, Vi tri: Thu mon, Que quan: Thanh Hoa" << endl;
break;
case BUI_TIEN_DUNG_2:
cout << "Ho va ten: Bui Tien Dung, Sinh nhat: 02/10/1995, Vi tri: Trung ve, Que quan: Ha Tinh" << endl;
break;
case DOAN_VAN_HAU:
cout << "Ho va ten: Doan Van Hau, Sinh nhat: 19/04/1999, Vi tri: Hau ve trai, Que quan: Thai Binh" << endl;
break;
case TRAN_DINH_TRONG:
cout << "Ho va ten: Tran Dinh Trong, Sinh nhat: 25/04/1997, Vi tri: Trung ve, Que quan: Ha Noi" << endl;
break;
case VU_VAN_THANH:
cout << "Ho va ten: Vu Van Thanh, Sinh nhat: 14/04/1996, Vi tri: Hau ve phai, Que quan: Hai Duong" << endl;
break;
case DO_DUY_MANH:
cout << "Ho va ten: Do Duy Manh, Sinh nhat: 29/09/1996, Vi tri: Tien ve TT, Que quan: Ha Noi" << endl;
break;
case PHAM_DUC_HUY:
cout << "Ho va ten: Pham Duc Huy, Sinh nhat: 20/01/1995, Vi tri: Tien ve TT, Que quan: Hai Duong" << endl;
break;
case NGUYEN_QUANG_HAI:
cout << "Ho va ten: Nguyen Quang Hai, Sinh nhat: 12/04/1997, Vi tri: Tien ve phai, Que quan: Ha Noi" << endl;
break;
case LUONG_XUAN_TRUONG:
cout << "Ho va ten: Luong Xuan Truong, Sinh nhat: 28/04/1995, Vi tri: Tien ve TT, Que quan: Tuyen Quang" << endl;
break;
case PHAN_VAN_DUC:
cout << "Ho va ten: Phan Van Duc, Sinh nhat: 11/04/1996, Vi tri: Tien ve canh, Que quan: Nghe An" << endl;
break;
case PHAM_XUAN_MANH:
cout << "Ho va ten: Pham Xuan Manh, Sinh nhat: 09/02/1996, Vi tri: Hau ve trai, Que quan: Nghe An" << endl;
break;
case NGUYEN_CONG_PHUONG:
cout << "Ho va ten: Nguyen Cong Phuong, Sinh nhat: 21/02/1995, Vi tri: Tien dao, Que quan: Nghe An" << endl;
break;
case HA_DUC_CHINH:
cout << "Ho va ten: Ha Duc Chinh, Sinh nhat: 22/09/1997, Vi tri: Tien dao, Que quan: Phu Tho" << endl;
break;
case NGUYEN_VAN_TOAN:
cout << "Ho va ten: Nguyen Van Toan, Sinh nhat: 12/04/1996, Vi tri: Tien dao, Que quan: Hai Duong" << endl;
break;
case BUI_TIEN_DUNG_3:
cout << "Ho va ten: Bui Tien Dung, Sinh nhat: 23/11/1998, Vi tri: Tien ve, Que quan: Thanh Hoa" << endl;
break;
case NGUYEN_THANH_CHUNG:
cout << "Ho va ten: Nguyen Thanh Chung, Sinh nhat: 08/09/1997, Vi tri: Tien ve, Que quan: Tuyen Quang" << endl;
break;
case NGUYEN_PHONG_HONG_DUY:
cout << "Ho va ten: Nguyen Duy Hong Phong, Sinh nhat: 13/06/1996, Vi tri: Tien ve, Que quan: Binh Phuoc" << endl;
break;
default:
cout << "Khong co cau thu nay" << endl;
}
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string s;
stringstream ss;
int toan_hang_1, toan_hang_2, ket_qua;
char toan_tu;
cout << "Nhap vao bieu thuc: ";
getline(cin, s);
ss << s;
ss >> toan_hang_1 >> toan_tu >> toan_hang_2;
switch (toan_tu)
{
case '+':
ket_qua = toan_hang_1 + toan_hang_2;
cout << "Ket qua cua bieu thuc " << toan_hang_1 << " " << toan_tu << " " << toan_hang_2 << " la: " << ket_qua << endl;
break;
case '-':
ket_qua = toan_hang_1 - toan_hang_2;
cout << "Ket qua cua bieu thuc " << toan_hang_1 << " " << toan_tu << " " << toan_hang_2 << " la: " << ket_qua << endl;
break;
case '*':
ket_qua = toan_hang_1 * toan_hang_2;
cout << "Ket qua cua bieu thuc " << toan_hang_1 << " " << toan_tu << " " << toan_hang_2 << " la: " << ket_qua << endl;
break;
case '/':
if (toan_hang_2 != 0)
{
ket_qua = toan_hang_1 / toan_hang_2;
cout << "Ket qua cua bieu thuc " << toan_hang_1 << " " << toan_tu << " " << toan_hang_2 << " la: " << ket_qua << endl;
}
else
{
cout << "Loi! Voi toan tu \"/\", toan hang 2 khong duoc bang 0." << endl;
}
break;
}
return 0;
}
BIN
View File
Binary file not shown.
@@ -0,0 +1,80 @@
#include <iostream>
#include <cmath>
#include <iomanip>
#define EPSILON 1.0E-10
#define PI 3.14159265359
using namespace std;
int main()
{
double x, lnx, sinx, cosx, tanx, a, b, c, i;
cout << "Nhap vao gia tri x: ";
cin >> x;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> x;
}
if (x > 0)
{
a = (x - 1)/(x + 1);
lnx = 0;
i = 0;
b = a;
while (fabs(b) > EPSILON)
{
lnx = lnx + b/(2*i + 1);
b = b*a*a;
i++;
}
lnx = lnx*2;
cout << "lnx = " << setprecision(10) << lnx << endl;
}
else
{
cout << "lnx khong xac dinh!" << endl;
}
x = x - (2*PI)*((unsigned long long)(x/(2*PI)));
sinx = 0;
i = 0;
a = x;
b = 1;
c = 1;
while (fabs(c*a/b) > EPSILON)
{
i = i + 1;
sinx = sinx + c*a/b;
a = a*x*x;
b = b*(2*i)*(2*i + 1);
c = -c;
}
x = x - (2*PI)*((unsigned long long)(x/(2*PI)));
cosx = 1;
i = 1;
a = 1;
b = 1;
c = 1;
while (fabs(c*a/b) > EPSILON)
{
a = a*x*x;
b = b*(2*i - 1)*(2*i);
c = -c;
i = i + 1;
cosx = cosx + c*a/b;
}
cout << "cos x = " << setprecision(10) << cosx << endl;
if (fabs(x - PI/2) < EPSILON || fabs(x + PI/2) < EPSILON)
{
cout << "tanx khong xac dinh!" << endl;
}
else
{
tanx = sinx/cosx;
cout << "tan x = " << setprecision(10) << tanx << endl;
}
return 0;
}
@@ -0,0 +1,33 @@
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string s;
stringstream ss;
double x, px;
int n;
cout << "Nhap chuoi cac he so: ";
getline(cin, s);
ss << s;
cout << "Nhap gia tri x: ";
cin >> x;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> x;
}
px = 0;
while (!ss.eof())
{
ss >> n;
px = px*x + n;
}
cout << "Gia tri bieu thuc la: " << px << endl;
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#include <iostream>
using namespace std;
int main()
{
char i;
for (i = '0'; i <= '9'; i++)
{
cout << "Ky tu \'" << i << "\' co ma ASCII la: " << int(i) << endl;
}
cout << endl;
for (i = 0; i <= 32; i++)
{
cout << "=";
}
cout << endl << endl;
for (i = 'a'; i <= 'z'; i++)
{
cout << "Ky tu \'" << i << "\' co ma ASCII la: " << int(i) << endl;
}
cout << endl;
for (i = 0; i <= 32; i++)
{
cout << "=";
}
cout << endl << endl;
for (i = 'A'; i <= 'Z'; i++)
{
cout << "Ky tu \'" << i << "\' co ma ASCII la: " << int(i) << endl;
}
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
int main()
{
unsigned long n, i;
unsigned long long tong = 0;
cout << "Nhap vao so nguyen duong n: ";
cin >> n;
while (cin.fail() || n <= 0)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
for (i = 1; i <= n; i = i + 2)
{
tong = tong + i*i;
}
cout << "Tong binh phuong cua cac so le tu 1 den n la: " << tong << endl;
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n, i, j;
cout << "Nhap n (n > 0, n%2 == 1): ";
cin >> n;
while (cin.fail() || n < 1 || n%2 == 0)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
for (i = 1; i <= (n/2 + 1); i++)
{
cout << setw(n/2 + 2 - i);
for (j = 1; j < 2*i; j++)
{
cout << '*';
}
cout << endl;
}
cout << endl << endl;
cout << setw(n/2 + 1) << '*' << endl;
for (i = 2; i <= n/2; i++)
{
cout << setw(n/2 + 2 - i) << '*' << setw(2*(i -1)) << '*' << endl;
}
for (i = 1; i <= n; i++)
{
cout << '*';
}
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
int main()
{
int current, previous, next;
float n, i;
cout << "Nhap n (khong am): ";
cin >> n;
while (cin.fail() || n < 0)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
if (n == 0 || n ==1)
{
cout << "F(" << n << ") = 1";
}
else
{
previous = 1;
current = 1;
for (i = 1; i < n; i++)
{
next = current + previous;
previous = current;
current = next;
}
cout << "F(" << n << ") = " << current;
}
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
#include <iostream>
#include <cmath>
#include <iomanip>
#define EPSILON 1.0E-10
#define PI 3.14159265359
using namespace std;
int main()
{
double x, sinx, a, b, c, i;
cout << "Nhap vao gia tri x: ";
cin >> x;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> x;
}
x = x - (2*PI)*((unsigned long long)(x/(2*PI)));
sinx = 0;
i = 0;
a = x;
b = 1;
c = 1;
while (fabs(c*a/b) > EPSILON)
{
i = i + 1;
sinx = sinx + c*a/b;
a = a*x*x;
b = b*(2*i)*(2*i + 1);
c = -c;
}
cout << "sinx = " << setprecision(10) << sinx;
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#include <iostream>
#include <cstdlib>
#define EPSILON 1.0E-10
using namespace std;
int main()
{
int n, i;
double x, y, dem;
cout << "Nhap vao gia tri n: ";
cin >> n;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
for (i = 0; i < n; i++)
{
x = ((double) rand() / RAND_MAX);
y = ((double) rand() / RAND_MAX);
if (x*x + y*y - 1 < EPSILON)
{
dem++;
}
}
cout << "PI = " << 4*dem/n;
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int n, i, canh = 6;
double pi, m = 1;
cout << "Nhap so lan phan chia: ";
cin >> n;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
for (i = 0; i <= n; i++)
{
pi = canh*m/2;
cout << "Lan phan chia thu " << i << ", la da giac deu " << canh << " canh, PI = " << setprecision(20) << pi << endl;
m = sqrt(2 - sqrt(4 - m*m));
canh = canh*2;
}
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int n, i;
double c1, pi1, c2, pi2, pi3;
cout << "Nhap n: ";
cin >> n;
while (cin.fail())
{
cin.fail();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
// Cach 1
c1 = 0;
pi1 = 2;
for (i = 0; i < n; i++)
{
c1 = sqrt(2+c1);
pi1 = pi1*2/c1;
}
cout << "Theo cach 1, pi1 = " << setprecision(20) << pi1 << endl;
// Cach 2
c2 = 1;
pi2 = 1;
for (i = 1; i < n; i++)
{
c2 = c2*i/(2*i + 1);
pi2 = pi2 + c2;
}
pi2 = pi2*2;
cout << "Theo cach 2, pi2 = " << setprecision(20) << pi2 << endl;
// Cach 3
pi3 = 1;
for (i = 1; i <= n; i++)
{
if (i%2)
{
pi3 = pi3*(i + 1)/i;
}
else
{
pi3 = pi3*i/(i + 1);
}
}
pi3 = pi3*2;
cout << "Theo cach 3, pi3 = " << setprecision(20) << pi3 << endl;
return 0;
}
+57
View File
@@ -0,0 +1,57 @@
#include <iostream>
using namespace std;
int main()
{
unsigned int a, b, c, d, e, emax, tong, n, so_nghiem = 0;
cout << "Nhap khoang gia tri can tim nghiem: ";
cin >> n;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap sai yeu cau! Vui long nhap lai: ";
cin >> n;
}
for (tong = 6; tong < n; tong++)
{
emax = tong/6;
for (e = 1; e < emax; e++)
{
if ((tong - e)%5 == 0)
{
d = (tong - e)/5;
if ((tong - d)%4 == 0)
{
c = (tong - d)/4;
if ((tong - c)%3 == 0)
{
b = (tong - c)/3;
if ((tong - b)%2 == 0)
{
a = (tong - b)/2;
if (tong == a + 6*e)
{
cout << "Chieu dai soi day thung cua moi gia dinh lan luot la: " << endl;
cout << "Gia dinh A: " << a << endl;
cout << "Gia dinh B: " << b << endl;
cout << "Gia dinh C: " << c << endl;
cout << "Gia dinh D: " << d << endl;
cout << "Gia dinh E: " << e << endl;
cout << "Chieu dai cua soi day chung la: " << tong << endl;
so_nghiem++;
cout << endl;
}
}
}
}
}
}
}
if (!so_nghiem)
{
cout << "Trong khoang vua cho khong tim thay gia tri thoa yeu cau." << endl;
}
return 0;
}
BIN
View File
Binary file not shown.
@@ -0,0 +1,18 @@
#include <iostream>
#define MAX_SIZE 100
using namespace std;
int main()
{
int i = 0;
char s[MAX_SIZE];
cin.getline(s, MAX_SIZE);
while (s[i] != '\0')
{
i++;
}
cout << "Do dai chuoi la: " << i << endl;
return 0;
}
@@ -0,0 +1,55 @@
#include <iostream>
#define MAX_SIZE 100
using namespace std;
int main()
{
int do_dai = 0, i, j, k;
char s[MAX_SIZE];
cin.getline(s, MAX_SIZE);
while (s[do_dai] != '\0')
{
do_dai++;
}
for (i = 0; i < do_dai; i++)
{
if (s[i] == ' ')
{
j = i;
do
{
j++;
} while (s[j] == ' ');
if (i == 0)
{
k = j - i;
for (j = i; j < do_dai - k; j++)
{
s[j] = s[j + k];
}
}
else
{
k = j - (i + 1);
for (j = i + 1; j < do_dai - k; j++)
{
s[j] = s[j + k];
}
}
do_dai -= k;
s[do_dai] = '\0';
}
}
if (s[do_dai - 1] == ' ')
{
s[do_dai - 1] = '\0';
}
for (i = 0; i < do_dai; i++)
{
cout << s[i];
}
cout << endl;
return 0;
}
@@ -0,0 +1,50 @@
#include <iostream>
#define MAX_SIZE 100
using namespace std;
int main()
{
int do_dai = 0, i, j, k;
char s[MAX_SIZE], t;
cin.getline(s, MAX_SIZE);
while (s[do_dai] != '\0')
{
do_dai++;
}
for (i = 0; i < do_dai/2; i++)
{
t = s[i];
s[i] = s[do_dai - (i + 1)];
s[do_dai - (i + 1)] = t;
}
i = 0;
while (i < do_dai)
{
if (s[i] != ' ')
{
for (j = i + 1; j <= do_dai; j++)
{
if (s[j] == ' ' || j == do_dai)
{
for (k = i; k < i + (j - i)/2; k++)
{
t = s[k];
s[k] = s[i + j - (k + 1)];
s[i + j - (k + 1)] = t;
}
break;
}
}
i = j;
}
i++;
}
for (i = 0; i < do_dai; i++)
{
cout << s[i];
}
cout << endl;
return 0;
}
+116
View File
@@ -0,0 +1,116 @@
#include <iostream>
#define MAX 100
using namespace std;
typedef struct
{
string ten;
int do_phan_giai[2];
int mau_sac;
int toc_do;
int bo_nho;
} may_in;
typedef struct
{
string ten;
float kt_mieng[2];
float kt_15_phut[4];
float kt_45_phut[2];
float kt_hoc_ky;
} mon_hoc;
typedef struct
{
string ten;
string nguoi_quan_ly;
string loai_doanh_nghiep;
float doanh_thu;
} doanh_nghiep;
typedef struct
{
string ten;
string ho;
string noi_phan_bo;
string thuc_an;
} con_chim;
int main()
{
may_in mayin[MAX];
mon_hoc monhoc[MAX];
doanh_nghiep doanhnghiep[MAX];
con_chim conchim[MAX];
mayin[0] =
{
.ten = "HP",
.do_phan_giai = {600, 600},
.mau_sac = 4,
.toc_do = 16,
.bo_nho = 64
};
monhoc[0] =
{
.ten = "Toan",
.kt_mieng = {9, 10},
.kt_15_phut = {9.5, 10, 8.5, 10},
.kt_45_phut = {9.25, 9.75},
.kt_hoc_ky = 9
};
doanhnghiep[0] =
{
.ten = "VinGroup",
.nguoi_quan_ly = "Pham Nhat Vuong",
.loai_doanh_nghiep = "Co Phan",
.doanh_thu = 65100
};
conchim[0] =
{
.ten = "Dieu hau dai bang",
.ho = "Accipitridae",
.noi_phan_bo = "Chau Phi",
.thuc_an = "thit"
};
cout << "May in 0:\tTen: " << mayin[0].ten << "\tDo phan giai: " << mayin[0].do_phan_giai[0] << " x " << mayin[0].do_phan_giai[1] << " dpi\tMau sac: " << mayin[0].mau_sac << "\tToc do: " << mayin[0].toc_do << " trang/phut\tBo nho: " << mayin[0].bo_nho << "MB" << endl;
cout << "Mon hoc 0:\tTen: " << monhoc[0].ten << "\tMieng: " << monhoc[0].kt_mieng[0] << " " << monhoc[0].kt_mieng[1] << "\t15phut: " << monhoc[0].kt_15_phut[0] << " " << monhoc[0].kt_15_phut[1] << " " << monhoc[0].kt_15_phut[2] << " " << monhoc[0].kt_15_phut[3] << "\t45 phut: " << monhoc[0].kt_45_phut[0] << " " << monhoc[0].kt_45_phut[1] << "\tThi: " << monhoc[0].kt_hoc_ky << endl;
cout << "Doanh nghiep 0:\tTen: " << doanhnghiep[0].ten << "\tNguoi quan ly: " << doanhnghiep[0].nguoi_quan_ly << "\tLoai doanh nghiep: " << doanhnghiep[0].loai_doanh_nghiep << "\tDoanh thu: " << doanhnghiep[0].doanh_thu << " ty dong" << endl;
cout << "Con chim 0:\tTen: " << conchim[0].ten << "\tHo: " << conchim[0].ho << "\tNoi phan bo: " << conchim[0].noi_phan_bo << "\tThuc an: " << conchim[0].thuc_an << endl;
may_in mayin1;
mon_hoc monhoc1;
doanh_nghiep doanhnghiep1;
con_chim conchim1;
mayin1 =
{
.ten = "Canon",
.do_phan_giai = {4800, 600},
.mau_sac = 6,
.toc_do = 8,
.bo_nho = 32
};
monhoc1 =
{
.ten = "Van",
.kt_mieng = {7, 8},
.kt_15_phut = {8.5, 9, 7.5, 8},
.kt_45_phut = {7.5, 7},
.kt_hoc_ky = 7
};
doanhnghiep1 =
{
.ten = "Hoang Anh Gia Lai",
.nguoi_quan_ly = "Doan Nguyen Duc",
.loai_doanh_nghiep = "Co Phan",
.doanh_thu = 4521.9
};
conchim1 =
{
.ten = "Chim canh cut",
.ho = "Sphenisciformes",
.noi_phan_bo = "Chau Nam Cuc",
.thuc_an = "ca"
};
mayin[1] = mayin1;
monhoc[1] = monhoc1;
doanhnghiep[1] = doanhnghiep1;
conchim[1] = conchim1;
}
+107
View File
@@ -0,0 +1,107 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#define MAX_SIZE 50
#define PI 3.14159
using namespace std;
typedef struct
{
int so_chieu = 0;
double chi_so[MAX_SIZE];
double do_dai;
} vecto;
int main()
{
ifstream f;
f.open("input.txt");
string str;
vecto vecA, vecB;
double t;
int i;
istringstream iss;
for (i = 0; i < 4; i++)
{
getline(f, str);
}
iss.str(str);
while (iss >> t || !iss.eof())
{
if (iss.fail())
{
iss.clear();
continue;
}
vecA.chi_so[vecA.so_chieu++] = t;
}
iss.clear();
getline(f, str);
iss.str(str);
while (iss >> t || !iss.eof())
{
if (iss.fail())
{
iss.clear();
continue;
}
vecB.chi_so[vecB.so_chieu++] = t;
}
if (vecA.so_chieu != vecB.so_chieu)
{
cout << "Co loi xay ra: So chieu cua vector A khac so chieu cua vector B!" << endl;
}
else
{
cout << "Vector A:";
for (i = 0; i < vecA.so_chieu; i++)
{
cout << " " << vecA.chi_so[i];
}
cout << endl << "Vector B:";
for (i = 0; i < vecB.so_chieu; i++)
{
cout << " " << vecB.chi_so[i];
}
cout << endl;
for (i = 0; i < vecA.so_chieu; i++)
{
vecA.do_dai += pow(vecA.chi_so[i], 2);
}
vecA.do_dai = sqrt(vecA.do_dai);
for (i = 0; i < vecB.so_chieu; i++)
{
vecB.do_dai += pow(vecB.chi_so[i], 2);
}
vecB.do_dai = sqrt(vecB.do_dai);
cout << "Do dai vector A: " << vecA.do_dai << endl;
cout << "Do dai vector B: " << vecB.do_dai << endl;
if (vecA.so_chieu == 3)
{
double tich_vo_huong = 0, tich_huu_huong[3];
for (i = 0; i < 3; i++)
{
tich_vo_huong += vecA.chi_so[i]*vecB.chi_so[i];
}
cout << "Tich vo huong cua vector A va vector B la: " << tich_vo_huong << endl;
tich_huu_huong[0] = vecA.chi_so[1]*vecB.chi_so[2] - vecB.chi_so[1]*vecA.chi_so[2];
tich_huu_huong[1] = -(vecA.chi_so[0]*vecB.chi_so[2] - vecB.chi_so[0]*vecA.chi_so[2]);
tich_huu_huong[2] = vecA.chi_so[0]*vecB.chi_so[1] - vecB.chi_so[0]*vecA.chi_so[1];
cout << "Tich huu huong cua vector A va vector B la: " << tich_huu_huong[0] << "i + " << tich_huu_huong[1] << "j + " << tich_huu_huong[2] << "k" << endl;
double goc, hc_vecA, hc_vecB;
goc = acos(tich_vo_huong/(vecA.do_dai*vecB.do_dai));
goc = goc/PI*180;
cout << "Goc tao voi hai vector: " << goc << "do" << endl;
hc_vecA = fabs(tich_vo_huong/vecB.do_dai);
hc_vecB = fabs(tich_vo_huong/vecA.do_dai);
cout << "Do dai hinh chieu cua vector A len vector B la: " << hc_vecA << endl;
cout << "Do dai hinh chieu cua vector B len vector A la: " << hc_vecB << endl;
}
}
f.close();
return 0;
}
+52
View File
@@ -0,0 +1,52 @@
#include <iostream>
#include <cmath>
#define MAX_SIZE 50
using namespace std;
int main()
{
float a[MAX_SIZE], trung_binh_cong = 0, do_lech_chuan = 0;
int n, i, t;
for (n = 0; n < MAX_SIZE; n++)
{
cin >> a[n];
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cin >> a[n];
}
if (a[n] < 0)
{
break;
}
}
for (i = 0; i < n; i++)
{
trung_binh_cong += a[i];
}
trung_binh_cong /= n;
for (i = 0; i < n; i++)
{
do_lech_chuan += pow((a[i] - trung_binh_cong), 2);
}
do_lech_chuan /= (n - 1);
do_lech_chuan = sqrt(do_lech_chuan);
for (i = 0; i < n/2; i++)
{
t = a[i];
a[i] = a[n - (i + 1)];
a[n - (i + 1)] = t;
}
cout << "Trung binh cong: " << trung_binh_cong << endl;
cout << "Do lech chuan: " << do_lech_chuan << endl;
cout << "Mang sau khi dao:" << endl;
for (i = 0; i < n; i++)
{
cout << a[i] << '\t';
}
cout << endl;
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;
int main()
{
int tra_cuu_nam[10] = {4, 2, 0, 6, 4, 2, 0, 6, 4, 2};
int tra_cuu_thang[12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
int tra_cuu_thang_nam_nhuan[12] = {6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
char* tra_cuu_thu[] =
{
"Chu nhat",
"Thu hai",
"Thu ba",
"Thu tu",
"Thu nam",
"Thu sau",
"Thu bay"
};
int ngay, thang, nam, tong_gia_tri, gia_tri_nam, gia_tri_thang;
cout << "Nhap ngay, thang, nam: ";
cin >> ngay >> thang >> nam;
gia_tri_nam = tra_cuu_nam[(nam/100 - 17)];
if ((nam%4 == 0 && nam%100 != 0) || (nam%400 ==0))
{
gia_tri_thang = tra_cuu_thang_nam_nhuan[thang - 1];
}
else
{
gia_tri_thang = tra_cuu_thang[thang - 1];
}
tong_gia_tri = (gia_tri_nam + (nam%100) + (nam%100)/4 + gia_tri_thang + ngay)%7;
cout << "Ngay " << ngay << " thang " << thang << " nam " << nam << " la " << tra_cuu_thu[tong_gia_tri] << endl;
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;
int main()
{
int thang_khong_nhuan[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int thang_nhuan[12]= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int thang, nam, so_ngay, so_tuan, lich_thang[6][7] = {0}, tong_gia_tri, gia_tri_nam, gia_tri_thang, i, j, ngay = 1;
int tra_cuu_nam[10] = {4, 2, 0, 6, 4, 2, 0, 6, 4, 2};
int tra_cuu_thang[12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
int tra_cuu_thang_nam_nhuan[12] = {6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
cout << "Nhap thang, nam: ";
cin >> thang >> nam;
if ((nam%4 == 0 && nam%100 != 0) || (nam%400 ==0))
{
so_ngay = thang_nhuan[thang - 1];
gia_tri_thang = tra_cuu_thang_nam_nhuan[thang - 1];
}
else
{
so_ngay = thang_khong_nhuan[thang - 1];
gia_tri_thang = tra_cuu_thang[thang - 1];
}
gia_tri_nam = tra_cuu_nam[(nam/100 - 17)];
tong_gia_tri = (gia_tri_nam + (nam%100) + (nam%100)/4 + gia_tri_thang + ngay)%7;
for (j = tong_gia_tri; j < 7; j++)
{
lich_thang[0][j] = ngay++;
}
for (i = 1; i < 6; i++)
{
for (j = 0; j < 7; j++)
{
if (ngay > so_ngay)
{
break;
}
lich_thang[i][j] = ngay++;
so_tuan = i;
}
}
cout << "Tuan\tCN\tHai\tBa\tTu\tNam\tSau\tBay" << endl;
for (i = 0; i < 60; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i <= so_tuan; i++)
{
cout << i + 1 << '\t';
for (j = 0; j < 7; j++)
{
if (lich_thang[i][j])
{
cout << lich_thang[i][j] << '\t';
}
else
{
cout << '\t';
}
}
cout << endl;
}
return 0;
}
+77
View File
@@ -0,0 +1,77 @@
#include <iostream>
using namespace std;
int main()
{
int ech[7] = {1, 1, 1, 0, -1, -1, -1};
int ech_xong[7] = {-1, -1, -1, 0, 1, 1, 1};
int buoc = 0, i, trong = 3;
bool hoanthanh = 0;
while (!hoanthanh)
{
hoanthanh = 1;
for (i = 0; i < 7; i++)
{
if (ech[i] != ech_xong[i])
{
hoanthanh = 0;
break;
}
}
cout << "Buoc thu " << buoc++ << ":\t";
for (i = 0; i < 7; i++)
{
if (ech[i])
{
if (ech[i] == 1)
{
cout << ">";
}
else
{
cout << "<";
}
}
else
{
cout << "0";
}
}
cout << endl;
if (trong > 1 && ech[trong - 2] == 1 && ech[trong - 1] == -1)
{
ech[trong - 2] = 0;
ech[trong] = 1;
trong -= 2;
}
else
{
if (trong < 6 && ech[trong + 2] == -1 && ech[trong + 1] == 1)
{
ech[trong + 2] = 0;
ech[trong] = -1;
trong += 2;
}
else
{
if (ech[trong - 1] == 1 && !(ech[trong - 2] == -1 && ech[trong + 1] == -1))
{
ech[trong - 1] = 0;
ech[trong] = 1;
trong -= 1;
}
else
{
if (ech[trong + 1] == -1 && !(ech[trong - 1] == 1 && ech[trong + 2] == 1))
{
ech[trong + 1] = 0;
ech[trong] = -1;
trong += 1;
}
}
}
}
}
return 0;
}
BIN
View File
Binary file not shown.
+46
View File
@@ -0,0 +1,46 @@
#include <iostream>
#define MAX_LEC 100
#define MAX_STU 100
using namespace std;
typedef struct
{
string id, academic_degree, research_field;
int age;
} Lecturers;
typedef struct
{
string id, name, major;
int age;
} Students;
typedef struct
{
string name, description, location, ranking;
Lecturers lecturers[MAX_LEC];
Students students[MAX_STU];
} College;
int main()
{
College a, b;
a.name = "Dai hoc Bach Khoa";
a.description = "thuoc DHQG TpHCM";
a.location = "Quan 10, TpHCM";
a.ranking = "Tot";
a.lecturers[0].id = "123456";
a.lecturers[0].academic_degree = "Tien si";
a.lecturers[0].age = 28;
a.lecturers[0].research_field = "Khoa hoc May tinh";
a.students[0].age = 19;
a.students[0].id = "1710000";
a.students[0].major = "Khoa hoc May tinh";
a.students[0].name = "Nguyen Van A";
cout << a.name << "\t" << a.description << "\t" << a.location << "\t" << a.ranking << endl;
cout << a.lecturers[0].id << "\t" << a.lecturers[0].age << "\t" << a.lecturers[0].academic_degree<< "\t" << a.lecturers[0].research_field << endl;
cout << a.students[0].id << "\t" << a.students[0].name << "\t" << a.students[0].age << "\t" << a.students[0].major << endl;
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <iostream>
#define MAX_SIZE 10
using namespace std;
int main()
{
int n, i, t, cur_size = 0, sum = 0, arr[MAX_SIZE];
cout << "Nhap vao n: ";
cin >> n;
while (cin.fail() || n <= 0 || n >= 10)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap khong thoa yeu cau!. Vui long nhap lai: ";
cin >> n;
}
for (i = 0; i < n; i++)
{
cin >> t;
if (t%2 == 0 && t%3 == 0)
{
arr[cur_size++] = t;
}
}
for (i = 0; i < cur_size; i++)
{
cout << arr[i] << "\t";
sum += arr[i];
}
cout << endl << sum << endl;
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#define MAX_SIZE 100
using namespace std;
int main()
{
srand(time(NULL));
int i, m;
unsigned char a[MAX_SIZE], c;
int count_char[26];
for (i = 0; i < 26; i++)
{
count_char[i] = 0;
}
cout << "Nhap vao m: ";
cin >> m;
while(cin.fail() || m < 0 || m > MAX_SIZE)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap khong thoa yeu cau! Vui long nhap lai: ";
cin >> m;
}
for (i = 0; i < m; i++)
{
a[i] = 'a' + rand()*26/RAND_MAX;
}
cout << "Chuoi vua tao:" << endl;
for (i = 0; i < m; i++)
{
cout << a[i];
}
cout << endl;
for (i = 0; i < m; i++)
{
count_char[a[i] - 'a']++;
}
for (c = 'a'; c <= 'z'; c++)
{
cout << "Tan suat xuat hien cua ky tu " << c << " la: " << setprecision(3) << (float) count_char[c - 'a']/m << "%" << endl;
}
cout << endl;
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
#include <iostream>
#include <cmath>
#define N 1000
#define M 50
using namespace std;
int main()
{
int arr[M], arr_sum[M], i, j, x, t, c;
for (i = 0; i < M; i++)
{
arr_sum[i] = 0;
}
for (x = 1; x <= N; x++)
{
t = x;
for (i = M - 1; i >= 0; i--)
{
arr[i] = t%10;
t /= 10;
}
for (i = 1; i < x; i++)
{
c = 0;
for (j = M - 1; j >= 0; j--)
{
arr[j] = arr[j]*x + c;
c = arr[j]/10;
arr[j] %= 10;
}
}
c = 0;
for (j = M - 1; j >= 0; j--)
{
arr_sum[j] += (arr[j] + c);
c = arr_sum[j]/10;
arr_sum[j] %= 10;
}
}
for (i = 0; i <= M - 1; i++)
{
cout << arr_sum[i];
}
return 0;
}
Binary file not shown.
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
#include <cstring>
#include <iomanip>
#define SO_SAO 33
using namespace std;
int main()
{
string ten_sinh_vien, ma_so_sinh_vien;
int i, khoang_trong;
cout << "Nhap vao ten sinh vien: ";
getline(cin, ten_sinh_vien);
cout << "Nhap vao ma so sinh vien: ";
getline(cin, ma_so_sinh_vien);
for (i = 0; i < SO_SAO; i++)
{
cout << "*";
}
khoang_trong = ((SO_SAO - 2) - ten_sinh_vien.length())/2;
cout << endl << "*" << setw(khoang_trong + ten_sinh_vien.length()) << ten_sinh_vien << setw((SO_SAO - 1) - (khoang_trong + ten_sinh_vien.length())) << "*" << endl;
for (i = 0; i < SO_SAO; i++)
{
cout << "*";
}
khoang_trong = ((SO_SAO - 2) - ma_so_sinh_vien.length())/2;
cout << endl << "*" << setw(khoang_trong + ma_so_sinh_vien.length()) << ma_so_sinh_vien << setw((SO_SAO - 1) - (khoang_trong + ma_so_sinh_vien.length())) << "*" << endl;
for (i = 0; i < SO_SAO; i++)
{
cout << "*";
}
cout << endl;
return 0;
}
+262
View File
@@ -0,0 +1,262 @@
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#define SO_CAU_THU 11
using namespace std;
typedef struct
{
int so_ao, so_tran_dau;
string ho_ten, vi_tri;
float chieu_cao, can_nang;
} cau_thu;
int main()
{
cau_thu cauthu[SO_CAU_THU];
bool b[SO_CAU_THU];
int i, sotrandau;
float chieucao, cannang;
string s, st;
char c;
stringstream ss, sst;
for (i = 0; i < SO_CAU_THU; i++)
{
b[i] = 1;
cout << "STT: " << i + 1 << endl;
cout << "So ao: ";
cin >> cauthu[i].so_ao;
cout << "Ho ten: ";
fflush(stdin);
getline(cin, cauthu[i].ho_ten);
cout << "Vi tri:";
fflush(stdin);
getline(cin, cauthu[i].vi_tri);
cout << "Chieu cao: ";
cin >> cauthu[i].chieu_cao;
cout << "Can nang: ";
cin >> cauthu[i].can_nang;
cout << "So tran dau: ";
cin >> cauthu[i].so_tran_dau;
cout << endl;
}
cout << "Nhap vao truy van thong tin cau thu:" << endl;
cout << "Chieu cao: ";
cin >> s;
ss << s;
ss >> c;
s = "";
s += c;
ss >> c;
if (c == '=')
{
s += c;
ss >> chieucao;
}
else
{
sst << c;
ss >> st;
sst << st;
sst >> chieucao;
}
if (ss == ">")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].chieu_cao <= chieucao)
{
b[i] = 0;
}
}
}
else if (ss == ">=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].chieu_cao < chieucao)
{
b[i] = 0;
}
}
}
else if (ss == "<")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].chieu_cao >= chieucao)
{
b[i] = 0;
}
}
}
else if (ss == "<=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].chieu_cao > chieucao)
{
b[i] = 0;
}
}
}
else if (ss == "==")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].chieu_cao != chieucao)
{
b[i] = 0;
}
}
}
cout << "Can nang: ";
cin >> s;
ss << s;
ss >> c;
s = "";
s += c;
ss >> c;
if (c == '=')
{
s += c;
ss >> cannang;
}
else
{
sst << c;
ss >> st;
sst << st;
sst >> cannang;
}
if (ss == ">")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].can_nang <= cannang)
{
b[i] = 0;
}
}
}
else if (ss == ">=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].can_nang < cannang)
{
b[i] = 0;
}
}
}
else if (ss == "<")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].can_nang >= cannang)
{
b[i] = 0;
}
}
}
else if (ss == "<=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].can_nang > cannang)
{
b[i] = 0;
}
}
}
else if (ss == "==")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].can_nang != cannang)
{
b[i] = 0;
}
}
}
cout << "So tran dau: ";
cin >> s;
ss << s;
ss >> c;
s = "";
s += c;
ss >> c;
if (c == '=')
{
s += c;
ss >> sotrandau;
}
else
{
sst << c;
ss >> st;
sst << st;
sst >> sotrandau;
}
if (ss == ">")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].so_tran_dau <= sotrandau)
{
b[i] = 0;
}
}
}
else if (ss == ">=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].so_tran_dau < sotrandau)
{
b[i] = 0;
}
}
}
else if (ss == "<")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].so_tran_dau >= sotrandau)
{
b[i] = 0;
}
}
}
else if (ss == "<=")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].so_tran_dau > sotrandau)
{
b[i] = 0;
}
}
}
else if (ss == "==")
{
for (i = 0; i < SO_CAU_THU; i++)
{
if (cauthu[i].so_tran_dau != sotrandau)
{
b[i] = 0;
}
}
}
for (i = 0; i < SO_CAU_THU; i++)
{
if (b[i])
{
cout << cauthu[i].so_ao << "\t" << cauthu[i].ho_ten << "\t" << cauthu[i].vi_tri << "\t" << cauthu[i].chieu_cao << "\t" << cauthu[i].can_nang << "\t" << cauthu[i].so_tran_dau << endl;
}
}
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
#include <iostream>
#include <cmath>
#define EPSILON 1E-10
#define LN10 2.30258509299404568402
using namespace std;
int main()
{
double x;
cout << "Nhap x: ";
cin >> x;
while (cin.fail())
{
cout << "Nhap khong thoa yeu cau. Vui long nhap lai: ";
cin.clear();
cin.ignore(999, '\n');
cin >> x;
}
if (x <= 0)
{
cout << "ln x khong xac dinh";
}
else
{
double lnx, a, b, c;
int d;
d = 0;
while (x > 1)
{
x /= 10;
d++;
}
lnx = 0;
a = 1;
b = x - 1;
c = 1;
while (fabs(a*b/c) > EPSILON)
{
lnx += a*b/c;
a *= -1;
b *= (x - 1);
c++;
}
lnx += (d*LN10);
cout << "lnx = " << lnx << endl;
}
return 0;
}
+94
View File
@@ -0,0 +1,94 @@
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#define MAX_SIZE 100
using namespace std;
int main()
{
int mang[MAX_SIZE], mang_binh_phuong[MAX_SIZE], mang_dao[MAX_SIZE], i, n, lua_chon, min, max;
float trung_binh;
string s;
stringstream ss;
cout << "Nhap vao mang cac so nguyen: ";
fflush(stdin);
getline(cin, s);
ss << s;
n = 0;
while (!ss.eof())
{
ss >> mang[n++];
}
min = max = mang[0];
trung_binh = 0;
for (i = 0; i < n; i++)
{
if (mang[i] < min)
{
min = mang[i];
}
if (mang[i] > max)
{
max = mang[i];
}
trung_binh += mang[i];
mang_binh_phuong[i] = mang[i]*mang[i];
mang_dao[(n - 1) - i] = mang[i];
}
trung_binh /= n;
lua_chon = 0;
cout << "Ban co cac lua chon sau:\n1. In so phan tu\n2. In gia tri lon nhat\n3. In gia tri nho nhat\n4. In gia tri trung binh\n5. Mang goc da nhap\n6. Mang binh phuong\n7. Mang dao\n8. Thoat" << endl;
while (lua_chon != 8)
{
cout << "Nhap lua chon: ";
cin >> lua_chon;
switch (lua_chon)
{
case 1:
cout << "So phan tu cua mang la: " << n << endl;
break;
case 2:
cout << "Phan tu lon nhat cua mang la: " << max << endl;
break;
case 3:
cout << "Phan tu nho nhat cua mang la: " << min << endl;
break;
case 4:
cout << "Gia tri trung binh cua mang la: " << trung_binh << endl;
break;
case 5:
cout << "Mang goc:" << endl;
for (i = 0; i < n; i++)
{
cout << mang[i] << "\t";
}
cout << endl;
break;
case 6:
cout << "Mang binh phuong:" << endl;
for (i = 0; i < n; i++)
{
cout << mang_binh_phuong[i] << "\t";
}
cout << endl;
break;
case 7:
cout << "Mang dao:" << endl;
for (i = 0; i < n; i++)
{
cout << mang_dao[i] << "\t";
}
cout << endl;
break;
case 8:
cout << "Ket thuc chuong trinh." << endl;
break;
default:
cout << "Nhap vao khong hop le!" << endl;
}
}
return 0;
}
+193
View File
@@ -0,0 +1,193 @@
#include <iostream>
#include <cstring>
#include <sstream>
#include <cstdio>
using namespace std;
typedef struct
{
int d, M, y, h, m, s;
} time;
int main()
{
stringstream ss;
string s;
int delta_time[6] = {0, 0, 0, 0, 0, 0};
int days[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int i, sec, sec1, sec2;
char c;
time t1, t2;
bool b, leap1 = 0, leap2 = 0;
cout << "Nhap vao 2 thoi gian theo dinh dang dd/MM/yyyy hh:mm:ss" << endl;
cout << "Nhap vao thoi gian thu nhat: ";
fflush(stdin);
getline(cin, s);
ss << s;
ss >> t1.d >> c >> t1.M >> c >> t1.y >> t1.h >> c >> t1.m >> c >> t1.s;
ss.clear();
cout << "Nhap vao thoi gian thu hai: ";
fflush(stdin);
getline(cin, s);
ss << s;
ss >> t2.d >> c >> t2.M >> c >> t2.y >> t2.h >> c >> t2.m >> c >> t2.s;
if ((t1.y%4 == 0 && t1.y%100 != 0) || (t1.y%400 ==0))
{
leap1 = 1;
}
if ((t2.y%4 == 0 && t2.y%100 != 0) || (t2.y%400 ==0))
{
leap2 = 1;
}
b = 1;
if (t1.y > t2.y)
{
b = 0;
}
else
{
if (t1.y == t2.y)
{
if (t1.M > t2.M)
{
b = 0;
}
else
{
if (t1.M == t2.M)
{
if (t1.d > t2.d)
{
b = 0;
}
else
{
if (t1.d == t2.d)
{
if (t1.h > t2.h)
{
b = 0;
}
else
{
if (t1.h == t2.h)
{
if (t1.m > t2.m)
{
b = 0;
}
else
{
if (t1.m == t2.m)
{
if (t1.s >= t2.s)
{
b = 0;
}
}
}
}
}
}
}
}
}
}
}
if (b)
{
time t;
t = t1;
t1 = t2;
t2 = t;
}
sec = 0;
for (i = t2.y + 1; i < t1.y; i++)
{
if ((i%4 == 0 && i%100 != 0) || (i%400 == 0))
{
sec += 366*24*3600;
}
else
{
sec += 365*24*3600;
}
}
for (i = t2.M + 1; i <= 12; i++)
{
sec += days[leap2][i - 1]*24*3600;
}
for (i = 1; i < t1.M; i++)
{
sec += days[leap1][i - 1]*24*3600;
}
for (i = t2.d + 1; i <= days[leap2][t2.M - 1]; i++)
{
sec += 24*3600;
}
for (i = 1; i < t1.d; i++)
{
sec += 24*3600;
}
sec1 = t1.h*3600 + t1.m*60 + t1.s;
sec2 = t2.h*3600 + t2.m*60 + t2.s;
sec += (sec1 + (24*3600 - sec2));
if (t1.y == t2.y)
{
if (leap1)
{
sec -= 366*24*3600;
}
else
{
sec -= 365*24*3600;
}
}
delta_time[5] = sec%60;
delta_time[4] = sec/60;
delta_time[3] = delta_time[4]/60;
delta_time[4] %= 60;
delta_time[2] = delta_time[3]/24;
delta_time[3] %= 24;
delta_time[1] = delta_time[2]/30;
delta_time[2] %= 30;
delta_time[0] = delta_time[1]/12;
delta_time[1] %= 12;
cout << "Do lech giay: " << sec << " giay." << endl;
b = 1;
cout << "Do lech thoi gian: ";
if (delta_time[0])
{
cout << delta_time[0] << " nam ";
b = 0;
}
if (delta_time[1])
{
cout << delta_time[1] << " thang ";
b = 0;
}
if (delta_time[2])
{
cout << delta_time[2] << " ngay ";
b = 0;
}
if (delta_time[3])
{
cout << delta_time[3] << " gio ";
b = 0;
}
if (delta_time[4])
{
cout << delta_time[4] << " phut ";
b = 0;
}
if (delta_time[5] || b)
{
cout << delta_time[5] << " giay";
}
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
srand(time(NULL));
unsigned int n;
cin >> n;
double chieu_dai_1, chieu_dai_2, tich_vo_huong;
chieu_dai_1 = chieu_dai_2 = tich_vo_huong = 0;
double *vector_1 = new double[n];
double *vector_2 = new double[n];
int i;
for (i = 0; i < n; i++)
{
vector_1[i] = (-10 + (double) rand()/RAND_MAX*20);
vector_2[i] = (-10 + (double) rand()/RAND_MAX*20);
}
cout << "Vector thu nhat: ";
for (i = 0; i < n; i++)
{
cout << vector_1[i] << "\t";
}
cout << endl;
cout << "Vector thu hai: ";
for (i = 0; i < n; i++)
{
cout << vector_2[i] << "\t";
}
cout << endl;
for (i = 0; i < n; i++)
{
chieu_dai_1 += pow(vector_1[i], 2);
chieu_dai_2 += pow(vector_2[i], 2);
tich_vo_huong += vector_1[i]*vector_2[i];
}
chieu_dai_1 = sqrt(chieu_dai_1);
chieu_dai_2 = sqrt(chieu_dai_2);
cout << "Do dai vector thu nhat: " << chieu_dai_1 << endl;
cout << "Do dai vector thu hai: " << chieu_dai_2 << endl;
cout << "Tich vo huong cua hai vector: " << tich_vo_huong << endl;
delete[] vector_1;
delete[] vector_2;
vector_1 = vector_2 = NULL;
return 0;
}
+186
View File
@@ -0,0 +1,186 @@
//Cach 1
/*
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#define MAX_SIZE 10
using namespace std;
int main()
{
srand(time(NULL));
unsigned int n, i, j, arr[MAX_SIZE][MAX_SIZE], t;
cin >> n;
cout << setprecision(4);
int *ptr = &arr[0][0];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
*(ptr + n*i +j) = (-40 + (int) rand()*90/RAND_MAX);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j < i)
{
cout << *(ptr + n*i +j);
}
cout << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j > i)
{
cout << *(ptr + n*i +j);
}
cout << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << *(ptr + n*i +j) << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n/2; i++)
{
t = *(ptr + (n + 1)*i);
*(ptr + (n + 1)*i) = *(ptr + (n + 1)*(n - 1 - i));
*(ptr + (n + 1)*(n - 1 - i)) = t;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << *(ptr + n*i +j) << "\t";
}
cout << endl;
}
return 0;
}
*/
//================================================================================================================================
//Cach 2
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
unsigned int n, i, j, t;
cin >> n;
cout << setprecision(4);
int **ptr = new int*[n];
for (i = 0; i < n; i++)
{
ptr[i] = new int[n];
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
ptr[i][j] = (-40 + (int) (rand()*90/RAND_MAX));
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j < i)
{
cout << ptr[i][j];
}
cout << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j > i)
{
cout << ptr[i][j];
}
cout << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << ptr[i][j] << "\t";
}
cout << endl;
}
for (i = 0; i < 16; i++)
{
cout << "=";
}
cout << endl;
for (i = 0; i < n/2; i++)
{
t = ptr[i][i];
ptr[i][i] = ptr[n - 1 - i][n - 1 - i];
ptr[n - 1 - i][n - 1 - i] = t;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << ptr[i][j] << "\t";
}
cout << endl;
}
for (i = 0; i < n; i++)
{
delete[] ptr[i];
ptr[i] = NULL;
}
delete[] ptr;
ptr[i] = NULL;
return 0;
}
+108
View File
@@ -0,0 +1,108 @@
//Cach 1
/*
#include <iostream>
#include <cmath>
#define MAX_SIZE 10
using namespace std;
int main()
{
int i, t, n = 0, arr[MAX_SIZE];
double trung_binh = 0, do_lech_chuan = 0;
int *ptr = arr;
do
{
cin >> ptr[n++];
} while (n < MAX_SIZE && ptr[n - 1] >= 0);
if (ptr[n - 1] < 0)
{
n--;
}
for (i = 0; i < n; i++)
{
cout << ptr[i] << "\t";
}
cout << endl;
for (i = 0; i < n; i++)
{
trung_binh += ptr[i];
}
trung_binh /= n;
for (i = 0; i < n; i++)
{
do_lech_chuan += pow(ptr[i] - trung_binh, 2);
}
do_lech_chuan /= (n - 1);
for (i = 0; i < n/2; i++)
{
t = ptr[i];
ptr[i] = ptr[n - 1 - i];
ptr[n - 1 - i] = t;
}
cout << "Gia tri trung binh: " << trung_binh << endl;
cout << "Do lech chuan: " << do_lech_chuan << endl;
cout << "Day so sau khi dao: " << endl;
for (i = 0; i < n; i++)
{
cout << ptr[i] << "\t";
}
cout << endl;
return 0;
}
*/
//================================================================
//Cach 2
#include <iostream>
#include <cmath>
#define MAX_SIZE 10
using namespace std;
int main()
{
int i, t, n = 0, *ptr = new int[MAX_SIZE];
double trung_binh = 0, do_lech_chuan = 0;
do
{
cin >> ptr[n++];
} while (n < MAX_SIZE && ptr[n - 1] >= 0);
if (ptr[n - 1] < 0)
{
n--;
}
for (i = 0; i < n; i++)
{
cout << ptr[i] << "\t";
}
cout << endl;
for (i = 0; i < n; i++)
{
trung_binh += ptr[i];
}
trung_binh /= n;
for (i = 0; i < n; i++)
{
do_lech_chuan += pow(ptr[i] - trung_binh, 2);
}
do_lech_chuan /= (n - 1);
for (i = 0; i < n/2; i++)
{
t = ptr[i];
ptr[i] = ptr[n - 1 - i];
ptr[n - 1 - i] = t;
}
cout << "Gia tri trung binh: " << trung_binh << endl;
cout << "Do lech chuan: " << do_lech_chuan << endl;
cout << "Day so sau khi dao: " << endl;
for (i = 0; i < n; i++)
{
cout << ptr[i] << "\t";
}
cout << endl;
delete[] ptr;
ptr = NULL;
return 0;
}
+48
View File
@@ -0,0 +1,48 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
unsigned long hang, cot, i, j;
cout << "Nhap so hang va so cot cua ma tran: ";
cin >> hang >> cot;
int **ma_tran = new int*[hang];
double *tan_suat = new double[256];
for (i = 0; i < 256; i++)
{
tan_suat[i] = 0;
}
for (i = 0; i < hang; i++)
{
ma_tran[i] = new int[cot];
}
for (i = 0; i < hang; i++)
{
for (j = 0; j < cot; j++)
{
ma_tran[i][j] = rand()*256/RAND_MAX;
cout << ma_tran[i][j] << "\t";
tan_suat[ma_tran[i][j]]++;
}
cout << endl;
}
for (i = 0; i < 256; i++)
{
tan_suat[i] /= (hang*cot);
cout << "Tan suat xuat hien cua gia tri " << i << " la: " << tan_suat[i] << "%." << endl;
}
for (i = 0; i < hang; i++)
{
delete[] ma_tran[i];
ma_tran[i] = NULL;
}
delete[] ma_tran;
ma_tran = NULL;
delete[] tan_suat;
tan_suat = NULL;
return 0;
}
+69
View File
@@ -0,0 +1,69 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAX_SIZE 100
using namespace std;
int main()
{
srand(time(NULL));
int **ptr = new int* [MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
{
ptr[i] = new int[MAX_SIZE];
}
int n;
cout << "Nhap gia tri n: ";
cin >> n;
while(cin.fail() || n < 1 || n > MAX_SIZE)
{
cout << "Nhap khong thoa yeu cau! Vui long nhap lai: n = ";
cin.clear();
cin.ignore(999, '\n');
cin >> n;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)\
{
ptr[i][j] = -40 + (int) (rand()*90/RAND_MAX);
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)\
{
cout << ptr[i][j] << "\t";
}
cout << endl;
}
for (int i = 0; i < 64; i++)
{
cout << "=";
}
cout << endl;
for (int i = 0; i < n/2; i++)
{
int t = ptr[i][i];
ptr[i][i] = ptr[n - 1 - i][n - 1 - i];
ptr[n - 1 - i][n - 1 - i] = t;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)\
{
cout << ptr[i][j] << "\t";
}
cout << endl;
}
for (int i = 0; i < MAX_SIZE; i++)
{
delete[] ptr[i];
ptr[i] = NULL;
}
delete[] ptr;
ptr = NULL;
return 0;
}
+67
View File
@@ -0,0 +1,67 @@
#include <iostream>
#define N 3
using namespace std;
int main()
{
int **m = new int*[N];
for (int i = 0; i < N; i++)
{
m[i] = new int[N];
}
int *v = new int[N];
int *r = new int[N];
cout << "N = " << N << endl;
cout << "Nhap vao cac phan tu cua ma tran m(" << N << "x" << N << "):" << endl;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> m[i][j];
}
}
cout << "Nhap vao cac phan tu cua vector v(" << N << "x1):" << endl;
for (int i = 0; i < N; i++)
{
cin >> v[i];
}
for (int i = 0; i < N; i++)
{
r[i] = 0;
for (int j = 0; j < N; j++)
{
r[i] += m[i][j]*v[j];
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << m[i][j] << "\t";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++)
{
cout << v[i] << endl;
}
cout << endl;
for (int i = 0; i < N; i++)
{
cout << r[i] << endl;
}
for (int i = 0; i < N; i++)
{
delete[] m[i];
m[i] = NULL;
}
delete[] m;
m = NULL;
delete[] v;
v = NULL;
delete[] r;
r = NULL;
}
+74
View File
@@ -0,0 +1,74 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#define N 3
using namespace std;
int main()
{
srand(time(NULL));
int **m = new int*[N];
for (int i = 0; i < N; i++)
{
m[i] = new int[i + 1];
}
int *y = new int[N];
double *x = new double[N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j <= i; j++)
{
m[i][j] = -10 + (int) (rand()*20/RAND_MAX);
if (i == j)
{
while (m[i][j] == 0)
{
m[i][j] = -10 + (int) (rand()*20/RAND_MAX);
}
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j <= i; j++)
{
cout << m[i][j] << "\t";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i ++)
{
y[i] = -10 + (int) (rand()*20/RAND_MAX);
}
for (int i = 0; i < N; i++)
{
cout << y[i] << endl;
}
cout << endl;
for (int i = 0; i < N; i++)
{
x[i] = y[i];
for (int j = 0; j < i; j++)
{
x[i] -= m[i][j]*x[j];
}
x[i]/=m[i][i];
}
for (int i = 0; i < N; i++)
{
cout << x[i] << endl;
}
for (int i = 0; i < N; i++)
{
delete[] m[i];
m[i] = NULL;
}
delete[] y;
y = NULL;
delete[] x;
x = NULL;
return 0;
}
+92
View File
@@ -0,0 +1,92 @@
#include <iostream>
#define MAX_SIZE 100
using namespace std;
int main()
{
int ra, ca, rb, cb;
int **a = new int*[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
{
a[i] = new int[MAX_SIZE];
}
int **b = new int*[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
{
b[i] = new int[MAX_SIZE];
}
cout << "Nhap so hang va so cot cua ma tran a: ";
cin >> ra >> ca;
cout << "Nhap so hang ca so cot cua ma tran b: ";
cin >> rb >> cb;
if (rb != ca)
{
cout << "Khong the nhan hai ma tran co kich thuoc " << ra << "x" << ca << " va " << rb << "x" << cb << endl;
}
else
{
int **c = new int*[ra];
for (int i = 0; i < ra; i++)
{
c[i] = new int[cb];
}
cout << "Nhap cac phan tu cua ma tran a: " << endl;
for (int i = 0; i < ra; i++)
{
for (int j = 0; j < ca; j++)
{
cin >> a[i][j];
}
}
cout << "Nhap cac phan tu cua ma tran b: " << endl;
for (int i = 0; i < rb; i++)
{
for (int j = 0; j < cb; j++)
{
cin >> b[i][j];
}
}
for (int i = 0; i < ra; i++)
{
for (int j = 0; j < cb; j++)
{
c[i][j] = 0;
for (int k = 0; k < ca; k++)
{
c[i][j] += a[i][k]*b[k][j];
}
}
}
for (int i =0; i < ra; i++)
{
for (int j = 0; j < cb; j++)
{
cout << c[i][j] << "\t";
}
cout << endl;
}
for (int i = 0; i < ra; i++)
{
delete[] c[i];
c[i] = NULL;
}
delete[] c;
c = NULL;
for (int i = 0; i < MAX_SIZE; i++)
{
delete[] b[i];
b[i] = NULL;
}
delete b;
b = NULL;
for (int i = 0; i < MAX_SIZE; i++)
{
delete[] a[i];
a[i] = NULL;
}
delete[] a;
a = NULL;
}
}
+18
View File
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
int main()
{
int n;
double d;
int *ptr;
ptr = &n;
cout << "ptr = " << ptr << endl;
*ptr = 0;
cout << "*ptr = " << *ptr << endl;
*ptr = 1000;
cout << "*ptr = " << *ptr << endl;
// ptr = &d;
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;
int main()
{
cout << "sizeof(char) = " << sizeof(char) << endl;
cout << "sizeof(char*) = " << sizeof(char*) << endl;
cout << "sizeof(unsigned char) = " << sizeof(unsigned char) << endl;
cout << "sizeof(unsigned char*) = " << sizeof(unsigned char*) << endl;
cout << "sizeof(signed char) = " << sizeof(signed char) << endl;
cout << "sizeof(signed char*) = " << sizeof(signed char*) << endl;
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(int*) = " << sizeof(int*) << endl;
cout << "sizeof(unsigned int) = " << sizeof(unsigned int) << endl;
cout << "sizeof(unsigned int*) = " << sizeof(unsigned int*) << endl;
cout << "sizeof(signed int) = " << sizeof(signed int) << endl;
cout << "sizeof(signed int*) = " << sizeof(signed int*) << endl;
cout << "sizeof(short int) = " << sizeof(short int) << endl;
cout << "sizeof(short int*) = " << sizeof(short int*) << endl;
cout << "sizeof(unsigned short int) = " << sizeof(unsigned short int) << endl;
cout << "sizeof(unsigned short int*) = " << sizeof(unsigned short int*) << endl;
cout << "sizeof(signed short int) = " << sizeof(signed short int) << endl;
cout << "sizeof(signed short int*) = " << sizeof(signed short int*) << endl;
cout << "sizeof(long int) = " << sizeof(long int) << endl;
cout << "sizeof(long int*) = " << sizeof(long int*) << endl;
cout << "sizeof(unsigned long int) = " << sizeof(unsigned long int) << endl;
cout << "sizeof(unsigned long int*) = " << sizeof(unsigned long int*) << endl;
cout << "sizeof(signed long int) = " << sizeof(signed long int) << endl;
cout << "sizeof(signed long int*) = " << sizeof(signed long int*) << endl;
cout << "sizeof(float) = " << sizeof(float) << endl;
cout << "sizeof(float*) = " << sizeof(float*) << endl;
cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "sizeof(double*) = " << sizeof(double*) << endl;
cout << "sizeof(long double) = " << sizeof(long double) << endl;
cout << "sizeof(long double*) = " << sizeof(long double*) << endl;
cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << endl;
cout << "sizeof(wchar_t*) = " << sizeof(wchar_t*) << endl;
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int n = 100;
int *ptr1, *ptr2, *ptr3;
ptr1 = &n;
ptr2 = &n;
ptr3 = ptr1;
printf("%d\n", n);
printf("%d\n", *ptr1);
printf("%d\n", *ptr2);
printf("%d\n", *ptr3);
printf("%p\n", &n);
printf("%p\n", &ptr1);
printf("%p\n", &ptr2);
printf("%p\n", &ptr3);
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {10, 20, 30, 40, 50};
int *ptr;
//ptr = arr;
//ptr = &arr[0];
ptr = &arr[2];
for (int i = 0; i < 3; i++)
{
cout << ptr[i] << endl;
}
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int *ptr;
ptr = 0;
printf("%p\n", ptr);
printf("%d\n", ptr);
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
#include <iostream>
#include <cmath>
#define N 4
using namespace std;
int main()
{
double vector_1[N], vector_2[N];
double length_1 = 0, length_2 = 0, scalar_product = 0, *ptr1 = vector_1, *ptr2 = vector_2;
int i;
cout << "Khong gian vector " << N << " chieu." << endl;
cout << "Nhap toa do cua vector thu nhat: ";
for (i = 0; i < N; i++)
{
cin >> ptr1[i];
}
cout << "Nhap toa do cua vector thu hai: ";
for (i = 0; i < N; i++)
{
cin >> ptr2[i];
}
for (i = 0; i < N; i++)
{
length_1 += ptr1[i]*ptr1[i];
length_2 += ptr2[i]*ptr2[i];
scalar_product += ptr1[i]*ptr2[i];
}
length_1 = sqrt(length_1);
length_2 = sqrt(length_2);
cout << "Do dai vector thu nhat: " << length_1 << endl;
cout << "Do dai vector thu hai: " << length_2 << endl;
cout << "Tich vo huong cua hai vector: " << scalar_product << endl;
return 0;
}
BIN
View File
Binary file not shown.
+59
View File
@@ -0,0 +1,59 @@
#include <iostream>
using namespace std;
//void thay_doi1(int& x); //Cau a
//void thay_doi1(int x); //Cau b
//void thay_doi2(int* x); //Cau a
void thay_doi(int& x); //Cau d
void thay_doi(int* x); //Cau d
void thay_doi(int x); //Cau e
int main()
{
int a = 9;
//int* a = new int(9); //Cau c
cout << "before:" << "a =" << a << endl;
//cout << "before:" << "a =" << *a << endl; //Cau c
//thay_doi1(a);
thay_doi(a);
//thay_doi1(*a); //Cau c
//thay_doi2(&a);
thay_doi(&a);
//thay_doi2(a); //Cau c
cout << "after:" << "a =" << a << endl;
//cout << "after:" << "a =" << *a << endl; //Cau c
//delete a; //Cau c
return 0;
}
//void thay_doi1(int& x){ //Cau a
void thay_doi(int& x){ //Cau d
//void thay_doi1(int x){ //Cau b
x = 100;
}
//void thay_doi2(int* x){
void thay_doi(int* x){ //Cau d
*x = 100;
}
void thay_doi(int x){ //Cau e
x = 100; //Cau e
} //Cau e
/*
Câu a: thêm 2 dòng khai báo
void thay_doi1(int x);
void thay_doi2(int* x);
trước hàm main().
Câu b: kết quả biến a không thay đi do hàm không lưu lại giá trị sau khi chạy.
Câu c: thay đi lời gọi hàm lại thành thay_doi1(*a); thay_doi2(a);
thay đi trong lệnh xuất hàm từ a thành *a
câu d: không lỗi
câu e: lỗi
*/
+11
View File
@@ -0,0 +1,11 @@
#include <iostream>
#include <math.h>
using namespace std;
void num_to_text(int num);
void tens_to_text(int tens);
void power_to_text(int power);
int get_num_digits(int n);
void translateHundred(int hundred_chunk);
void translateThousand(int thousand_chunk);
+130
View File
@@ -0,0 +1,130 @@
#include "Bai_2.h"
int main()
{
int n;
cin >> n;
if (n == 0)
{
cout << "zero";
}
else if (n < 0)
{
cout << "negative ";
n *= -1;
}
int num_digits = get_num_digits(n);
int chunk_count = (num_digits%3 == 0) ? (num_digits/3) : (num_digits/3) + 1;
int t;
while (chunk_count > 0)
{
t = pow(10, (chunk_count - 1)*3);
translateThousand(n/t);
if (chunk_count > 1)
{
cout << " ";
}
power_to_text(chunk_count - 1);
n = n%t;
chunk_count--;
}
return 0;
}
void num_to_text(int num)
{
char* arr_num[19] =
{
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"night",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
if (num > 0 && num < 20)
{
cout << arr_num[num - 1];
}
}
void tens_to_text(int tens)
{
char* arr_tens[8] =
{
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
if (tens > 1 && tens < 10)
{
cout << arr_tens[tens - 2];
}
}
void power_to_text(int power)
{
char* arr_power[3] =
{
"thousand ",
"million ",
"billion "
};
if (power > 0 && power < 3)
{
cout << arr_power[power - 1];
}
}
int get_num_digits(int n)
{
int num_digits = 0;
while (n != 0)
{
n /= 10;
num_digits++;
}
return num_digits;
}
void translateThousand(int thousand_chunk)
{
if (thousand_chunk >= 100)
{
num_to_text(thousand_chunk/100);
cout << " hundred ";
}
translateHundred(thousand_chunk%100);
}
void translateHundred(int hundred_chunk)
{
if (hundred_chunk >= 20)
{
tens_to_text(hundred_chunk/10);
cout << " ";
num_to_text(hundred_chunk%10);
}
else
{
num_to_text(hundred_chunk);
}
}
+26
View File
@@ -0,0 +1,26 @@
//Bai_3.h
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include <io.h>
using namespace std;
typedef struct
{
wchar_t chat;
char* so;
} Bai;
wchar_t bo_chat[4] = {L'', L'', L'', L''};
char* bo_so[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int so_lan_tron = 100;
Bai bai[52], bai_tam[52];
void khoi_dong();
void hien_thi();
void tron_bai();
+81
View File
@@ -0,0 +1,81 @@
//main.cpp
#include "Bai_3.h"
int main()
{
_setmode(_fileno(stdout), 0x00020000);
khoi_dong();
wcout << "Trang thai khoi dong: " << endl;
hien_thi();
for (int i = 0; i < 100; i++)
{
wcout << "=";
}
wcout << endl;
tron_bai();
wcout << "Sau khi tron bai: " << endl;
hien_thi();
return 0;
}
void khoi_dong()
{
for (int i = 0; i < 52; i++)
{
bai[i].so = bo_so[i%13];
bai[i].chat = bo_chat[i/13];
}
}
void hien_thi()
{
for (int i = 0; i <52; i++)
{
wcout << bai[i].so << bai[i].chat << "\t";
if (i%13 == 12)
{
wcout << endl;
}
}
}
void tron_bai()
{
srand(time(NULL));
while(so_lan_tron--)
{
int random_1 = rand()*52/RAND_MAX;
int random_2 = random_1 + rand()*(52 - random_1)/RAND_MAX;
for (int i = 0; i < 52; i++)
{
bai_tam[i] = bai[i];
}
for (int i = 0; i < random_1; i++)
{
bai[i + random_2 - random_1] = bai_tam[i];
}
for (int i = 0; i < random_2 - random_1; i++)
{
bai[i] = bai_tam[i + random_1];
}
}
}
/*
void tron_bai()
{
srand(time(NULL));
while(so_lan_tron--)
{
int random_1 = rand()*52/RAND_MAX;
int random_2 = rand()*52/RAND_MAX;
Bai tam = bai[random_1];
bai[random_1] = bai[random_2];
bai[random_2] = tam;
}
}
*/
+8
View File
@@ -0,0 +1,8 @@
#include <iostream>
#include <math.h>
using namespace std;
bool IsPrime(int n);
bool IsAmstrong(int n);
bool IsPerfectNumber(int n);
+92
View File
@@ -0,0 +1,92 @@
#include "Bai_4.h"
int main()
{
int n1, n2, count_Prime = 0, count_Amstrong = 0, count_PerfectNumber = 0;
cout << "Nhap khoang gia tri [n1, n2]:" << endl;
cin >> n1 >> n2;
cout << "Cac so nguyen to trong doan [" << n1 << ", " << n2 << "]:" << endl;
for (int n = n1; n <= n2; n++)
{
if (IsPrime(n))
{
count_Prime++;
cout << n << "\t";
}
}
cout << endl << "Cac so Amstrong trong doan [" << n1 << ", " << n2 << "]:" << endl;
for (int n = n1; n <= n2; n++)
{
if (IsAmstrong(n))
{
count_Amstrong++;
cout << n << "\t";
}
}
cout << endl << "Cac so hoan hao trong doan [" << n1 << ", " << n2 << "]:" << endl;
for (int n = n1; n <= n2; n++)
{
if (IsPerfectNumber(n))
{
count_PerfectNumber++;
cout << n << "\t";
}
}
cout << endl << "Trong doan da cho co " << count_Prime << " so nguyen to, " << count_Amstrong << " so Amstrong, " << count_PerfectNumber << " so hoan hao." << endl;
return 0;
}
bool IsPrime(int n)
{
if (n == 0 || n == 1)
{
return 0;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n%i == 0)
{
return 0;
}
}
return 1;
}
bool IsAmstrong(int n)
{
int t = n, sum = 0;
while (t != 0)
{
int r = t%10;
sum += r*r*r;
t /= 10;
}
if (n == sum)
{
return 1;
}
else
{
return 0;
}
}
bool IsPerfectNumber(int n)
{
int sum = 0;
for (int i = 1; i < n; i++)
{
if (n%i == 0)
{
sum += i;
}
}
if (n == sum)
{
return 1;
}
else
{
return 0;
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef BAI_5_H_INCLUDED
#define BAI_5_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
long long tong_mang(int *a, int n);
long long tich_mang(int *a, int n);
void in_thuan(int *a, int n);
void in_nghich(int *a, int n);
int pt_max(int *a, int n);
string dec_2_bin(int n);
void hailstone(int n, int &h);
#endif // BAI_5_H_INCLUDED
+131
View File
@@ -0,0 +1,131 @@
#include "Bai_5.h"
int main()
{
int n, d, e, h = 0;
cout << "Nhap so phan tu cua mang: ";
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Tong cac phan tu trong mang: " << tong_mang(a, n) << endl;
cout << "Tich cac phan tu trong mang: " << tich_mang(a, n) << endl;
cout << "Mang khi in thuan:" << endl;
in_thuan(a, n);
cout << endl;
cout << "Mang khi in nghich:" << endl;
in_nghich(a, n);
cout << endl;
cout << "Phan tu lon nhat trong mang la: " << pt_max(a, n) << endl;
cout << "Nhap gia tri can chuyen sang ma nhi phan:" << endl;
cin >> d;
cout << "Gia tri cua " << d << " khi chuyen sang ma nhi phan la:" << endl;
cout << dec_2_bin(d) << endl;
cout << "Nhap gia tri can tim chuoi so hailstone:" << endl;
cin >> e;
cout << "Chuoi so hailstone sinh ra tu " << e << " la:" << endl;
hailstone(e, h);
cout << endl;
cout << "Do dai chuoi hailstone: " << h << endl;
return 0;
}
long long tong_mang(int *a, int n)
{
if (n > 0)
{
return a[n - 1] + tong_mang(a, n - 1);
}
else
{
return 0;
}
}
long long tich_mang(int *a, int n)
{
if (n > 0)
{
return a[n - 1]*tich_mang(a, n - 1);
}
else
{
return 1;
}
}
void in_thuan(int *a, int n)
{
if (n > 0)
{
cout << a[0] << "\t";
in_thuan(&a[1], n - 1);
}
}
void in_nghich(int *a, int n)
{
if (n > 0)
{
cout << a[n - 1] << "\t";
in_nghich(a, n - 1);
}
}
int pt_max(int *a, int n)
{
if (n <= 1)
{
return a[n - 1];
}
else
{
int t = pt_max(a, n - 1);
if (a[n - 1] < t)
{
return t;
}
else
{
return a[n - 1];
}
}
}
string dec_2_bin(int n)
{
if (n == 0)
{
return "";
}
else
{
if (n%2 == 1)
{
return dec_2_bin(n/2) + "1";
}
else
{
return dec_2_bin(n/2) + "0";
}
}
}
void hailstone(int n, int &h)
{
cout << n << "\t";
if (n != 1)
{
h++;
if (n%2 == 0)
{
hailstone(n/2, h);
}
else
{
hailstone(3*n + 1, h);
}
}
}
BIN
View File
Binary file not shown.
+53
View File
@@ -0,0 +1,53 @@
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
private:
//protected:
int m_value;
public:
/*
A(): m_value(123)
{
cout << "In A(), value = " << m_value << endl;
}
*/
A()
{
this->m_value = 123;
cout << "In A(), value = " << m_value << endl;
}
///*
A(const A& ob): m_value(ob.m_value)
{
cout << "In A(const A& ob), value = " << m_value << endl;
}
//*/
A(int value): m_value(value)
{
cout << "In A(int value), value = " << m_value << endl;
}
///*
~A()
{
cout << "In ~A(), value = " << m_value << endl;
}
//*/
};
int main()
{
A ob1;
A *ptr1 = new A(246);
{
A ob2(456);
A ob3(ob1);
}
delete ptr1;
A ob4(789);
A ob5 = 257;
return 0;
};
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;
class X
{
public:
int m_value;
};
class Y: public X
{
public:
int m_value;
};
class Z: public Y
{
public:
Z(int vx, int vy, int vz)
{
X::m_value = vx;
Y::m_value = vy;
Z::m_value = vz;
}
public:
int m_value;
};
int main()
{
Z obj(1, 2, 3);
cout << "obj = (" << obj.X::m_value << ", " << obj.Y::m_value << ", " << obj.Z::m_value << ")" << endl;
return 0;
};
+39
View File
@@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
class X
{
public:
void display()
{
cout << "class X" << endl;
}
};
class Y: public X
{
public:
void display()
{
cout << "class Y" << endl;
}
};
class Z: public Y
{
public:
void display()
{
cout << "class Z" << endl;
}
};
int main(){
Z obj;
obj.X::display();
obj.Y::display();
obj.Z::display();
obj.display();
return 0;
};
+62
View File
@@ -0,0 +1,62 @@
#include <iostream>
using namespace std;
class X{
public:
/*(1)*/virtual void display(){
cout << "Type of \"this\" object is: Class X" << endl;
}
};
class Y: public X{
public:
void display(){
cout << "Type of \"this\" object is: Class Y" << endl;
}
};
class Z: public Y{
public:
void display(){
cout << "Type of \"this\" object is: Class Z" << endl;
}
};
int main(){
cout << "Call method via NON-POINTER variable - CASE I:" << endl;
X x;
Y y;
Z z;
x.display();
y.display();
z.display();
/////////////////
cout << " Call method via NON-POINTER variable - CASE II:" << endl;
X x1, x2, x3;
Y y2;
Z z3;
x2 = y2; x3 = z3; //ATTENTION
x1.display();
x2.display();
x3.display();
/////////////////
cout << " Call method via POINTER variable - CASE I:" << endl;
X *px = new X();
Y *py = new Y();
Z *pz = new Z();
px->display();
py->display();
pz->display();
delete px;
delete py;
delete pz;
/////////////////
cout << " Call method via POINTER variable - CASE II:" << endl;
X *px1 = new X();
X *px2 = new Y(); //ATTENTION
X *px3 = new Z(); //ATTENTION
px1->display();
px2->display();
px3->display();
delete px1;
delete px2;
delete px3;
return 0;
};
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;
class X{
public:
/*(1)*/ void display(){
cout << "Type of \"this\" object is: Class X" << endl;
}
};
class Y: public X
{
public:
void display()
{
cout << "Type of \"this\" object is: Class Y" << endl;
}
};
class Z: public Y
{
protected:
void display()
{
cout << "Type of \"this\" object is: Class Z" << endl;
}
};
int main(){
X x;
Y y;
Z z;
x.display(); //OK
y.display(); //OK
z.display(); //Error during compilation
return 0;
};
+95
View File
@@ -0,0 +1,95 @@
#include <iostream>
using namespace std;
class Rectangle
{
protected:
int length;
int width;
public:
Rectangle()
{
this->length = 20;
this->width = 10;
};
Rectangle (int length, int width)
{
this->length = length;
this->width = width;
};
void display()
{
cout << "Chieu dai: " << this->length << ". Chieu rong: " << this->width << endl;
};
void getInfo()
{
cout << "Nhap chieu dai va chieu rong:";
cin >> this->length >> this->width;
};
void setLength(int length)
{
this->length = length;
};
void setWidth(int width)
{
this->width = width;
};
int getLength()
{
return this->length;
};
int getWidth()
{
return this->width;
};
int area()
{
return this->length*this->width;
};
~Rectangle()
{
};
};
class Box: public Rectangle
{
protected:
int depth;
public:
Box()
{
this->length = 20;
this->width = 10;
this->depth = 5;
}
Box (int length, int width, int depth)
{
this->length = length;
this->width = width;
this->depth = depth;
}
void display()
{
cout << "Chieu dai: " << this->length << ". Chieu rong: " << this->width << ". Chieu sau: " << this->depth << endl;
};
void getInfo()
{
cout << "Nhap chieu dai, chieu rong va chieu sau:";
cin >> this->length >> this->width >> this->depth;
};
int area()
{
return this->length*this->width*this->depth;
};
~Box()
{
}
};
int main()
{
return 0;
}
+82
View File
@@ -0,0 +1,82 @@
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
class Date
{
private:
int mday;
int mon;
int year;
public:
Date()
{
time_t now = time(0);
tm *lct = localtime(&now);
this->mday = lct->tm_mday;
this->mon = 1 + lct->tm_mon;
this->year = 1900 + lct->tm_year;
}
Date(int mday, int mon, int year)
{
this->mday = mday;
this->mon = mon;
this->year = year;
}
friend ostream &operator<< (ostream &output, const Date &D)
{
output << setw(2) << setfill('0') << D.mday << "/" << setw(2) << setfill('0') << D.mon << "/" << setw(4) << D.year;
return output;
}
friend istream &operator>> (istream &input, Date &D)
{
input >> D.mday;
input.ignore(1);
input >> D.mon;
input.ignore(1);
input >> D.year;
return input;
}
bool operator< (Date D)
{
if (this->year < D.year)
{
return 1;
}
else if (this->year == D.year && this->mon < D.mon)
{
return 1;
}
else if (this->year == D.year && this->mon == D.mon && this->mday < D.mday)
{
return 1;
}
return 0;
}
string toString()
{
stringstream ss;
string s;
if (this->mday < 10)
{
ss << "0";
}
ss << this->mday << "/";
if (this->mon < 10)
{
ss << "0";
}
ss << this->mon << "/" << year;
ss >> s;
return s;
}
};
#endif // DATE_H_INCLUDED
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
#include <iomanip>
#include "Date.h"
using namespace std;
int main()
{
Date d1, d2;
cout << "Enter date:";
cin >> d1;
cout << "Enter date:";
cin >> d2;
cout << "Date 1:" << d1 << endl;
cout << "Date 2:" << d2 << endl;
cout << (d1 < d2? "d1 < d2" : "d1 >= d2") << endl;
Date d3(25,2,2017);
cout << d3.toString() << endl;
return 0;
}
BIN
View File
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;
class M
{
int n;
double **m;
M(int n)
{
this->n = n;
double **m = new double *[this->n];
for (int i = 0; i < this->n; i++)
{
m[i] = new double [3];
}
}
};
int main()
{
cout << "Hello world!" << endl;
return 0;
}
+76
View File
@@ -0,0 +1,76 @@
#include <iostream>
using namespace std;
class matrix{
public :
double** ptr;
int size;
matrix()
{
this->size = 0;
this->ptr = NULL;
}
matrix(int size)
{
this -> size = size;
ptr = new double*[3];
for (int i = 0; i < 3; i++)
{
ptr[i] = new double[size];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < size; j++)
{
ptr[i][j] = i + j;
}
}
}
void print()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < this->size; j++)
{
cout << this->ptr[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
};
matrix transform(matrix Min)
{
double x = 0, y = 0, z = 0;
matrix Mout(Min.size);
for (int j = 0; j < Min.size; j++)
{
x += Min.ptr[0][j];
y += Min.ptr[1][j];
z += Min.ptr[2][j];
}
x = x/Min.size;
y = y/Min.size;
z = z/Min.size;
for (int j = 0; j < Min.size; j++)
{
Mout.ptr[0][j] = Min.ptr[0][j] - x;
Mout.ptr[1][j] = Min.ptr[1][j] - y;
Mout.ptr[2][j] = Min.ptr[2][j] - z;
}
return Mout;
}
int main()
{
int N;
cout << "ENTER N : ";
cin >> N;
matrix x(N);
matrix y;
y = transform(x);
x.print();
y.print();
return 0;
}
+98
View File
@@ -0,0 +1,98 @@
#include <iostream>
#include <iomanip>
using namespace std;
#define MAX_SIZE 10
class matrix {
public:
double** ptr;
int size;
matrix()
{
this->size = 0;
this->ptr = NULL;
}
matrix(int size)
{
this->size = size;
ptr = new double*[3];
for (int i = 0; i < 3; i++)
{
ptr[i] = new double[MAX_SIZE];
}
}
friend ostream &operator<< (ostream &output, const matrix &m)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < m.size; j++)
{
output << setw(5) << m.ptr[i][j];
}
output << endl;
}
return output;
}
void del()
{
for (int i = 0; i < 3; i++)
{
if (ptr[i] != NULL)
{
delete[] ptr[i];
ptr[i] = NULL;
}
}
if (ptr != NULL)
{
delete[] ptr;
ptr = NULL;
}
}
~matrix(){}
};
matrix transform(const matrix& Min)
{
double x = 0, y = 0, z = 0;
static matrix Mout(Min.size);
for (int j = 0; j < Min.size; j++)
{
x += Min.ptr[0][j];
y += Min.ptr[1][j];
z += Min.ptr[2][j];
}
x = x / Min.size;
y = y / Min.size;
z = z / Min.size;
for (int j = 0; j < Min.size; j++)
{
Mout.ptr[0][j] = Min.ptr[0][j] - x;
Mout.ptr[1][j] = Min.ptr[1][j] - y;
Mout.ptr[2][j] = Min.ptr[2][j] - z;
}
return Mout;
}
int main()
{
int N;
cout << "ENTER N : ";
cin >> N;
matrix x(N);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < N; j++)
{
cout << "a[" << i << "][" << j << "] : ";
cin >> x.ptr[i][j];
}
}
matrix y;
y = transform(x);
cout << x << endl;
cout << y << endl;
x.del();
y.del();
return 0;
}
+1
View File
@@ -1,2 +1,3 @@
# KTLT
Ky thuat lap trinh HK172
Code không hoàn hảo, chỉ mang tính chất tham khảo...