Skip to content

Commit

Permalink
Add source code of Lesson 6
Browse files Browse the repository at this point in the history
  • Loading branch information
lamhoangtung committed Jul 22, 2018
1 parent 182e927 commit 4b9a8e9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Bai thuc hanh so 6/bai1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string s;
cout << "Nhap xau ki tu: ";
getline(cin,s);
int count=0;
for (int i=0;i<s.length();i++){
if (s[i]>='a' && s[i]<='z'){
count++;
}
}
cout << "Xau vua nhap co " << count << " chu cai thuong.\n";
for (int i=0;i<s.length();i++){
if (s[i]=='a'){
s.erase(i,1);
i--;
}
}
cout << "Xau sau khi xoa moi ki tu 'a' la: " << s << "\n";
return 0;
}
28 changes: 28 additions & 0 deletions Bai thuc hanh so 6/bai2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string s;
cout << "Nhap xau ki tu: ";
getline(cin,s);
bool ans=true;
int n=s.length(),words=1;
for (int i=0;i<n-1;i++){
if (s[i]==' ' && s[i+1]==' '){
ans=false;
break;
}
if (s[i]==' ' && s[i+1]!=' '){
words++;
if (words>10){
ans=false;
break;
}
}
}
if (ans==true) cout << "Xau tren hop le.\n";
else cout << "Xau tren khong hop le.\n";
return 0;
}
19 changes: 19 additions & 0 deletions Bai thuc hanh so 6/bai3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <string>

using namespace std;

int main(){
string s;
cout << "Nhap xau ki tu: ";
getline(cin,s);
char c;
cout << "Nhap ki tu c: ";
cin >> c;
int k;
cout << "Nhap so nguyen k: ";
cin >> k;
s.insert(k,1,c);
cout << "Xau sau khi chen them ki tu " << c << " vao vi tri " << k << " la: " << s << "\n";
return 0;
}
38 changes: 38 additions & 0 deletions Bai thuc hanh so 6/bai4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool arePair(char opening,char closing){
if (opening == '(' && closing == ')') return true;
else if (opening == '{' && closing == '}') return true;
else if (opening == '[' && closing == ']') return true;
return false;
}

bool balanced(string str){
stack <char> S;
for (int i=0;i<str.length();i++){
if(str[i] == '(' || str[i] == '{' || str[i] == '[')
S.push(str[i]);
else if(str[i] == ')' || str[i] == '}' || str[i] == ']'){
if (S.empty() || !arePair(S.top(),str[i]))
return false;
else
S.pop();
}
}
return S.empty() ? true:false;
}

int main(){
string s;
cout << "Nhap xau dau ngoac: ";
cin >> s;
if (balanced(s))
cout << "Hop le.\n";
else
cout << "Khong hop le.\n";
return 0;
}
30 changes: 30 additions & 0 deletions Bai thuc hanh so 6/bai5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){
int n;
cout << "Nhap so phan tu cua mang: ";
cin >> n;
int *a=new int[n];
for (int i=0;i<n;i++){
cout << "Nhap phan tu thu " << i+1 << " cua mang: ";
cin >> *(a+i);
}
int k=n;
for (int i=0;i<n;i++){
if (*(a+i)%2!=0){
k++;
a=(int*) realloc(a,k*sizeof(int));
*(a+k-1)=*(a+i);
}
}
n=k;
cout << "Mang sau khi sao chep cac phan tu le vao cuoi la:\n";
for (int i=0;i<n;i++){
cout << *(a+i) << " ";
}
cout << "\n";
return 0;
}
34 changes: 34 additions & 0 deletions Bai thuc hanh so 6/bai6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <stdlib.h>

using namespace std;

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

for (int i=0;i<n;i++){
if (*(a+i)%2==0){
n--;
for (int j=i;j<n;j++){
*(a+j)=*(a+j+1);
}
a = (int*) realloc(a,n*sizeof(int));
i--;
}
}

cout << "Mang sau khi xoa het cac phan tu chan la:\n";
for (int i=0;i<n;i++){
cout << *(a+i) << " ";
}
cout << "\n";
return 0;
}

0 comments on commit 4b9a8e9

Please sign in to comment.