Skip to content

Commit

Permalink
Add source code of Lesson 8C
Browse files Browse the repository at this point in the history
  • Loading branch information
lamhoangtung committed Jul 28, 2018
1 parent 1114ca3 commit 144adef
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Bai thuc hanh so 8C/FILE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9
7 2 -3 5 0 1 3 8 4
2 changes: 2 additions & 0 deletions Bai thuc hanh so 8C/SORTED_FILE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9
-3 0 1 2 3 4 5 7 8
58 changes: 58 additions & 0 deletions Bai thuc hanh so 8C/bai1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <math.h>

using namespace std;

void inputFloat(float &x, char c){
cout << "Nhap so thuc " << c << ": ";
cin >> x;
}

void inputInteger(int &x, char c){
cout << "Nhap so nguyen " << c << ": ";
cin >> x;
}

float area(float x, float y){
return x*y;
}

int sum(int n){
int ans=0;
for (int i=0;i<n;i+=2){
if (i%3==0) ans+=i;
}
return ans;
}

float f1(float x,int n){
float ans=2016*pow(x,n),temp=1;
for (int i=1;i<=n;i++){
temp*=x/3;
ans+=temp;
}
return ans;
}

float f2(float x, int n){
float ans;
if (n>10) ans=2016*x;
else {
ans=exp(x);
for (int i=1;i<=n;i++) ans+=i;
}
return ans;
}

int main(){
float x,y;
inputFloat(x,'x');
inputFloat(y,'y');
int n;
inputInteger(n,'n');
cout << "Dien tich cua hinh chu nhat co canh la " << x << " va " << y << " la " << area(x,y) << "\n";
cout << "Tong cac so chan va chia het cho 3 trong doan tu 1 den " << n << " la " << sum(n) << "\n";
cout << "F1(" << x << "," << n << ") = " << f1(x,n) << "\n";
cout << "F2(" << x << "," << n << ") = " << f2(x,n) << "\n";
return 0;
}
49 changes: 49 additions & 0 deletions Bai thuc hanh so 8C/bai2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>

using namespace std;

int main(){
int n;
int a[1000];
cout << "Nhap so phan tu cua day: ";
cin >> n;
for (int i=0;i<n;i++){
cout << "Nhap phan thu thu " << i+1 << ": ";
cin >> a[i];
}

for (int j=n-1;j>=1;j--){
for (int i=0;i<=j-1;i++){
if (a[i]>a[i+1]){
int tg=a[i];
a[i]=a[i+1];
a[i+1]=tg;
}
}
}
cout << "Mang sau khi da sap xep tang dan la: \n";
for (int i=0;i<n;i++){
cout << a[i] << " ";
}
cout << "\n";

int count=0;
for (int i=0;i<n;i++){
if (a[i]>=10 && a[i]<=20){
count++;
}
}
cout << "Co " << count << " phan tu trong mang thuoc [10, 20].\n";

bool found=false;
for (int i=0;i<n-2;i++){
if (a[i]%2==0 && a[i]+2==a[i+1] && a[i+1]+2==a[i+2]){
found=true;
break;
}
}
if (found==true) cout << "Mang co ton tai ba so chan lien tiep.\n";
else cout << "Mang khong ton tai ba so chan lien tiep.\n";

return 0;
}
81 changes: 81 additions & 0 deletions Bai thuc hanh so 8C/bai3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>

using namespace std;

int main(){
int n,m;
cout << "Nhap so hang cua ma tran: ";
cin >> n;
cout << "Nhap so cot cua ma tran: ";
cin >> m;
int a[n][m];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cout << "Nhap phan tu [" << i+1 << "," << j+1 << "]: ";
cin >> a[i][j];
}
}
cout << "\nMa tran vua nhap la:\n";
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
cout << a[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
for (int i=0;i<n;i++){
int max=a[i][0];
for (int j=0;j<m;j++){
if (a[i][j]>max) max=a[i][j];
}
cout << "Gia tri max tren dong " << i+1 << " la " << max << "\n";
}

for (int j=0;j<m;j++){
int min=a[0][j];
for (int i=0;i<n;i++){
if (a[i][j]<min) min=a[i][j];
}
cout << "Gia tri min tren cot " << j+1 << " la " << min << "\n";
}
int b[m][n];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
b[i][j]=a[j][i];
a[j][i]=-a[j][i];
}
}

cout << "\nMa tran a sau khi doi dau la:\n";
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
cout << a[i][j] << " ";
}
cout << "\n";
}

cout << "\nMa tran chuyen vi b la:\n";
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
cout << b[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
for (int i=0;i<n;i++){
int count=0;
for (int j=0;j<m;j++){
if (a[i][j]>0) count++;
}
cout << "So phan tu duong tren dong " << i+1 << " cua a la " << count << "\n";
}
for (int i=0;i<m;i++){
int count=0;
for (int j=0;j<n;j++){
if (b[i][j]>0) count++;
}
cout << "So phan tu duong tren dong " << i+1 << " cua b la " << count << "\n";
}

return 0;
}
37 changes: 37 additions & 0 deletions Bai thuc hanh so 8C/bai4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string s;
cout << "Nhap xau ki tu: ";
getline(cin,s);
bool found=false;
for (int i=0;i<s.length()-2;i++){
if (s[i]=='B' && s[i+1]=='I' && s[i+2]=='S'){
found=true;
break;
}
}
if (found==true) cout << "Xau vua nhap co chua tu \"BIS\".\n";
else cout << "Xau vua nhap khong chua tu \"BIS\".\n";

cout << "Nhap vao mot ki tu: ";
char c;
cin >> c;
for (int i=0;i<s.length();i++){
if (s[i]==c) s.erase(i,1);
}
cout << "Xau ki tu sau khi xoa moi ki tu " << c << " co trong xau la: " << s << "\n";
cout << "Nhap vao mot ki tu: ";
cin >> c;

cout << "Nhap vao vi tri k: ";
int k;
cin >> k;
s.insert(k,1,c);
cout << "Xau ki tu sau khi chen ki tu " << c << " vao vi tri " << k << " la: " << s << "\n";

return 0;
}
34 changes: 34 additions & 0 deletions Bai thuc hanh so 8C/bai5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>

using namespace std;

int main(){
ifstream ifile("FILE.txt");
int n;
ifile >> n;
int a[n];
for (int i=0;i<n;i++){
ifile >> a[i];
}
ifile.close();
for (int j=n-1;j>=1;j--){
for (int i=0;i<=j-1;i++){
if (a[i]>a[i+1]){
int tg=a[i];
a[i]=a[i+1];
a[i+1]=tg;
}
}
}

ofstream ofile;
ofile.open("SORTED_FILE.txt");
ofile << n << endl;
for (int i=0;i<n;i++){
ofile << a[i] << " ";
}
ofile << endl;
ofile.close();
return 0;
}
55 changes: 55 additions & 0 deletions Bai thuc hanh so 8C/bai6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <iomanip>

using namespace std;

struct monhoc{
string ten;
int sotrinh;
float diem;
};

struct phieu{
string masinhvien;
string tensinhvien;
string lop;
int khoa;
monhoc mon[100];
};

int main(){
phieu phieubaodiem;
cout << "Nhap ma sinh vien: ";
getline(cin,phieubaodiem.masinhvien);
cout << "Nhap ten sinh vien: ";
getline(cin,phieubaodiem.tensinhvien);
cout << "Nhap lop: ";
getline(cin,phieubaodiem.lop);
cout << "Nhap khoa: ";
cin >> phieubaodiem.khoa;
float sum=0;
cout << "Nhap so luong mon hoc: ";
int n;
cin >> n;
for (int i=0;i<n;i++){
cout << "\nMon thu " << i+1 << ":\n";
cout << "\tTen mon hoc: ";
cin.ignore();
getline(cin,phieubaodiem.mon[i].ten);
cout << "\tSo trinh: ";
cin >> phieubaodiem.mon[i].sotrinh;
cout << "\tDiem: ";
cin >> phieubaodiem.mon[i].diem;
sum+=phieubaodiem.mon[i].diem;
}
cout << "\n\t\t\t\tPHIEU BAO DIEM\n";
cout << setw(20) << "Ma sinh vien: " << phieubaodiem.masinhvien << " " << setw(20) << "Ten sinh vien: " << phieubaodiem.tensinhvien << "\n";
cout << setw(20) << "Lop: " << phieubaodiem.lop << " " << setw(20) << "Khoa: " << phieubaodiem.khoa << "\n";
cout << setw(20) << "Bang diem:\n";
cout << setw(20) << "Ten mon" << setw(20) << "So trinh" << setw(20) << "Diem\n";
for (int i=0;i<n;i++){
cout << setw(20) << phieubaodiem.mon[i].ten << setw(20) << phieubaodiem.mon[i].sotrinh << setw(20) << phieubaodiem.mon[i].diem << "\n";
}
cout << "\n\tDiem trung binh: " << sum/n << "\n";
return 0;
}

0 comments on commit 144adef

Please sign in to comment.