Cộng đồng chia sẻ tri thức Doc24.vn

Bài Tập C++ Tuần 11

7b487333038878e73b6604849a4fe286
Gửi bởi: Đỗ Thị Kiều vào ngày 2020-12-08 02:55:13 || Kiểu file: DOCX Lượt xem: 104 | Lượt Download: 1 | File size: 3.087229 Mb

Nội dung tài liệu Xem trước tài liệu

Link tài liệu:
Tải xuống

Các tài liệu liên quan

Thông tin tài liệu




















TUẦN 9


Bài
1


#include
<iostream>


#include <string>


using namespace
std;


string s;


int Count = 0;


void NhapXau()


{


cout <<
"Cho biet xau ki tu: ";


getline(cin, s);
// ham nhap 1 xau ki tu vao bien s, ket thuc khi enter


cout <<
endl << "Xau da nhap: " << s;


}


void BoKiTuThua()


{


while (s[0] == '
') {


s.erase(0, 1);
// xoa di 1 ki tu trong o dau xau


if (s.empty())
break; // neu xau bi trong thi thoat khoi vong lap


}


if (!s.empty())
{ // neu xau chua bi rong


// bo qua cac
ki tu trong o cuoi xau


while
(s[s.size() - 1] == ' ') {


s.erase(s.size()
- 1, 1); // xoa 1 ki tu trong o cuoi xau


}


// chuan hoa
xau, bo cac ki tu trong canh nhau


int i = 1;


do {


if ((s[i] == '
') && (s[i-1] == ' ')) s.erase(i, 1);


else i++;


} while (i <
s.size() - 1);


// dem so tu
trong xau chuan hoa


i = 1;


while (i <
s.size() - 1) {


if ((s[i] == '
') && (s[i-1] != ' ')) Count++;


i++;


}


Count++; // can
tang them 1 tu, vi doan lap tren chi tim cac vi tri s[i] = ' ' va
s[i-1] != ' '


}


}


int main()


{


NhapXau();


BoKiTuThua();


cout <<
endl << "Xau sau chuan hoa: " << s;


cout <<
endl << "Do dai xau sau chuan hoa: " <<
s.size();


cout <<
endl << "So tu cua xau la: " << Count;


return 0;


}




Bài
2


#include
<iostream>


#include <string>


using namespace
std;


string s;


void NhapChuoi()


{


cout <<
"Nhap mot xau ki tu: ";


getline(cin, s);


cout <<
endl << "Xau da nhap la: " << s;


}


void
DemSoKiTuKhongLaChuCai()


{


int nCount = 0;


for (int i = 0;
i < s.size(); i++)


{


if ((s[i] >
'9') || (s[i] < '0'))


nCount++;


}


if (nCount >
0)


{


int i = 0;


do


{


if ((s[i] >
'9') || (s[i] < '0'))


{


s.erase(i,
1);


}


else i++;


} while (i <
s.size());



cout <<
endl << "Xau da cho KHONG bieu dien mot so nguyen duong.";


cout <<
endl << "Can xoa di " << nCount << "
ki tu de xau con lai bieu dien so nguyen duong.";


cout <<
endl << "Xau sau khi xoa cac ki tu la: " << s;


}


else {


cout <<
endl << "Xau da cho bieu dien mot so nguyen duong !";


}


}


int main()


{


NhapChuoi();


DemSoKiTuKhongLaChuCai();


return 0;


}




Bài
3


#include
<iostream>


#include <string>


using namespace
std;


string s1, s2;








void NhapXau()


{


cout <<
"Cho biet xau s1: ";


getline(cin,
s1);


cout <<
"Cho biet xau s2: ";


getline(cin,
s2);


}


int nCount = 0;


void KiemTra()


{








string sub_s1;


int i = 0;


while (i <
s1.size())


{


sub_s1 =
s1.substr(i, s2.size());


if
(s2.compare(sub_s1) == 0)


{


nCount++;


i +=
s2.size();


}


else i++;


}


}


int main()


{


NhapXau();


KiemTra();


cout <<
endl << "So lan xuat hien cua xau s2 trong xau s1 la: ";


cout <<
nCount;


return 0;


}




Bài
4


#include
<iostream>


#include
<fstream>


#include <string>


#include <vector>


#include <cctype>


#include
<conio.h>


#define MAX 100


using namespace
std;


ifstream finp;


ofstream fout;


vector<string>
sList;


vector<double>
mList;



void Mofile()


{


finp.open("C:/New
folder/danhsach.txt");


if (finp.fail())


{


cout <<
"Khong mo duoc file \"danhsach.txt\" !";


getch();


}


fout.open("C:/New
folder/danhsach_diem.txt");


if (fout.fail())


{


cout <<
"Khong mo duoc file \"danhsach_diem.txt\" !";


getch();



}


}


void ChuanHoa()


{


sList.clear();


string sName;


while
(getline(finp, sName))


{


// chuan hoa ho
ten sinh vien


if
(islower(sName[0])) sName[0] = toupper(sName[0]);


int i = 1;


while (i <
sName.size())


{


if
((sName[i-1] == ' ') && (sName[i] != ' '))


{


if
(islower(sName[i])) sName[i] = toupper(sName[i]);


}


i++;


}


sList.push_back(sName);


}


}


void NhapDiem()


{


cout <<
"Nhap diem Toan cho cac sinh vien: " << endl;


double pMath;


mList.clear();


for (int i = 0;
i < sList.size(); i++) {


cout <<
sList[i] << ": ";


cin >>
pMath;


mList.push_back(pMath);


}


fout <<
sList.size();


fout <<
endl;


int i = 0;


while (i <
sList.size()) {


fout <<
sList[i] << " " << mList[i] << endl;


i++;


}


}


int main()


{


Mofile();


ChuanHoa();


NhapDiem();


finp.close();


fout.close();


cout <<
endl << "Chuong trinh da ket thuc !";


cout <<
endl << "Hay mo file \"danhsach_diem.txt\" de
xem ket qua !";


cout <<
endl;


return 0;


}










Bài 5


#include
<iostream>


#include
<fstream>


#include <string>


#include
<conio.h>


#define MAX 100


using namespace
std;


ifstream finp;


ofstream fout;


int A[MAX] = {0};


int B[MAX] = {0};


int C[2 * MAX] =
{0};


int n, m;


void Mofile()


{


finp.open("C:/New
folder/input.dat.txt");


if (finp.fail())
{


cout <<
"Khong mo duoc file \"input.dat.txt\" !";


getch();


}


finp >> n
>> m;


for (int i = 0;
i < n; i++) {


finp >>
A[i];


}


for (int i = 0;
i < m; i++) {


finp >>
B[i];


}


}


void Sapxep()


{


for (int i = 0;
i < n - 1; i++)


for (int j =
i+1; j < n; j++) {


if (A[i] >
A[j]) {


A[i] = A[i] +
A[j];


A[j] = A[i] -
A[j];


A[i] = A[i] -
A[j];


}


}


// sap xep mang
B


for (int i = 0;
i < m - 1; i++)


for (int j =
i+1; j < m; j++) {


if (B[i] >
B[j]) {


B[i] = B[i] +
B[j];


B[j] = B[i] -
B[j];


B[i] = B[i] -
B[j];


}


}


}


void Ghepmang()


{


int i = 0, j =
0, k = 0;


while ((i <
n) && (j < m)) {


// neu A[i] <
B[j] thi lay phan tu A[i] vao C


if (A[i] <
B[j]) {


C[k] = A[i];


i++;


}


else {


C[k] = B[j];


j++;


}


k++;


}


if (i == n) {


while (j <
m) {


C[k] = B[j];


j++;


k++;


}


}


else {


while (i <
n) {


C[k] = A[i];


i++;


k++;


}


}


}


void Dongfile()


{


fout.open("C:/New
folder/output.dat.txt");


if (fout.fail())
{


cout <<
"Khong mo duoc file \"output.dat.txt\" !";


getch();


}


fout << n
+ m;


fout <<
endl;


for (int i = 0;
i < n + m; i++) fout << C[i] << " ";








finp.close();


fout.close();


cout <<
endl << "Hay mo file \"output.dat.txt\" de xem
ket qua !"<<endl;


}


int main()


{


Mofile();


Sapxep();


Ghepmang();


Dongfile();


return 0;


}








TUẦN
8


Bài
1


#include<iostream>


#include<cmath>


using namespace
std;


float diem[69];


void nhapDiem(int
&b)


{


for(int
i=0;i<=b-1;i++)


{


cout<<"
nhap diem cua sinh vien thu "<<i+1<<" : ";


cin>>
diem[i];


}


}


int main()


{


int SV;


float tam;


do


{


cout<<
"Nhap so sinh vien: " <<endl;


cin>>SV;


}


while(SV<20||SV>70);


nhapDiem(SV);


for(int
i=0;i<SV+1;i++)


{


for(int
j=i+1;j<SV+2;j++)


{


if(diem[i]<diem[j])


{


tam=diem[j];


diem[j]=diem[i];


diem[i]=tam;


}


}


}


for(int
i=0;i<=SV-1;i++)


{


cout<<diem[i]
<< " ";


}


int
tb=0,gioi=0,yeu=0;


for(int
i=0;i<=SV-1;i++)


{


if(diem[i]>=8.0)


{


gioi+=1;


}


if(diem[i]<5.0)


{


yeu+=1;


}


if(diem[i]>5.0&&diem[i]<8.0)


{


tb+=1;


}


}


cout<<
endl;


cout<< "
so hoc sinh gioi la: " << gioi<<endl;


cout<< "
so hoc sinh trung binh la: " << tb<<endl;


cout<< "
so hoc sinh yeu la: " << yeu;


return 0;


}




Bài
2


#include<iostream>


#include<cmath>


using namespace
std;


int TH(int j,int
i){


int
n,k,A=1,B=1,C=1;




for (int
n=1;n<=i;n++){


A=A*n;}


for(int
k=1;k<=j;k++){


B=B*k;}


for (int p=1;
p<=(i-j);p++){


C=C*p;


}


return
A/(B*C);








}








int
main(){


int
n;


cout
<<"nhap n: ";


cin >>
n;




for (int i =
0; i < n; i++) {


for (int j =
n; j > i; j--)


cout
<< " ";




for (int
j = 0; j <= i; j++) {


cout
<< TH(j,i) << " ";


}


cout <<
endl;


}


return 0;


}




Bài
3


#include<iostream>


#include<cmath>


#include
<cstdlib>


#include <ctime>


using namespace
std;


int main(){


srand(time(NULL));


int a[100][100];


for(int
i=0;i<100;i++){


for(int
j=0;j<100;j++){


int b= 0+
rand()%(255-0+1);


a[i][j]=b;


}


}


int
sang=0,toi=0,be_hon_70=0;


for(int
i=0;i<100;i++){


for(int
j=0;j<100;j++){


if(a[i][j]>sang){


sang=
a[i][j];


toi=sang;


}


if(a[i][j]<toi){


toi=a[i][j];


}


if(a[i][j]<70){


be_hon_70+=1;


}


}


}


cout <<
"diem sang nhat la : " << sang <<endl;


cout <<
"diem toi nhat la : " <<toi<<endl;


cout <<
"so diem be hon 70 la : " <<be_hon_70<<endl;


if
(be_hon_70>8000){


cout<<
"buc anh toi";


for(int
i=0;i<100;i++){


for (int
j=0;j<100;i++){


if
(a[i][j]<70){


a[i][j]+=5;


if(a[i][j]>255){


a[i][j]=255;


}


}


}


}


}


else{


cout<<
"buc anh sang" ;


}


return 0;


}




Bài
4


#include<iostream>


#include<cmath>


#include<vector>


using namespace
std;


int main(){


int n;


cout <<
"nhap n: " ;


cin>> n;


vector<int>A;


while(n>0){


int c=n%2;



A.push_back(c);


n=n/2;


}


while(A.size()>0){


cout <<
A.back();


A.pop_back();


}


return 0;


}




Bài
5


#include
<iostream>


#include
<cstdlib>


#include <ctime>


#include<vector>


using namespace
std;


int main(){


srand(time(NULL));


int n= 0+
rand()%(100-0+1);


int a,thu=1;


vector<int>A;


cout <<"
nhap so du doan: ";


cin>> a;


A.push_back(a);


while(a!=n){


if(a>n){


cout<<
"so ban nhap lon hon so cua toi. hay tiep tuc du doan" <<
endl;


}


if(a<n){


cout<<
"so ban nhap nho hon so cua toi. hay tiep tuc du doan" <<
endl;


}


cout <<
"nhap so du doan: ";


cin>>a;


int size=
A.size();


for(int
i=0;i<size;i++){


if(a==A[i]){


cout <<
"ban da nhap so nay roi, hay nhap so khac!!" << endl;


}


}


A.push_back(a);


thu+=1;


}


cout<<
"ban da du doan dung so " << n<< " . Ban
da thu " <<thu<< " lan. Chuc mung ban!!"
<<endl;


cout<<
"nhan E de thoat khoi tro choi ";


return 0;


}