SV: Nguyễn Hoài Danh
LAB 5
MSSV: 1610391
Bài 1:
#include<iostream>
#include<string>
using namespace std;
#define NUM 100
int main()
{
typedef struct
{
string name;
float weight;
int nam_sx;
int gia_tien;
} mayin;
typedef struct
{
string name;
float diem;
int hoc_ki;
int so_hs;
} monhoc;
typedef struct
{
string name;
string nam_thanh_lap;
string thanh_pho;
int so_thanh_vien;
} doanhnghiep;
typedef struct
{
string name;
string color;
string gioi_tinh;
float can_nang;
} conchim;
mayin arr1[NUM];
monhoc arr2[NUM];
doanhnghiep arr3[NUM];
conchim arr4[NUM];
arr1[0]
arr2[0]
arr3[0]
arr4[0]
=
=
=
=
{
{
{
{
"LG",15.5,1990,100000 };
"toan",5.5,1,100 };
"fpt","1990","hcm",50 };
"sao","xanh","male",1 };
cout <<"May in: "<< arr1[0].name<
arr1[0].nam_sx<
cout << "Mon hoc: " << arr2[0].name << endl << arr2[0].diem <<
endl << arr2[0].hoc_ki << endl << arr2[0].so_hs<< endl;
cout << "doanh nghiep: " << arr3[0].name << endl << arr3[0].nam_thanh_lap
<< endl << arr3[0].so_thanh_vien << endl << arr3[0].thanh_pho << endl;
cout << "con chim: " << arr4[0].name << endl << arr4[0].color << endl
<< arr4[0].can_nang << endl << arr4[0].gioi_tinh << endl;
}
system("pause");
Bài 2:
#include<iostream>
#include<string>
#include<math.h>
#include<sstream>
using namespace std;
#define NUM 50
int main()
{
typedef struct
{
double a;
double b;
} vecto;
vecto vt[NUM];
int N;
int dema = 0, demb = 0;
double sum_binh_phuong_a =0, sum_binh_phuong_b = 0;
double da, db, dca, dcb, tich_vh = 0, goc;
string str;
getline(cin, str);
getline(cin, str);
getline(cin, str);
getline(cin, str);
for (int j = 0; j < str.length(); j++)
{
if (str[j] == ' ')
dema++;
}
stringstream str1(str);
int i = 0;
for (i = 0; i <= dema; i++)
{
str1 >> vt[i].a;
cout << vt[i].a << " ";
}
cout << endl;
getline(cin, str);
for (int j = 0; j < str.length(); j++)
{
if (str[j] == ' ')
demb++;
}
stringstream str2(str);
int k = 0;
for (k = 0; k <= demb; k++)
{
str2 >> vt[k].b;
cout << vt[k].b << " ";
}
if (dema != demb)
{
}
else
{
cout << "error , khong cung so chieu";
N = dema + 1;
if (N > 50)
{
cout << "N>50";
}
else
{
N = dema + 1;
for (int z = 0; z < N; z++)
{
sum_binh_phuong_a += pow(vt[z].a, 2);
sum_binh_phuong_b += pow(vt[z].b, 2);
tich_vh += vt[z].a*vt[z].b;
}
da = sqrt(sum_binh_phuong_a);
db = sqrt(sum_binh_phuong_b);
if (N == 3)
{
cout << "tich co huong: " << vt[1].a*vt[2].b - vt[2].a*vt[1].b
<< "i + " <<
vt[0].a*vt[2].b - vt[2].a*vt[0].b << " j + " <<
vt[0].a*vt[1].b - vt[1].a*vt[0].b << " k" << endl;
}
goc = acos(tich_vh / (da*db));
dca = tich_vh / db;
dcb = tich_vh / da;
cout << "Chieu dai vt a: " << da << endl;
cout << "Chieu dai vt b: " << db<< endl;
cout<<"Tich vo huong : "<
cout << "goc giua 2vt : " << goc << endl;
cout << "hinh chieu a tren b : " << dca<< endl;
cout << "hinh chieu b trena : " << dcb << endl;
}
}
}
system("pause");
Bài 3:
#include<iostream>
#include<math.h>
using namespace std;
#define MAX_SIZE 100
int main()
{
int i, dem = 0;
double sum = 0, sd, tb;
double arr[MAX_SIZE];
cout<<”Nhap chuoi:”<
for (i = 0; i < MAX_SIZE; i++)
{
cin >> arr[i];
if (arr[i] < 0)break;
dem++;
sum = sum + arr[i];
}
tb = sum / dem;
}
sd = 0;
for (i = 0; i < dem; i++)
{
sd = sd + pow(arr[i] - tb, 2);
}
sd = sqrt(sd / (dem - 1));
cout << "Trung Binh:" << tb << endl << "Do lech chuan" << sd << endl;
system("pause");
Bài 4:
#include<iostream>
#include<math.h>
#include <cstdlib>
#include <ctime>
using namespace std;
double d(double x1, double y1, double x2, double y2);
#define MAX_VERT 10
int main()
{
int N; double chuvi = 0;
typedef struct
{
double x;
double y;
} Vertice;
Vertice v[MAX_VERT];
cout << "Nhap so dinh: ";
cin >> N;
while (N <= 2 || N > MAX_VERT)
{
cout << "Nhap lai ";
cin >> N;
}
srand(time(NULL));
for (int i = 0; i < N; i++)
{
v[i].x = rand() % 201 - 100;
v[i].y = rand() % 201 - 100;
}
cout << "X: ";
for (int i = 0; i < N; i++)
{
cout<
}
cout << endl;
cout << "Y: ";
for (int i = 0; i < N; i++)
{
cout << v[i].y << " ";
}
cout << endl;
chuvi = d(v[0].x, v[0].y, v[N-1].x, v[N-1].y);
for (int i = 0; i < N-1; i++)
{
chuvi += d(v[i].x, v[i].y, v[i + 1].x, v[i + 1].y);
}
cout << "chu vi la"<
system("pause");
}
double d(double x1, double y1, double x2, double y2)
{
double d;
d= (sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
return d;
}
Bài 5:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
#define NUM 100
int main()
{
int dem = 0, i, j, M, N;
string str;
int A[NUM][NUM];
getline(cin, str);
dem = str.length();
stringstream str1(str);
for (i = 0; i < dem; i++)
{
str1 >> A[0][i];
}
M = dem; //M la so ki tu moi dong
N = 1; // N la so dong
while (!cin.eof())
{
getline(cin, str);
dem = str.length();
if (dem != M)
{
cout << "error";
break;
}
stringstream str1(str);
for (i = 0; i < dem; i++)
{
str1 >> A[N][i];
}
N++;
}
M = (M + 1) / 2; //M la so cot
cout << "M = " << M << endl << "N = " << N << endl;
cout << "Ma tran X: " << endl;
for (i = 0; i < N; i++)
cout << A[i][0] << " ";
cout << endl;
cout << "Ma tran W: " << endl;
for (i = 0; i < N; i++)
{
for (j = 1; j < M; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
cout << "Ket qua" << endl;
}
int X[NUM], W[NUM][NUM];
for (i = 0; i
{
//cat tach mang ra thanh 2 mang X va W
X[i] = A[i][0];
cout << X[i] << " ";
}
cout << endl;
for (i = 0; i < N; i++)
{
for (j = 0; j < M - 1; j++)
{
W[i][j] = A[i][j+1];
cout << W[i][j] << " ";
}
cout << endl;
}
for (i = 0; i < N; i++)
{
int s = 0;
for (j = 0; j < M-1; j++)
{
s += X[j] * W[i][j];
}
cout << s << " ";
}
system("pause");
Bài 6:
#include<iostream>
#include<math.h>
using namespace std;
#define MAX_SIZE 100
int main()
{
char chuoi[MAX_SIZE];
int i, dem = 0;
cout << "Nhap chuoi" << endl;
cin.getline(chuoi, MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
{
if (chuoi[i] != NULL)
{
dem++;
}
else break;
}
cout << "Do dai chuoi la" << dem << endl;
system("pause");
}
Bài7:
#include<iostream>
#include<string>
using namespace std;
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int main()
{
int length;
string chuoi;
cout << "Nhap chuoi" << endl;
getline(cin, chuoi);
length = chuoi.length();
while (chuoi[length - 1] == ' ')
{
chuoi.pop_back();
length--;
}
while (chuoi[0] == ' ')
{
chuoi.erase(0, 1);
length--;
}
for (int i = 0; i < length; i++)
{
if ((chuoi[i] == chuoi[i + 1]) && (chuoi[i] == ' '))
{
chuoi.erase(i, 1);
length--; i--;
}
}
cout << chuoi;
system("pause");
}
Bài 8:
#include<iostream>
#include<string>
using namespace std;
void dao(string a);
int main()
{
int length;
string chuoi;
cout << "Nhap chuoi" << endl;
getline(cin, chuoi);
int pos = chuoi.find(' ');
length = chuoi.length();
while (pos != -1)
{
dao(chuoi.substr(0, pos));
cout << " ";
length = length - (pos + 1);
chuoi = chuoi.substr(pos + 1, length);
pos = chuoi.find(' ');
}
dao(chuoi);
cout << endl;
system("pause");
}
void dao(string a)
{
string tam;
int do_dai = a.length();
for (int i = 0; i < do_dai / 2; i++)
{
tam[0] = a[i];
a[i] = a[do_dai - i - 1];
a[do_dai - i - 1] = tam[0];
}
}
cout << a;