Share cho khóa 4 học C++ chuẩn bị kiểm tra giữa kì Con trỏ kí tự - full: Mã: #include <iostream> #include <string> using namespace std; void nhap(char *p) { cout<<"Nhap xau..."<<endl; gets(p); } void xuat(char *p) { cout<<"Xuat..."<<endl; cout<<p; } void xuatnguoc(char *p) { for(int i=strlen(p)-1;i>=0;i--) { cout<<*(p+i); } } void demkitu(char *p) { int dem=0; for(int i=0;i<strlen(p);i++) if(*(p+i)==' ' || *(p+i)==NULL) dem+=1; cout<<"\n So ki tu :"<<strlen(p)-dem; } void demtu(char *p) { int demtu=0, i=0; while(i<strlen(p)) { if(*(p+i)==' ') { i++; continue; } else { while(*(p+i)!=' ') { i++; } demtu++; } } cout<<"\n So tu:"<<demtu; } void hoa_thuong(char *p) { int hoa=0; int thuong=0; for(int i=0;i<strlen(p);i++) { if(*(p+i)==toupper(*(p+i)) && *(p+i)!=' ') hoa+=1; if(*(p+i)==tolower(*(p+i)) && *(p+i)!=' ') thuong+=1; } cout<<"\n So ki tu hoa :"<<hoa<<endl; cout<<"So ki tu thuong :"<<thuong<<endl; } void chuyen(char *p) { for(int i=0;i<strlen(p);i++) *(p+i)=toupper(*(p+i)); cout<<"Chuoi hoa..."<<p; } /* bool check(const char *str1, const char *str2) { if (strstr(str1,str2)) return true; return false; }*/ void del_space(char *p) { for(int i=0;i<strlen(p);i++) { if(*(p+i)==' ') for(int j=i;j<strlen(p);j++) *(p+j)=*(p+j+1); i--; } cout<<p; } void main() { char *p;char *x; x=new char[100]; p=new char[100]; nhap(p); xuat(p); cout<<"Xuat nguoc \n"<<endl; xuatnguoc(p); demkitu(p); demtu(p); hoa_thuong(p); chuyen(p); cout<<endl; del_space(p); } Mảng 2 chiều: - Nhập, xuất - Phần tử chẵn, tb các phần tử - Tổng các phần tử trên hàng/cột/đường chéo chính/ phụ - Kiểm tra đối xứng Mã: #include <iostream> using namespace std; void main() { int a[10][10]; int m,n,i,j; //Nhap, xuat mang 2 chieu cout<<"Nhap so hang, so cot (m,n)"<<endl; cin>>m>>n; cout<<"Nhap gia tri pt mang"<<endl; for(i=0;i<m;i++) for(j=0;j<n;j++) { cout<<"a["<<i+1<<"]["<<j+1<<"]="; cin>>a[i][j]; } cout<<"Gia tri vua nhap"<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<"\t"; } cout<<endl; } cout<<endl; //Cac phan tu chan cout<<"Cac phan tu chan"<<endl; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(a[i][j]%2==0) cout<<"a["<<i+1<<"]["<<j+1<<"]="<<a[i][j]<<"\t"; } cout<<endl; //Trung binh cong cac phan tu float t=0;float tb; for(i=0;i<m;i++) for(j=0;j<n;j++) t+=a[i][j]; tb=t/(m*n); cout<<"TB cac phan tu = "<<tb<<endl; //Tong cac phan tu tren tung hang cout<<"Tong cac phan tu tren tung hang"<<endl; int tong[10]; for(i=0;i<m;i++) { tong[i]=0; for(j=0;j<n;j++) { tong[i]+=a[i][j]; } } for(i=0;i<m;i++) cout<<"Hang "<<i+1<<" = "<<tong[i]<<endl; //Tong cac phan tu tren tung cot cout<<"Tong cac phan tu tren tung cot"<<endl; for(j=0;j<n;j++) { float tongc=0; for(i=0;i<m;i++) tongc+=a[i][j]; cout<<"\nCot"<<j+1<<" la: "<<tongc<<endl; } //Tong duong cheo chinh, duong cheo phu (m=n) if(m!=n) cout<<"Ko phai ma tran vuong"; else { //Duong cheo chinh int dcc=0; for(i=0;i<m;i++) dcc+=a[i][i]; cout<<"Tong duong cheo chinh la: "<<dcc<<endl; //Duong cheo phu int dcf=0; for(i=0;i<m;i++) for(j=0;j<n;j++) if(i==n-1-j) dcf+=a[i][j]; cout<<"Tong duong cheo phu la:"<<dcf<<endl; } //Ma tra doi xung int k; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(a[i][j]==a[j][i]) k=1; else k=0; } if(k==1) cout<<"Ma tran doi xung"; else cout<<"ma tran khong doi xung"; } Trộn mảng, sắp xếp: Mã: #include <iostream> #define max 100 using namespace std; //Nhap void nhap(int a[], int n) { for(int i=0;i<n;i++) cin>>a[i]; } //Xuat void xuat(int a[], int n) { for(int i=0;i<n;i++) cout<<a[i]<<"\t"; } //Tron void tron(int a[],int b[],int c[], int n, int m) { for (int i=0;i<n;i++) { c[i]=a[i]; } i=n; for (int j=0;j<m;j++) { c[i+j]=b[j]; } } //Sap xep void sapxep(int a[], int n) { for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(a[i]>a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } } void main() { int a[max], b[max], c[max]; int n1,n2; cout<<"Nhap so phan tu mang 1"<<endl; cin>>n1; cout<<"Nhap gia tri phan tu mang 1"<<endl; nhap(a,n1); cout<<endl; cout<<"Nhap so phan tu mang 2"<<endl; cin>>n2; cout<<"Nhap gia tri phan tu mang 2"<<endl; nhap(b,n2); cout<<endl; cout<<"Gia tri 1 mang vua nhap"<<endl; cout<<"Mang 1"<<endl; xuat(a,n1); cout<<endl; cout<<"Mang 2"<<endl; xuat(b,n2); cout<<endl; cout<<"--------------------------"; tron(a,b,c,n1,n2); sapxep(c,n1+n2); cout<<"Hai mang sau khi tron"<<endl; xuat(c,n1+n2); }