Bài tâp C++ thông dụng

Dảnh cho sinh viên cao đẳng tham khảo #include #include //Ham kiem tra so nguyen to int KIEMTRA_NT(int n) { int ok=1; if (n<2) ok=0; else { for (int i=2;i<=sqrt(n);i++) if (n%i==0) { ok=0; break; } } return ok; } //chuong trinh chinh

doc30 trang | Chia sẻ: lvcdongnoi | Ngày: 03/07/2013 | Lượt xem: 253 | Lượt tải: 1download
Tóm tắt tài liệu Bài tâp C++ thông dụng, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Các bài tập lập trình C++ Mùa A Sênh – SP TinA-k44 (Sưu tầm) //Tong hop cac bai tap ve mang 1 chieu #include #include #define N 50 void nhap(int a[],int n) { for (int i=0;i<n;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } } void in_ra(int a[], int n) { for (int i=0;i<n;i++) cout<<a[i]<<" "; endln; } void max_min(int a[], int n) { int max,min,vt1,vt2; max=a[0]; vt1=0; for (int i=1;i<n;i++) if (max<a[i]) { max=a[i]; vt1=i; } min=a[0]; vt2=0; for (i=0;i<n;i++) if (min>a[i]) { min=a[i]; vt2=i; } cout <<"\nPhan tu max = "<<max<<" , nam o vi tri: "<<vt1; cout<<"\nPhan tu min= "<<min<<" , nam o vi tri: "<<vt2; } void hoanvi(int &x, int &y) { int tg=x; x=y; y=tg; } void sapxep(int a[], int n) { for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) if (a[i]>a[j]) hoanvi(a[i],a[j]); } int ktra_ngto(int n) { int ok=1; if (n<2) ok=0; else for (int i=2;i<=sqrt(n);i++) if (n%i==0) { ok=0; break; } return ok; } void sodgnn(int a[N],int n) { int b[N],j=0,sn; for (int k=0;k<n;k++) if (a[k]>0) { b[j]=a[k]; j++; } for (int i=0; i<j;i++) sapxep(b,j); cout<<"\nSo duong nho nhat la: "<<b[0]; } void in_ngto(int a[], int n) { for (int i=0;i<n;i++) if (ktra_ngto(a[i])) cout<<a[i]<<" "; } int ktra_hh(int n) { int m=0, ok=1; for (int i=1;i<=(n/2);i++) if (n%i==0) m=m+i; if (m!=n) ok=0; else ok=1; return ok; } void dem_hh(int a[],int n) { int dem=0; for (int i=0;i<n;i++) if (ktra_hh(a[i])) dem++; cout<<"\nCo "<<dem<<" so hoan hao trong mang"; } void main() { int a[50], n,dem=0; do //Dieu kien nhap 2<=n<=50; { cout <<"\nNhap vao so phan tu cua mang: "; cin>>n; } while (n50); nhap(a,n); //Nhap ma tran a; cout<<"\n"; //Xuong dong; max_min(a,n); //Max min cua mang a; int d1=0,d2=0,d3=0; for (int i=0;i<n;i++) //Dem so duong,so am,so 0; if (a[i]>0) d1++; else if (a[i]==0) d2++; else d3++; cout<<"\nMang co "<<d1<<" so duong, co "<<d2<<" so bang 0, co " <<d3<<" so nho hon 0"; sodgnn(a,n); //In ra so duong nho nhat; dem_hh(a,n); //Dem cac so hoan hao trong mang; cout<<"\nCac so nguyen to co trong mang la: "; in_ngto(a,n); //In cac so nguyen to cua mang; cout <<"\nMang sau khi sap xep la: "; sapxep(a,n); //Sap xep lai mang tang dan; in_ra(a,n); //In ra mang sau khi sap xep; } //Mang 2 chieu va file include #include #include //---------------------------------------------------------------------------------------------- int a[50][50],m,n; FILE *f; //---------------------------------------------------------------------------------------------- void nhap() { int ok=0; while (!ok) { cout<<"Nhap so hang va so cot: "; cin>>m>>n; if ((m>=2)&&(m=2)&&(n<=50)) ok=1; } for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) { cout<<"Nhap phan tu A["<<i<<","<<j<<"]: "; cin>>a[i][j]; } fprintf(f,"Mang vua nhap la: \n"); for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) fprintf(f,"%d%s",a[i][j]," "); fprintf(f,"\n"); } cout<<"Mang vua nhap la: \n"; for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<"\n"; } } //---------------------------------------------------------------------------------------------- void maxmin() { int max=a[1][1],min=a[1][1],vtmaxh=1,vtmaxc=1,vtminh=1,vtminc=1; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) { if (a[i][j]>max) { max=a[i][j]; vtmaxh=i; vtmaxc=j; } if (a[i][j]<min) { min=a[i][j]; vtminh=i; vtminc=j; } } cout<<"\nPhan tu lon nhat cua mang la: "<<max<<" o vi tri hang: "<<vtmaxh<<", cot: "<<vtmaxc; cout<<"\nPhan tu nho nhat cua mang la: "<<min<<" o vi tri hang: "<<vtminh<<", cot: "<<vtminc; fprintf(f,"Max cua mang la: %d,o vi tri hang: %d, cot: %d\n",max,vtmaxh,vtmaxc); fprintf(f,"Min cua mang la: %d, o vi tri hang: %d, cot: %d\n",min,vtminh,vtminc); } //---------------------------------------------------------------------------------------------- void tong() { long tong=0; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) tong+=a[i][j]; cout<<"\nTong cua mang la: "<<tong; float tb=tong/(m*n); printf("\nTrung binh cua mang la: %5.2f",tb); fprintf(f,"\nTong cua mang la: %d",tong); fprintf(f,"\nTrung binh cua mang la: %5.2f",tb); } //---------------------------------------------------------------------------------------------- main() { f=fopen("BAI2.TXT","w"); nhap(); maxmin(); tong(); fclose(f); } #include #include #include #include //------------------------------------------------------------------------------ int a[51][51],n; //------------------------------------------------------------------------------ void nhap() { while (1) { cout>n; if ((n>=2)&&(n<=50)) break; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { cout<<"Nhap phan tu A["<<i<<","<<j<<"]: "; cin>>a[i][j]; } } //------------------------------------------------------------------------------ void inmang() { for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<"\n"; } } //------------------------------------------------------------------------------ void maxmin() { int max=a[1][1],min=a[1][1],vtmaxh=1,vtmaxc=1,vtminh=1,vtminc=1; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { if (a[i][j]>max) { max=a[i][j]; vtmaxh=i; vtmaxc=j; } if (a[i][j]<min) { min=a[i][j]; vtminh=i; vtminc=j; } } cout<<"\nMax la: "<<max<<" o vi tri hang: "<<vtmaxh<<", cot: "<<vtmaxc; cout<<"\nMin la: "<<min<<" o vi tri hang: "<<vtminh<<", cot: "<<vtminc; } //------------------------------------------------------------------------------ int ktnt(int x) { if (x<2) return 0; else for (int i=2;i<=sqrt(x);i++) if (x%i==0) return 0; return 1; } //------------------------------------------------------------------------------ void tongnt() { int s=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (ktnt(a[i][j])) s+=a[i][j]; cout<<"\nTong cac so nguyen to trong mang la: "<<s; } //------------------------------------------------------------------------------ void main() { nhap(); inmang(); maxmin(); tongnt(); } //Fibo va file #include #include #include #include ------------------------------------------------------------ int n; FILE *f; ------------------------------------------------------------ void nhap() { cout<<"Nhap n: "; cin>>n; fprintf(f,"%d\n",n); } ------------------------------------------------------------ int ktnt(int x) { if (x<2) return 0; else for (int i=2;i<=sqrt(x);i++) if(x%i==0) return 0; return 1; } ------------------------------------------------------------ void fibo() { long i=2,f0=1,f1=1,d=0,f2; cout<<n<<" so fibonaci dau tien la: \n"; if(n<3)for (int j=1;j<=n;j++) { cout<<"1 "; fprintf(f,"1 "); } else { cout<<"1 1 "; fprintf(f,"1 1 "); while (i<n) { f2=f0+f1; f0=f1; f1=f2; i++; cout<<f2<<" "; fprintf(f,"%d%s",f2," "); if (ktnt(f2)) d++; } } cout<<"\nSo luong so nguyen to trong day fibo la: "<<d; fprintf(f,"\nSo luong so nguyen to trong day fibo la: %d",d); } ------------------------------------------------------------ main() { f=fopen("FIBO.OUT","w"); nhap(); fibo(); fclose(f); } //So phuc #include #include #include //------------------------------------------------------------------------------------------------ struct sophuc { float a,b; } so1,so2; //------------------------------------------------------------------------------------------------ void nhap() { cout<<"Nhap so phuc thu 1: \n"; cout>so1.a; cout>so1.b; cout<<"Nhap so phuc thu 2: \n"; cout>so2.a; cout>so2.b; } //------------------------------------------------------------------------------------------------ void cong() { sophuc so3; so3.a=so1.a+so2.a; so3.b=so1.b+so2.b; cout<<"("<<so1.a<<" + "<<so1.b<<"i) + ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } //------------------------------------------------------------------------------------------------ void tru() { sophuc so3; so3.a=so1.a-so2.a; so3.b=so1.b-so2.b; cout<<"("<<so1.a<<" + "<<so1.b<<"i) - ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } //------------------------------------------------------------------------------------------------ void nhan() { sophuc so3; so3.a=so1.a*so2.a-so1.b*so2.b; so3.b=so1.a*so2.b+so1.b*so2.a; cout<<"("<<so1.a<<" + "<<so1.b<<"i) * ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } //------------------------------------------------------------------------------------------------ void chia() { sophuc so3; so3.a=(so1.a*so2.a+so1.b*so2.b)/(so2.a*so2.a+so2.b*so2.b); so3.b=(so1.a*so2.b-so1.b*so2.a)/(so2.a*so2.a+so2.b*so2.b); cout<<"("<<so1.a<<" + "<<so1.b<<"i) / ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } //------------------------------------------------------------------------------------------------ main() { nhap(); cong(); tru(); nhan(); chia(); } #include #include #include #include //------------------------------------------------------------------------------------------ struct nhanvien { char ht[30],que[20]; int tuoi; }a[100]; int n; //------------------------------------------------------------------------------------------ void nhap() { char c; cout>n; scanf("%c",&c); for (int i=1;i<=n;i++) { cout<<"Nhap cho nhan vien thu"<<i<<":\n"; cout<<"Nhap ho ten: ";cin.getline(a[i].ht,30); cout<<"Nhap que: ";cin.getline(a[i].que,20); cout>a[i].tuoi; scanf("%c",&c); } } //------------------------------------------------------------------------------------------ void inkq() { cout<<"\nDanh sach nhan vien vua nhap la: "; for (int i=1;i<=n;i++) { cout<<"\nNhan vien thu "<<i; cout<<"\nHo ten: "<<a[i].ht; cout<<"\nQue: "<<a[i].que; cout<<"\nTuoi: "<<a[i].tuoi; } } //------------------------------------------------------------------------------------------ void dem() { int d=0; for (int i=1;i<=n;i++) if(a[i].tuoi>=20) d++; cout=20 tuoi la: "<<d; } //------------------------------------------------------------------------------------------ void sapxep() { nhanvien tg; for (int i=1;i<n;i++) for (int j=i+1;j<=n;j++) if (strcmpi(a[i].que,a[j].que)>0) { tg=a[i]; a[i]=a[j]; a[j]=tg; } cout<<"\n\nDanh sach nhan vien sap xep theo que la:"; inkq(); } //------------------------------------------------------------------------------------------ main() { nhap(); inkq(); dem(); sapxep(); } //Quan ly Sinh vien + cau truc tu tro #include #include #include #include //--------------------------------------------------------------------------------------- struct SINHVIEN { char ht[30],lop[10]; float toan,tin,anh; SINHVIEN *tiep; }*pdau,*p; int n=0; //--------------------------------------------------------------------------------------- void nhap() { char hoten[30],l[10]; float dtoan,dtin,danh; pdau=NULL; while (1) { cout<<"Nhap ho ten: ";cin.getline(hoten,30); cout<<"Lop: ";cin.getline(l,10); cout<<"Diem toan, tin, anh: "; cin>>dtoan>>dtin>>danh; char tl; cin.ignore(); if (pdau==NULL) { pdau=new (SINHVIEN); p=pdau; } else { p->tiep=new (SINHVIEN); p=p->tiep; } strcpy(p->ht,hoten); strcpy(p->lop,l); p->toan=dtoan; p->tin=dtin; p->anh=danh; n++; cout>tl; if (tl=='K'||tl=='k') break; cin.ignore(); } } //--------------------------------------------------------------------------------------- void inkq() { p=pdau; for (int i=1;i<=n;i++) { couthtlop<<"\n"; couttoantinanh<<"\n\n"; p=p->tiep; } } //--------------------------------------------------------------------------------------- int dem() { p=pdau; int d=0; for (int i=1;i<=n;i++) { if (((p->toan)+(p->tin)+(p->anh))/3.0>=7) d++; p=p->tiep; } return d; } //--------------------------------------------------------------------------------------- void main() { nhap(); cout<<"\nDanh sach sinh vien vua nhap la: \n\n"; inkq(); cout=7 la: "<<dem()<<"\n"; } #include #include #include //------------------------------------------------------------------------------------ struct hoso { char ht[30],lop[10]; int toan, tin,anh; }a[100]; int n; //------------------------------------------------------------------------------------ void nhap() { int i=1; while (1) { printf("Nhap ho ten: ");gets(a[i].ht); printf("Nhap lop: ");gets(a[i].lop); printf("Nhap diem toan, tin, anh: ");scanf("%d%d%d",&a[i].toan,&a[i].tin,&a[i].anh); i++; char tl,c; scanf("%c",&c); printf("Ban co muon tiep tuc ko?(C/K): ");scanf("%c",&tl); scanf("%c",&c); if (tl=='k'||tl=='K') break; } n=i-1; } //------------------------------------------------------------------------------------ void inkq() { for (int i=1;i<=n;i++) { printf("\n"); printf("Ho va ten: %s \n",a[i].ht); printf("Lop: %s\n",a[i].lop); printf("Diem toan: %d, Diem tin: %d, Diem anh: %d \n",a[i].toan,a[i].tin,a[i].anh); } } //------------------------------------------------------------------------------------ int dem() { int d=0; for (int i=1;i<=n;i++) if((a[i].toan+a[i].tin+a[i].anh)/3.0>=7) d++; return d; } //------------------------------------------------------------------------------------ void main() { nhap(); inkq(); printf("So luong sinh vien duoc thi top nghiep la: %d",dem()); } //File void inrafile() { FILE *f; f=fopen("SO.OUT","w"); for (int i=1;i<=n;i++) fprintf(f,"%d%s",a[i]," "); fprintf(f,"%s","\n"); fprintf(f,"%s%d","\nso cac so nguyen to co trong mang la: ") } //Quan ly nhan su #include #include #include //--------------------------------------------------------------------------------------- struct NHANSU { char ho[20],ten[20]; int ngay, thang, nam; }a[100]; int n; //--------------------------------------------------------------------------------------- void nhap() { int i=1; while (1) { cout<<"Nhap ho: ";cin.getline(a[i].ho,20); cout<<"Nhap ten: ";cin.getline(a[i].ten,20); cout>a[i].ngay; cout>a[i].thang; cout>a[i].nam; char c,tl; scanf("%c",&c); i++; cout>tl; if (tl=='k'||tl=='K') break; scanf("%c",&c); } n=i-1; } //--------------------------------------------------------------------------------------- void inkq() { for (int i=1;i<=n;i++) { cout<<"\nHo va ten: "<<a[i].ho<<" "<<a[i].ten; cout<<"\nSinh ngay: "<<a[i].ngay<<"/"<<a[i].thang<<"/"<<a[i].nam; } cout<<"\n"; } //--------------------------------------------------------------------------------------- int dem() { int d=0; for (int i=1;i<=n;i++) if((a[i].nam>=1990)&&(a[i].nam<=2000))d++; return d; } //--------------------------------------------------------------------------------------- void main() { nhap(); cout<<"\nMang vua nhap la: "; inkq(); cout 2000."; } //QL Nhan su #include #include #include #include //-------------------------------------------------------------------------------------- struct NHANSU { char hoten[30]; int tuoi,hsl; }; int n; //-------------------------------------------------------------------------------------- void nhap() { NHANSU a; FILE *f; f=fopen("NHANSU.DAT","w"); while (1) { cout<<"Nhap ho ten: ";cin.getline(a.hoten,30); cout>a.tuoi; cout>a.hsl; cin.ignore(); char tl; cout>tl; cin.ignore(); fwrite(&a,sizeof(NHANSU),1,f); if (tl=='k'||tl=='K') break; } fclose(f); } //-------------------------------------------------------------------------------------- void inkq() { FILE *f; f=fopen("NHANSU.DAT","r"); NHANSU a; while (fread(&a,sizeof (NHANSU),1,f)>0) cout<<"\nHo ten: "<<a.hoten<<", tuoi: "<<a.tuoi<<", he so luong: "<<a.hsl; fclose(f); } //-------------------------------------------------------------------------------------- int dem() { FILE *f; f=fopen("NHANSU.DAT","r"); NHANSU a; int d=0; while (fread(&a,sizeof(NHANSU),1,f)>0) if ((a.hsl)*500>2500000) d++; return d; } //-------------------------------------------------------------------------------------- void main() { nhap(); inkq(); cout=2tr5 la: "<<dem(); } #include #include #include //----------------------------------------------------------------------------- struct NHANSU { char ht[30],gt[5],que[20]; int tuoi; }; int n=0; //----------------------------------------------------------------------------- void nhap() { NHANSU a; FILE *f; f=fopen("NHANSU.TXT","wb"); while (1) { cout<<"Nhap ho va ten: ";cin.getline(a.ht,30); cout<<"Nhap gioi tinh: ";cin.getline(a.gt,5); cout<<"Nhap que: ";cin.getline(a.que,20); cout>a.tuoi; cin.ignore(); fwrite(&a,sizeof(NHANSU),1,f); n++; char tl; cout>tl; cin.ignore(); if (tl=='k'||tl=='K') break; } fclose(f); } //----------------------------------------------------------------------------- void inkq() { FILE *f; NHANSU a; f=fopen("NHANSU.TXT","rb"); while (fread(&a,sizeof(NHANSU),1,f)>0) { cout<<"\nHo ten: "<<a.ht; cout<<"\nGioi tinh: "<<a.gt; cout<<"\nQue: "<<a.que; cout<<"\nTuoi: "<<a.tuoi; } cout<<"\n"; fclose(f); } //----------------------------------------------------------------------------- void chen() { NHANSU a,tg; FILE *f,*ftg; f=fopen("NHANSU.TXT","rb"); int m; cout>m; cin.ignore(); m--; if (m>=0&&m<=n) { ftg=fopen("FILETG.TXT","wb"); int tt=0; while(fread(&a,sizeof(NHANSU),1,f)>0) { fwrite(&a,sizeof(NHANSU),1,ftg); if(tt==m) while (1) { cout<<"Nhap du lieu can chen: \n"; cout<<"Nhap ho ten: ";cin.getline(tg.ht,30); cout<<"Nhap gioi tinh: ";cin.getline(tg.gt,5); cout<<"Nhap que: ";cin.getline(tg.que,20); cout>tg.tuoi; cin.ignore(); fwrite(&tg,sizeof(NHANSU),1,ftg); n++; char tl; cout>tl; cin.ignore(); if (tl=='k'||tl=='K') break; } tt++; } fclose(f); fclose(ftg); remove("NHANSU.TXT"); rename("FILETG.TXT","NHANSU.TXT"); } } //----------------------------------------------------------------------------- void xoa() { NHANSU a; FILE *f,*ftg; f=fopen("NHANSU.TXT","rb"); int m; cout>m; cin.ignore(); if (m>=0&&m<=n) { ftg=fopen("FILETG.TXT","wb"); int tt=0; while (fread(&a,sizeof(NHANSU),1,f)>0) { if (tt!=m) { fwrite(&a,sizeof(NHANSU),1,ftg); tt++; } tt++; } } fclose(f); fclose(ftg); remove("NHANSU.TXT"); rename("FILETG.TXT","NHANSU.TXT"); } //----------------------------------------------------------------------------- void main() { nhap(); inkq(); chen(); cout<<"\nDanh sach sau khi duoc chen them vao la: \n"; inkq(); xoa(); cout<<"\nDanh sach sau khi duoc xoa la: \n"; inkq(); } #include #include #include char s[50]; int n,dem=0; void nhap() { cout<<"Nhap xau: "; cin.getline(s,50); n=strlen(s); } void batdau(char *s) { char x[50]=" "; int d=0; strcat(x,s); for (int i=0;i<strlen(x);i++) if (((x[i]==' ')&&(x[i+1]=='n'))||((x[i]==' ')&&(x[i+1]=='N'))) d++; cout<<"\nSo tu bat dau bang chu N or n la: "<<d; } void ketthuc(char *s) { strcat(s," "); int d=0; for (int i=0;i<strlen(s);i++) if (((s[i]=='g')&&(s[i+1]==' '))||((s[i]=='G')&&(s[i+1]==' '))) d++; cout<<"\nSo luong tu ket thuc bang chu G or g la: "<<d; } int ktra(char *x, int k) { int ok=1; for (int i=0;i<=k/2;i++) if (x[i]!=x[k-i-1]) { ok=0; break; } return ok; } void tachtu() { char x[30]; int i=0,k=0; while (i<n) { while ((s[i]!=' ')&&(i<n)) { x[k]=s[i]; k++; i++; } if (ktra(x,k)) dem++; k=0; i++; } } void inkq() { cout<<"\nSo tu doi xung trong xau la: "<<dem; } void main() { nhap(); batdau(s); ketthuc(s); tachtu(); inkq(); } //Dem tu #include #include #include int dem_tu(char *s) { int n=strlen(s),d=0; for (int i=0;i<n;i++) if (s[i]!=' '&&s[i+1]==' ') d++; return d; } void main() { char ch[80]; FILE *f; /*f=fopen("chuoi.TXT","w"); while (strlen(gets(ch))>0) { fputs(ch,f); fputs("\n",f); } fclose(f);*/ cout<<"\nNoi dung tep la: "; f=fopen("chuoi.TXT","r"); while (fgets(ch,80,f) !=NULL) { cout <<ch; cout<<"\nSo tu co trong tep vb la: "; dem_tu(ch); } fclose(f); } //Fibo # include # include # include long fibo(int n) { if (n<3) return 1; else return (fibo(n-2)+fibo(n-1)); } int nto(int n) { int ok=1; if (n<2) ok=0; else for(int i=2;i<=sqrt(n);i++) if (n%i==0) { ok=0; break; }; return ok; } int dem(int n) { int d=0; for (int i=0;i<n;i++) if (nto(fibo(i))) d=d+1; return d; } void main() { FILE *f; int n,d=0; cout>n; f= fopen("fbb.txt","w"); fprintf(f,"%d%s",n,"\n"); for (int i=0;i<n;i++) fprintf(f,"%d%s",fibo(i)," "); d=dem(n); fprintf(f,"\nso luong so nguyen to trong day fibo la: %d",d); } //Ma tran file #include #include #define N 50 char ten[]="matran.TXT"; void ghi(int a[][N],int n, int m) { FILE *f; f=fopen(ten,"wb"); for (int i=0;i<n;i++) for (int j=0;j<m;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cin>>a[i][j]; fwrite(&a,sizeof(int),n*m,f); } fclose(f); } void doc(int a[][N],int n,int m) { FILE *f; if ((f=fopen(ten,"rb"))==NULL) cout <<"\nLoi mo tep"; else { fread (&a,sizeof(int),n*m,f); for(int i=0;i<n;i++) { cout<<"\n"; for (int j=0;j<m;j++) cout<<a[i][j]<<" "; } } fclose(f); } void main() { int a[N][N],n,m; cout<<"\nNhap vao kich thuoc ma tran: "; cin>>n>>m; ghi(a,n,m);cout<<endl; doc(a,n,m); } //Ham kt nguyen to #include #include //Ham kiem tra so nguyen to int KIEMTRA_NT(int n) { int ok=1; if (n<2) ok=0; else { for (int i=2;i<=sqrt(n);i++) if (n%i==0) { ok=0; break; } } return ok; } //chuong trinh chinh void main() { int k; cout <<"\nNhap vao so k= "; cin>>k; if (KIEMTRA_NT(k)) cout <<"\n"<<k<<" la so nguyen to"; else cout <<"\n"<<k<<" khong la so nguyen to"; } //Cau truc tu tro #include #include #include #include typedef struct hoso { char hoten[25]; int tuoi; struct hoso *tiep; }nhansu; nhansu *pdau,*p,p1; void nhap(); void inra(); void them(); void chen(int n); void tim(int tuoi); void xoa(int n); void main() { nhap(); inra(); tim(20); delete p; delete pdau; } void nhap() { char ht[25]; int t; pdau=NULL; while (1) { cout<<"Ho va ten: ";cin.gestline(ht,25); cout>t; if (pdau==NULL) { pdau=new hoso; p=pdau; } else { p->tiep =new hoso; p=p->tiep; } strcp(p->hoten,ht); p->tiep=NULL; char tl; cout<<"\nBan muon nhap tiep khong? (c/k)"; cin>>tl; if (tl=='K' || tl=='k') break; } } void inra() { p=pdau; while (p!=NULL) { cout<<(*p).hoten<<" "<<(*p).tuoi<<endl; p=p->tiep; } } void tim(int tuoi) { p=pdau; cout<<"\nDanh sach nhung nguoi tuoi <= "<<tuoi<<endl; int i=1; while (p!=NULL) { if (p->tuoi<=tuoi) { cout<<i<<" "<<(*p).hoten<<endl; i++; } p=p->tiep; } } //Xau doi xung #include #include #include char s[50]; int n,dem=0; void nhap() { cout<<"Nhap xau: "; cin.getline(s,50); n=strlen(s); } int ktra(char *x, int k) { int ok=1; for (int i=0;i<=k/2;i++) if (x[i]!=x[k-i-1]) { ok=0; break; } return ok; } void tachtu() { char x[30]; int i=0,k=0; while (i<n) { while ((s[i]!=' ')&&(i<n)) { x[k]=s[i]; k++; i++; } if (ktra(x,k)) dem++; k=0; i++; } } int ktdx(char *s) //bai cua thay; { int n=strlen(s); if (n==0) return 0; int m=n/2, ok=1; for (int i=0; i<m;i++) if (s[i]!=s[n-i-1]) { ok=0; break; } return ok; } void demdx(char *s) //bai cua thay; { int k=0,sotudx=0,n=strlen(s); char *tg=new char(); strcat(s," "); for (int i=0;i<n;i++) { if (s[i]!=' ') { tg[k]=s[i]; k++; } else { tg[k]='\0'; k=0; } if (k==0) { if (ktdx(tg)) { cout<<" "<<tg; sotudx++; } } } cout<<"\nLa cac tu doi xung\n"; cout<<"\nSo tu doi xung la: "<<sotudx<<"\n"; } void int() { nhap(); tachtu(); cout<<"\nSo tu doi xung trong xau la: "<<dem<<"\n"; cout<<"\nbai tiep: \n"; demdx(s); } //Kiem tra tu #include #include #include void main() { char *st; char them[1]={' '}; printf("\nNhap vao mot xau ki tu: "); gets(st); int n=strlen(st); int d=0; for (int i=0;i<n;i++) if (st[i]!=' ' && st[i+1]==' ') d++; cout<<"\nCo "<<d<<" tu"; d=0; st=strcpy(st,them); for (int j=0;j<n;j++) if (st[j]==' ' && (st[j+1]=='n' || st[j+1]=='N')) d++; cout<<"\nCo "<<d<<" tu tu bat dau bang N hoac n"; d=0; for (int k=0;k<n;k++) if (st[k+1]==' ' && (st[k]=='g' || st[k]=='G')) d++; cout<<"\nCo "<<d<<" tu tu ket thuc bang N hoac n"; } THE END

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

  • docBài tâp c++ thông dụng.doc