Ôn tập lập trình C++

20 146 0
Ôn tập lập trình C++

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 Tính tổng S(n)= 1+ ½ + 1/3+1/4+…+1/n } #include #include Tìm số đảo ngược số nguyên dương n Vd nhập 123456 in 654321 int main() #include { int i, n; #include float S; int main() S = 0; i = 1; { int n; { int SoNghichDao = 0; printf("\nNhap n: "); scanf("%d", &n); { printf("\nNhap n: "); if(n < 1) scanf("%d", &n); { }while(n < && printf("\nLoi: (n >= 0)")); printf("\nN phai > = bang Xin nhap lai !"); } printf("\nSo dao nguoc cua %d la: ", n); }while(n < 1); { printf("%d", n % 10); while(i địa biến pa, // có địa // địa biến trỏ pa // địa mà trỏ pa trỏ đến delete[] a[i]; } delete[] a; // Toán tử * để lấy giá trị mà trỏ trỏ đến // Ko để tốn tử lấy địa mà trỏ trỏ đến // Nếu để dấu & trước tên trỏ ta lấy "địa riêng" trỏ getch(); getch(); return 0; return 0; } } 11 Con trỏ ** Khai báo khởi tạo 12 SourceCode Phân số #include #include #include #include #include int main() { struct PhanSo int a = 5, *pa = &a; { printf("\nGia tri bien pa: %d", pa); float TuSo; // -> pa &a địa biến a float MauSo; }; printf("\nDia chi cua bien a: %d", &a); // -> *pa a nội dung biến a typedef struct PhanSo PHANSO; 12 scanf("%f", &temp); void NhapPhanSo(PHANSO &); ps.MauSo = temp; void XuatPhanSo(PHANSO); if(ps.MauSo == 0) PHANSO RutGonPhanSo(PHANSO); { PHANSO TinhTong2PhanSo(PHANSO, PHANSO); printf("\nMau so phai khac Xin kiem tra lai !"); PHANSO TinhHieu2PhanSo(PHANSO, PHANSO); } PHANSO TinhTich2PhanSo(PHANSO, PHANSO); }while(ps.MauSo == 0); PHANSO TinhThuong2PhanSo(PHANSO, PHANSO); } bool KiemTraPhanSoToiGian(PHANSO); void QuiDong2PhanSo(PHANSO &, PHANSO &); void XuatPhanSo(PHANSO ps) bool KiemTraPhanSoDuong(PHANSO); { bool KiemTraPhanSoAm(PHANSO); printf("%.2f / %.2f", ps.TuSo, ps.MauSo); int SoSanh2PhanSo(PHANSO, PHANSO); } PHANSO operator +(PHANSO, PHANSO); PHANSO operator -(PHANSO, PHANSO); float TimUCLN(float a, float b) PHANSO operator *(PHANSO, PHANSO); { PHANSO operator /(PHANSO, PHANSO); if(a < 0) { a = a * -1; void NhapPhanSo(PHANSO &ps) } { if(b < 0) float temp; { printf("\nNhap vao tu so: "); b = b * -1; scanf("%f", &temp); } ps.TuSo = temp; if(a == && b !=0) { { printf("\nNhap vao mau so: "); 13 return b; { } PHANSO ketqua; else if( b == && a != ) ketqua.TuSo = a.TuSo * b.MauSo + a.MauSo * b.TuSo; { ketqua.MauSo = a.MauSo * b.MauSo; return a; return ketqua; } } // Bài 508 : tính hiệu phân số while(a != b) PHANSO TinhHieu2PhanSo(PHANSO a, PHANSO b) { { if(a > b) PHANSO ketqua; { ketqua.TuSo = a.TuSo * b.MauSo - a.MauSo * a = a - b; b.TuSo; } ketqua.MauSo = a.MauSo * b.MauSo; else return ketqua; b = b - a; } } // Bài 509 : tính tích phân số return a; PHANSO TinhTich2PhanSo(PHANSO a, PHANSO b) { } // Bài 506: Rút gon phân số PHANSO ketqua; PHANSO RutGonPhanSo(PHANSO a) ketqua.TuSo = a.TuSo * b.TuSo; { ketqua.MauSo = a.MauSo * b.MauSo; return ketqua; float UCLN = TimUCLN(a.TuSo, a.MauSo); a.TuSo = a.TuSo / UCLN; } a.MauSo = a.MauSo / UCLN; return a; } // Bài 507: tính tổng phân số // Bài 510 tính thương phân số PHANSO TinhThuong2PhanSo(PHANSO a, PHANSO b) { PHANSO TinhTong2PhanSo(PHANSO a, PHANSO b) 14 PHANSO ketqua; } ketqua.TuSo = a.TuSo * b.MauSo; ketqua.MauSo = a.MauSo * b.TuSo; //513 Kiểm tra phân số dương return ketqua; bool KiemTraPhanSoDuong(PHANSO a) } { if (a.TuSo >= && a.MauSo > 0) // Bài 511 kiểm tả phân số tối giản return true; bool KiemTraPhanSoToiGian(PHANSO a) { return false; } PHANSO c = RutGonPhanSo(a); if (c.TuSo == a.TuSo && c.MauSo == a.MauSo) return true; //514 Kiểm tra phân só âm bool KiemTraPhanSoAm(PHANSO a) { return false; if (a.TuSo < || a.MauSo < 0) } return true; return false; // Bài 512 quy đồng phân số } void QuiDong2PhanSo(PHANSO &a, PHANSO &b) { //515 So sánh phân số: hàm trả giá trị: 0,1,1 PhanSo temp; int SoSanh2PhanSo(PHANSO a, PHANSO b) temp.TuSo = a.TuSo; { temp.MauSo = a.MauSo; float kq1 = a.TuSo / a.MauSo; float kq2 = b.TuSo / b.MauSo; a.TuSo *= b.MauSo; if (kq1 < kq2) a.MauSo *= b.MauSo; return -1; if (kq1 > kq2) b.TuSo *= temp.MauSo; return 1; b.MauSo *= temp.MauSo; return 0; 15 } PhanSo kq; //516 Ð?nh nghia toán t? operator + cho phân s? kq.TuSo = a.TuSo * b.MauSo; PHANSO operator +(PHANSO a, PHANSO b) kq.MauSo = a.MauSo * b.TuSo; { return kq; PhanSo kq; } kq.TuSo = a.TuSo * b.MauSo + a.MauSo * b.TuSo; kq.MauSo = a.MauSo * b.MauSo; //520 Ð?nh nghia toán t? operator ++ cho phân s? return kq; PHANSO operator ++ (PHANSO &a) } { //517 Ð?nh nghia toán t? operator - cho phân s? a.TuSo += a.MauSo; PHANSO operator -(PHANSO a, PHANSO b) return a; { } PHANSO kq; //521 Ð?nh nghia toán t? operator cho phân s? kq.TuSo = a.TuSo * b.MauSo - a.MauSo * b.TuSo; PHANSO operator ++ (PHANSO &a) kq.MauSo = a.MauSo * b.MauSo; { return kq; a.TuSo -= a.MauSo; } return a; //518 Ð?nh nghia toán t? operator * cho phân s? } PHANSO operator *(PHANSO a, PHANSO b) int main() { { PhanSo kq; PHANSO ps1, ps2; kq.TuSo = a.TuSo * b.TuSo; NhapPhanSo(ps1); kq.MauSo = a.MauSo * b.MauSo; XuatPhanSo(ps1); return kq; NhapPhanSo(ps2); } XuatPhanSo(ps2); //519 Ð?nh nghia toán t? operator / cho phân s? PHANSO operator /(PHANSO a, PHANSO b) // 506 { PHANSO a = RutGonPhanSo(ps1); 16 printf("\nPhan so sau rut gon: "); XuatPhanSo(a); // 511 bool Check = KiemTraPhanSoToiGian(a); PHANSO b = RutGonPhanSo(ps2); if (Check) printf("\nPhan so sau rut gon: "); printf("\nPhan so a toi gian roi"); XuatPhanSo(b); else printf("\nPhan so a chua toi gian"); // 507 // 512 PHANSO tong = TinhTong2PhanSo(a, b); QuiDong2PhanSo(ps1, ps2); printf("\nTong phan so: "); printf("\nQuy dong phan so: "); XuatPhanSo(tong); XuatPhanSo(ps1); printf("\nQuy dong phan so: "); // 508 XuatPhanSo(ps2); PHANSO hieu = TinhHieu2PhanSo(a, b); getch(); printf("\nHieu phan so: "); return 0; XuatPhanSo(hieu); } // 509 16 Tập tin Đọc in nội dung tập tin PHANSO tich = TinhTich2PhanSo(a, b); printf("\nTich phan so: "); XuatPhanSo(tich); // 510 PHANSO thuong = TinhThuong2PhanSo(a, b); #include #include void transFile(FILE* inFile, FILE* outFile) { printf("\nThuong phan so: "); int ch; XuatPhanSo(thuong); while(1) 17 { #include ch = fgetc(inFile); if(!feof(inFile)) void UpcaseFile(FILE* inoutFile) { { fputc(ch, outFile); char ch; } long pos; else while(!feof(inoutFile)) break; { } ch = fgetc(inoutFile); // d?c ký t? hi?n hành } if(feof(inoutFile)){ void main() break; { } FILE *fpIn; char* fname = "Data.txt"; fpIn = fopen(fname,"rt"); pos = ftell(inoutFile); nh?n v? trí d?c ghi hi?n t?i // ghi fseek(inoutFile, pos - 1, SEEK_SET); // d?i v? trí d?c ghi lên tru?c if(fpIn == NULL) ch = toupper(ch); // d?i fputc(ch, inoutFile); // ghi sang ký t? hoa { printf("FIle %s not found\n", fname); ký t? } transFile(fpIn, stdout); SEEK_SET); fseek(inoutFile, pos, // d?i v? trí d?c ghi v? ch? cu } fclose(fpIn); } getch(); } void main() { char* fname = "Data.txt"; ** In hoa chữ tập tin #include #include FILE* fpInOut = fopen(fname, "r + b"); if(fpInOut == NULL) { 18 printf("File %s not found!\n", fscanf(f, "%d", &a[i]); fname); } return; fclose(f); } } UpcaseFile(fpInOut); void XuatMang(int n, int a[]) fclose(fpInOut); { getch(); for(int i = 0; i < n; i++) } { printf("%4d", a[i]); } ** Nhập mảng từ file printf("\n"); #include } #include void XuatMangRaFile(char TenFile[], int n, int a[]) void NhapMangTuFile(char TenFile[], int &n, int a[]) { { FILE* f; f = fopen(TenFile, "w"); FILE* f; if(f == NULL) f = fopen(TenFile, "r"); { if(f == NULL) printf("\nKhong tao duoc file\n"); { fclose(f); printf("\nKhong doc duoc file"); return; fclose(f); } return; for(int i = 0; i < n; i++) } { fprintf(f, "%4d", a[i]); fscanf(f, "%d", &n); } for(int i = 0; i < n; i++) fclose(f); { } 19 int main() } { return found; int n; } int a[100]; NhapMangTuFile("input.txt", n, a); void main() printf("\nMang da nhap"); { XuatMangRaFile("output.txt", n, a); FILE* fp = fopen("Data.txt", "rt"); getch(); char *str = "This is"; return 0; if(fp != NULL) } { ** Tìm chuỗi kí tự long pos = findLine(fp, str); if(pos != -1) #include { #include printf("Found at line # %ld\n", pos); #include } #define maxN 2048 else { long findLine(FILE* inFile, char *str) printf("\n" "%s not found in file \n", str); { } long count = 0, found = -1; char line[maxN]; } while(fgets(line, maxN - 1, inFile) != NULL) { getch(); if(strncmp(str, line, strlen(str)) == 0) } { found = count; break; } count++; 20 ... TinhHieu2PhanSo(a, b); getch(); printf("\nHieu phan so: "); return 0; XuatPhanSo(hieu); } // 509 16 Tập tin Đọc in nội dung tập tin PHANSO tich = TinhTich2PhanSo(a, b); printf("\nTich phan so: "); XuatPhanSo(tich);... ghi v? ch? cu } fclose(fpIn); } getch(); } void main() { char* fname = "Data.txt"; ** In hoa chữ tập tin #include #include FILE* fpInOut = fopen(fname, "r + b"); if(fpInOut ==

Ngày đăng: 14/04/2018, 02:12

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan