Bài tập môn phương pháp lập trình - Trường công nghiệp Tp Hồ Chí Minh

Các bài tập môn phương pháp lập trình trong giáo trình của trường đại học công nghiệp 4_môn phương pháp lập trình include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a);cout<=0;i--) { if(a[i]==' ') break; k--; } for(int i=k;i

doc18 trang | Chia sẻ: lvcdongnoi | Lượt xem: 3140 | Lượt tải: 1download
Bạn đang xem nội dung tài liệu Bài tập môn phương pháp lập trình - Trường công nghiệp Tp Hồ Chí Minh, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
GIÁO VIÊN: MÔN:PHƯƠNG PHÁP LẬP TRÌNH. Họ Và Tên:ĐINH TRỌNG THOẠI MSSV :10299021 Lớp :CDTH12C ØØ Bài Tập Chương 7×× BAI 1: #include #include #include int main() { char a[50]; cout<<"nhap vao sau s1:= "; gets(a); cout<<a<<endl; int n,k; n=strlen(a); for(int i=0;i<n;i++) { k=a[i]; cout<<k<<endl; } cout<<a<<endl; } BAI 2: #include #include #include int main() { char a[50],k; cout<<"Nhap vao sau S:= "; gets(a); cout<<"\n===============================\n"; cout<<a<<endl; cout<<"\n===============================\n"; int n; n=strlen(a); for(int i=0;i<n/2;i++) {k=a[i];a[i]=a[n-1-i];a[n-1-i]=k;} cout<<a<<endl; cout<<"\n===============================\n"; } BAI 3: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a); cout<<"\n===============================\n\n"; cout<<a<<endl; cout<<"\n===============================\n\n"; int n,k,m=1; n=strlen(a); for(int i=0;i<n/2;i++) if(a[i]!=a[n-1-i]) m=0; if(m==1) cout<<"Chuoi 'S' la chuoi doi xung !!"; else if(m==0) cout<<"Chuoi 'S' khong fai la chuoi doi xung !!"; cout<<endl; cout<<"\n===============================\n"; } BAI 4: #include #include #include int main() { char a[50],k; cout<<"Nhap vao sau S:= "; gets(a); cout<<"\n===============================\n"; cout<<a<<endl; cout<<"\n===============================\n"; int n,s[30]; for(int i=65;i<91;i++) s[i]=0; n=strlen(a); for(int i=0;i<n;i++) for(int j=65;j<91;j++) if(int(a[i])==j||int (a[i])==j+32) s[j]++; cout<<endl; cout<<"So lan suat hien cua moi ky tu la: \n"; for(int i=65;i<91;i++) if(s[i]!=0) { k=i; cout<<(k)<<" : Suat Hien - "<<s[i]<<" - Lan"<<endl; } cout<<"\n===============================\n"; } BAI 5: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a);cout<<endl; cout<<a<<endl; cout<<"\n=======================\n"; int n,k=0,m=1; n=strlen(a); for(int i=0;i<n;i++) {cout<<a[i]; if(a[i]==' ') break; k++; } cout<<endl; for(int i=k+1;i<n;i++) cout<<a[i]; cout<<"\n-----------------------\n"; k=n; for(int i=n-1;i>=0;i--) { if(a[i]==' ') break; k--; } for(int i=k;i<n;i++) cout<<a[i];cout<<endl; for(int i=0;i<k;i++) cout<<a[i]; cout<<"\n========================"; cout<<endl; } BAI 6: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a); cout<<"\n===========================\n"; cout<<a<<endl; cout<<"===========================\n" ; int n,k=0,m=1; n=strlen(a); for(int i=0;i<n;i++) { cout<<a[i];if(a[i]==char(32)) cout<<endl; } cout<<"\n============================\n"; } BAI 7: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a); cout<<"\n=========================\n"; cout<<a; cout<<"\n=========================\n" ; int n,k,j=0,l; n=strlen(a); k=n; for(int i=n;i>=0;i--) { if(a[i]==' ') {for(int i=k+1;i<n;i++) cout<<a[i];cout<<" "; n=k; } k--; } for(int i=0;i<n;i++) cout<<a[i];cout<<endl; cout<<"========================="; cout<<endl; } BAI 8: #include #include char s[50] = ""; char *bachuso(unsigned int n) { char kyso[10][7]= {"", "Mot", "Hai", "Ba", "Bon","Nam","Sau","Bay", "Tam", "Chin"}; int donvi, chuc, tram; s[0] = 0; donvi = n % 10; n /= 10; chuc = n % 10; tram = n / 10; if (tram >0) { strcat(s, kyso[tram]); strcat(s, " Tram "); } if (chuc > 0) { if (chuc == 1) strcat(s, "Muoi "); else { strcat(s, kyso[chuc]); strcat(s, " Muoi "); } } if (donvi > 0) { if (chuc == 0 && tram != 0) strcat(s, "Le "); if (donvi == 1) strcat(s, "Mot"); else if (donvi == 5 && (chuc != 0 || tram != 0)) strcat(s, "Lam"); else if (donvi == 5 && (chuc = 0 || tram != 0)) strcat(s, "Nam"); else strcat(s, kyso[donvi]); } return s;} main() { unsigned long n; unsigned ngan, trieu, ty, donvi; char chuoi[200]; cout<<"- Nhap so tien cua ban : "; cin>> n; cout"; if (n == 0) cout<<"khong"; else { donvi = n % 1000; n /= 1000; ngan = n % 1000; n /= 1000; trieu = n % 1000; ty = n / 1000; chuoi[0] = 0; if (ty > 0) cout<<bachuso(ty) <<" Ty "; if (trieu > 0) cout<<bachuso(trieu) <<" Trieu "; if (ngan > 0) cout<<bachuso(ngan) <<" Ngan "; if (donvi > 0) cout<<bachuso(donvi) ; } cout<<"Dong\n"; cout<<"**********************************************\n"; } BAI 9: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a); cout<<"========================\n"; cout<<a; int n,k=0,m=1; n=strlen(a); cout<<"\n=======================\n"; for(int i=0;i<n;i++) { if(a[i]==' '&&a[i+1]==' ') { n--; for(int j=i;j<n;j++) a[j]=a[j+1]; i--; } } if(a[0]==' ') { n--; for(int i=0;i<n;i++) a[i]=a[i+1];} if(a[n-1]==' ') n--; for(int i=0;i<n;i++) cout<<a[i]; cout<<"\n========================\n"; k=n; for(int i=n-1;i>=0;i--) { if(a[i]==' ') break; k--; } for(int i=k;i<n;i++) cout<<a[i]; cout<<"\n========================\n"; } BAI 10: #include #include #include int main() { char a[50]; cout<<"Nhap vao sau S:= "; gets(a); cout<<"========================\n"; cout<<"Chuoi ban dau :\n"; cout<<a; int n,k=0,m=1; n=strlen(a); cout<<"\n========================\n"; cout<<"Chuoi sau khi cat bo cac khoang trang khong can thiet la:\n"; for(int i=0;i<n;i++) { if(a[i]==' '&&a[i+1]==' ') { n--; for(int j=i;j<n;j++) a[j]=a[j+1]; i--; } } if(a[0]==' ') { n--; for(int i=0;i<n;i++) a[i]=a[i+1];} if(a[n-1]==' ') n--; for(int i=0;i<n;i++) cout<<a[i]; cout<<"\n========================\n"; cout<<"Chuoi doi thanh chu hoa la:\n"; for(int i=0;i<n;i++) if(a[i]>='a'&&a[i]<='z') a[i]-=32; for(int i=0;i<n;i++) cout<<a[i]; cout<<"\n========================\n"; cout<<"Chuoi doi thanh chu thuong:\n"; for(int i=0;i<n;i++) if(a[i]>='A'&&a[i]<='Z') a[i]+=32; for(int i=0;i<n;i++) cout<<a[i]; cout<<"\n========================\n"; } BAI 11: #include #include #include int main() { char a[20][40]; int j,tt; int n,k=0,m=1; cout<<"Chon so luong nhap vao danh sach:= "; cin>>n; cout<<"\nBat dau nhap vao danh sach:"; n++; cout<<"\n=======================\n"; for(int i=0;i<n;i++) gets(a[i]); cout<<"=======================\n"; for(int q=1;q<n;q++) { cout<<"\nNguoi thu "<<q<<" la: "; tt=strlen(a[q]); cout<<"\n======================\n"; for(int i=0;i<tt;i++) { if(a[q][i]==' '&&a[q][i+1]==' ') { tt--; for(int j=i;j<tt;j++) a[q][j]=a[q][j+1]; i--; } } if(a[tt][0]==' ') { tt--; for(int i=0;i<tt;i++) a[q][i]=a[q][i+1];} if(a[q][tt-1]==' ') tt--; for(int i=0;i<tt;i++) cout<<a[q][i]; for(int i=0;i<tt;i++) if(a[q][i]==' ') a[q][i+1]-=32; a[q][0]-=32; cout<<endl; for(int i=0;i<tt;i++) cout<<a[q][i]; cout<<"\n=======================\n"; } } Bai 12: #include #include #include int main() { char a[20][40],s[20][7]; int j=0,tt; int n,k=0,m=1; cout<<"chon so luong nhap vao danh sach:= "; cin>>n; cout<<"bat dau nhap vao danh sach \n"; n++; cout<<"\n=======================\n"; for(int i=0;i<n;i++) gets(a[i]); cout<<"\n=======================\n"; for(int q=1;q<n;q++) { tt=strlen(a[q]); k=tt; for(int i=tt-1;i>=0;i--) { if(a[q][i]==' ') break; k--; } j=0; for(int i=k;i<tt;i++) { j++; cout<<a[q][i]; } cout<<"\n============================\n"; } } ØØ Bai Tap Chuong 8 ×× Bai 1: #include #include struct NhanVien{ long MaNV; char hoTen[50]; char diaChi[100]; int CBQL; } NV[100]; void Nhap(int i){ cout <<" Nhap Ma So Ma Nhan Vien Thu "<<i+1<<": "; cin >> NV[i].MaNV; cout <<" Nhap Ho Va Ten Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].hoTen,100); cin.getline(NV[i].hoTen,100); cout <<" Nhap Dia Chi Cua Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].diaChi,50); cout : "; cin >> NV[i].CBQL; cout <<"\n\n"; } void Xuat(int i){ if(i==0) cout <<" STT\t"<<"MSNV\t\t"<<"Ho Ten\t\t"<<"Dia Chi\t\t"<<"CBQL\n"; cout <<i+1<<"."<<"\t"<<NV[i].MaNV<<"\t\t"<<NV[i].hoTen<<"\t\t"; cout <<NV[i].diaChi<<"\t\t"<<NV[i].CBQL<<"\n"; } int main() { int n; cout<<"\n Nhap Vao So Luong NV := "; cin>>n; for(int i=0;i<n;i++) Nhap(i); cout<<"\n Xuat Ra Thong Tin Cua Cac Nhan Vien\n"; for(int i=0;i<n;i++) Xuat(i); cout<<"\n Ten Nhung Nhan Vien La Can Bo Quan Ly \n"; cout<<"Ho Va Ten:\n"; for(int i=0;i<n;i++) if(NV[i].CBQL==1) cout<<NV[i].hoTen<<"."<<endl; } Bai 2: #include #include struct NhanVien{ long MaNV; char hoTen[50]; char diaChi[100]; int CBQL; } NV[100]; void Nhap(int i){ cout <<" Nhap Ma So Ma Nhan Vien Thu "<<i+1<<": "; cin >> NV[i].MaNV; cout <<" Nhap Ho Va Ten Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].hoTen,100); cin.getline(NV[i].hoTen,100); cout <<" Nhap Dia Chi Cua Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].diaChi,50); cout : "; cin >> NV[i].CBQL; cout <<"\n\n"; } void Xuat(int i){ if(i==0) cout <<" STT\t"<<"MSNV\t\t"<<"Ho Ten\t\t"<<"Dia Chi\t\t"<<"CBQL\n"; cout <<i+1<<"."<<"\t"<<NV[i].MaNV<<"\t\t"<<NV[i].hoTen<<"\t\t"; cout <<NV[i].diaChi<<"\t\t"<<NV[i].CBQL<<"\n"; } int main() { int n; cout<<"\n Nhap Vao So Luong NV := "; cin>>n; for(int i=0;i<n;i++) Nhap(i); cout<<"\n Xuat Ra Thong Tin Cua Cac Nhan Vien\n"; for(int i=0;i<n;i++) Xuat(i); //xoa nham vien khong la can bo quan ly; int k=0; for(int i=0;i<n;i++) if(NV[i].CBQL!=1) { k++; for(int j=i;j<n-1;j++) NV[j]=NV[j+1]; } if(k<n) { cout<<"\n Danh Sach Nhan Vien La Can Bo Quan Ly \n"; for(int i=0;i<n-k;i++) Xuat(i);} else if(k==n) cout<<"Ko Co Nguoi Nao La Can Bo Quan Ly\n"; } Bai 3: #include #include struct NhanVien{ long MaNV; char hoTen[50]; char diaChi[100]; int CBQL; } NV[100]; void Nhap(int i){ cout <<" Nhap Ma So Ma Nhan Vien Thu "<<i+1<<": "; cin >> NV[i].MaNV; cout <<" Nhap Ho Va Ten Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].hoTen,100); cin.getline(NV[i].hoTen,100); cout <<" Nhap Dia Chi Cua Nhan Vien Thu "<<i+1<<": "; cin.getline(NV[i].diaChi,50); cout : "; cin >> NV[i].CBQL; cout <<"\n\n"; } void Xuat(int i){ if(i==0) cout <<" STT\t"<<"MSNV\t\t"<<"Ho Ten\t\t"<<"Dia Chi\t\t"<<"CBQL\n"; cout <<i+1<<"."<<"\t"<<NV[i].MaNV<<"\t\t"<<NV[i].hoTen<<"\t\t"; cout <<NV[i].diaChi<<"\t\t"; if(NV[i].CBQL==1) cout<<"LA CBQL\n"; else cout<<"Nhan Vien\n"; } int main() { int n; cout<<"\n Nhap Vao So Luong NV := "; cin>>n; for(int i=0;i<n;i++) Nhap(i); cout<<"\n Xuat Ra Thong Tin Cua Cac Nhan Vien\n"; for(int i=0;i<n;i++) Xuat(i); int k; cout<<"nhap vao vi tri: "; cin>>k; k--; for(int i=n-1;i>=k;i--) NV[i+1]=NV[i]; Nhap(k); n++; for(int i=0;i<n;i++) Xuat(i); }

Các file đính kèm theo tài liệu này:

  • docBài tập môn phương pháp lập trình_trường công nghiệp tphcm.DOC