Các bài tập C căn bản

PHANSO tru(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.mauso - a.mauso * b.tuso; c.mauso = a.mauso * b.mauso; c = uocluoc(c); return c; } PHANSO nhan(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.tuso; c.mauso = a.mauso * b.mauso; c = uocluoc(c); return c; } PHANSO chia(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.mauso; c.mauso = a.mauso * b.tuso; c = uocluoc(c); return c; }

doc28 trang | Chia sẻ: hachi492 | Ngày: 07/01/2022 | Lượt xem: 371 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Các bài tập C căn bản, để 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 C căn bản P4 --------------------------------- /* Bai tap 1_93 - In tat ca cac uoc so cua so n */ #include #include void main() { int n, i; printf("Cho gia tri N = "); scanf("%d", &n); printf("Cac uoc so cua %d la :\n", n); for (i=1; i<n; i++) if ((n % i) == 0) printf("%5d", i); getch(); } ---------------------------- #include unsigned USCLN (unsigned n, unsigned m) { while (n != 0 && m != 0) if (n>m) n -= m; else m -= n; if (n == 0) return m; else return n; } unsigned BSCNN (unsigned n, unsigned m) { return n * m / USCLN(n, m); } void main() { unsigned n, m; printf("\nNhap hai vao so nguyen duong : "); scanf("%u%u", &n, &m); printf("\nUSCLN cua %u va %u = %u", n, m, USCLN(n,m)); printf("\nBSCNN cua %u va %u = %u", n, m, BSCNN(n,m)); getch(); } ---------------------------- /* Xem tap tin cho phep cuon */ #include #include void main() { FILE *fp; char filename[50], s[255], *hang[1000], c; int nline = 0, line = 0, i; printf("\nNhap ten tap tin muon xem : "); gets(filename); if ((fp = fopen(filename,"r")) == NULL) printf("\nKhong the mo tap tin %s", filename); else { while (!feof(fp)) { fgets(s, 255, fp); hang[nline] = (char *)malloc(strlen(s)); strcpy(hang[nline], s+1); nline++; } do { clrscr(); i = line; while (i<line+26 && i<nline) printf("%s", hang[i++]); c = getch(); if (c == 0) { c = getch(); switch(c) { case 72 : if (line > 0) line--; break; case 80 : if (line+25 < nline) line++; break; } } } while (c != 27); fclose(fp); } } --------------------------- /* Bai tap 9_5 - Tim so tu trong chuoi */ #include #include #include #pragma warn -pia char *trim(char *chuoi) { char *p; while (p = strstr(chuoi, " ")) memmove(p, p+1, strlen(chuoi) - (p - chuoi)); if (chuoi[0] == ' ') memmove(chuoi, chuoi+1, strlen(chuoi) - 1); if (chuoi[strlen(chuoi)-1] == ' ') chuoi[strlen(chuoi)-1] = 0; return chuoi; } void main() { char chuoi[125]; int i = 0, count = 0; printf("\nNhap chuoi mau : "); textattr(0x1e); gets(chuoi); trim(chuoi); if (strlen(chuoi)) { while(chuoi[i] != 0) if (chuoi[i++] == ' ') count++; count++; } printf("\nSo tu co trong chuoi : %d", count); getch(); } -------------------------------------- -------------------------------- #include void main() { char s[100]; int i; printf("\nNhap vao mot chuoi : "); gets(s); i = 0; while (s[i] != 0) { if (isalpha(s[i])) if (s[i] < 97) s[i] += 32; else s[i] -= 32; i++; } printf("\nChuoi bien thanh : %s", s); getch(); } --------------------------------------- /* Bai tap 5_3 - Tim phuong an doi tien */ #include #define TONGSOTIEN 300000 void main() { long i, j, k, l, m, count=0, soluong = 0; for (i=0; i<=TONGSOTIEN/1000; i++) for (j=0; j<=TONGSOTIEN/2000; j++) for (k=0; k<=TONGSOTIEN/5000; k++) for (l=0; l<=TONGSOTIEN/10000; l++) for (m=0; m<=TONGSOTIEN/20000; m++) { if ((i*1000 + j*2000 + k*5000 + l*10000 + m*20000) == TONGSOTIEN) printf("\n%5ld - %5ld%5ld%5ld%5ld%5ld", ++count, i, j, k, l, m); soluong++; } printf("so luong = %ld", soluong); getch(); } ----------------------------------------- /* Bai tap 9_4 - Tim chuoi doi xung */ void main() { char chuoi[125]; int i = 0, j; printf("\nNhap vao chuoi kiem tra : "); gets(chuoi); j = strlen(chuoi) - 1; while(chuoi[i++] == chuoi[j--]) ; if (--i>=++j) printf("Chuoi doi xung"); else printf("Chuoi khong doi xung"); getch(); } ------------------------------------------ /* Bai tap 1_87 - Demo ve hop dung conio */ #include #include #include char doublebox[] ="ÉÍ»ºÈ¼"; char singlebox[] ="ÚÄ¿³ÀÙ"; void swap(int *x, int *y) { int temp; temp = *y; *y = *x; *x = temp; } void drawbox(int x1, int y1, int x2, int y2, int type) { char *boxtype; int i; if (type == 2) boxtype = doublebox; else boxtype = singlebox; gotoxy(x1,y1); cprintf("%c", boxtype[0]); for (i=x1 + 1; i < x2; i++) cprintf("%c", boxtype[1]); cprintf("%c", boxtype[2]); for (i=y1+1; i<y2; i++) { gotoxy(x1, i); cprintf("%c", boxtype[3]); gotoxy(x2, i); cprintf("%c", boxtype[3]); } gotoxy(x1,y2); cprintf("%c", boxtype[4]); for (i=x1 + 1; i < x2; i++) cprintf("%c", boxtype[1]); cprintf("%c", boxtype[5]); } void drawbox_color(int x1, int y1, int x2, int y2, int type, int color) { if (x1>x2) swap(&x1, &x2); if (y1>y2) swap(&y1, &y2); textattr(color); window(x1, y1, x2, y2); clrscr(); window(1,1,80,25); drawbox(x1, y1, x2, y2, type); } void main() { int x1, y1, x2, y2, type, color; cprintf("\n\rDRAWBOX DEMO.\n\rPress any key to begin..."); getch(); randomize(); while (!kbhit()) { x1 = random(80); do { x2 = random(80); } while (x1 == x2); y1 = random(25); do { y2 = random(25); } while (y1 == y2); type = random(2) + 1; color = random(256); drawbox_color(x1,y1,x2,y2,type,color); delay(100); } } ----------------------------------------- #include void main() { double fahrenheit, celsius; printf("\nNhap gia tri do Fahrenheit : "); scanf("%lf", &fahrenheit); if (fahrenheit != 32.0) { celsius = 5.0 * (fahrenheit-32.0) / 9.0 ; printf("Do Celsius tuong ung = %5.2lf", celsius); } getch(); } ---------------------------------------- /* Bai tap 1_21 - In day so Fibonanci */ #include void main() { int n, i, fib1 = 1, fib2 = 1, fib = 2; printf("\nNhap gia tri N : "); scanf("%d", &n); printf("%d %d ", fib1, fib2); while (fib1+fib2 < n) { fib = fib1 + fib2; printf("%d ", fib); fib2 = fib1; fib1 = fib; } getch(); } ------------------------------------ /* Bai tap 1_68 - Tap hop thong tin */ #include void main() { char s[100], s1[100], s2[100], filename1[50], filename2[50]; FILE *fp1, *fp2; printf("\nCho viet ten tap tin thu nhat : "); gets(filename1); if ((fp1 = fopen(filename1, "w+")) == NULL) printf("\nKhong the tao tap tin %s", filename1); else { printf("\nCho viet ten tap tin thu nhat : "); gets(filename2); if ((fp2 = fopen(filename2, "w+")) == NULL) printf("\nKhong the tao tap tin %s", filename2); else { do { printf("\nHo ten (ENTER de ket thuc) : "); gets(s); if (strlen(s) > 0) { strcat(s, "\n"); fputs(s, fp1); printf("\nTuoi : "); gets(s); strcat(s, "\n"); fputs(s, fp1); printf("\nDia chi : "); gets(s); strcat(s, "\n"); fputs(s, fp1); } } while (strlen(s) > 0); fclose(fp1); fp1 = fopen(filename1, "r"); while (fgets(s, 100, fp1) != NULL) { fgets(s1, 100, fp1); fgets(s2, 100, fp1); s[strlen(s)-1] = 0; s1[strlen(s1)-1] = 0; fprintf(fp2, "%s %s %s", s, s1, s2); } fclose(fp2); } } } ------------------------------------------ /* Bai tap 1_86 - Doc va ghi len file mot mang 2 chieu 10x10 */ #include #define MAX 10 char filename[] = "MANG1010.DAT"; void main() { int mang[MAX][MAX], i, j; FILE *fp; if ((fp = fopen(filename, "w+")) == NULL) printf("\nKhong the tao tap tin %s", filename); else { printf("\nNhap cac phan tu cua mang : "); for (i=0; i<MAX; i++) for (j=0; j<MAX; j++) { printf("\nMang[%d][%d] = ", i, j); scanf("%d", &mang[i][j]); } fwrite(&mang, sizeof(int), MAX * MAX, fp); fclose(fp); printf("\nViec ghi hoan tat."); } getch(); } --------------------------------------- /* Bai tap 3_1 - Ham tinh N! */ #include unsigned long giaithua(int n) { unsigned long ketqua = 1; int i; for (i=2; i<=n; i++) ketqua *= i; return ketqua; } void main() { int n; printf("\nNhap vao gia tri N : "); scanf("%d", &n); printf("%d! = %lu", n, giaithua(n)); getch(); } ---------------------------------- #include void main() { int a[10], i; printf("\nNhap vao 10 phan tu nguyen cua day : "); for (i=0; i<10; i++) scanf("%d", &a[i]); i = 1; while (a[i] <= a[i-1] && i<10) i++; if (i==10) printf("Day da cho la day giam"); else printf("Day da cho khong la day giam"); getch(); } ------------------------------------------------ /* Bai tap 2_6 - Cho biet goc thuoc goc vuong nao */ #include void main() { int goc; printf("\nNhap vao gia tri goc : "); scanf("%d", &goc); printf("Goc do thuoc goc vuong thu %d", (goc/90)%4 + 1); getch(); } ---------------------------------- /* Bai tap - Bai toan Gonbach */ #include #define MAX 100 void main() { unsigned long nguyento[MAX], i, j, l, k=0, n; for (i=1; i<MAX; i++) if (i%2 == 1) { for (j=2; j<i; j++) if (i%j == 0) break; if (j >= i) nguyento[k++] = i; } for (n=0; n<k; n++) for (i=0; i<k; i++) for (j=0; j<k; j++) for (l=0; l<k; l++) if (nguyento[i] + nguyento[j] + nguyento[l] == nguyento[n]) printf("\n%ld = %ld + %ld + %ld", nguyento[n], nguyento[i], nguyento[j], nguyento[l]); getch(); } ----------------------------------------- /* Bai tap 1_20 - Tinh n!! */ #include void main() { int n, start, i; unsigned long gthua = 1; printf("\nNhap gia tri N : "); scanf("%d", &n); if (n%2 == 0) start = 2; else start = 1; for (i=start; i<=n; i = i+2) gthua *= i; printf("\n%d!! = %ld", n, gthua); getch(); } ------------------------------------------- /* Bai tap 2_8 - In so Hex tuong ung voi mot so nguyen duong */ #include void main() { unsigned number; char hex[] = "0123456789ABCDEF"; printf("\nNhap vao mot gia tri nguyen duong 16 bit : "); scanf("%u", &number); printf("Gia tri Hex tuong ung = %c%c%c%c", hex[number/0x1000], hex[(number/0x100)%0x10], hex[(number/0x10)%0x10], hex[number%0x10]); getch(); } -------------------------------------------- #include #include void main(int argc, char *argv[]) { FILE *fp; char s[255]; if (argc <= 1) printf("\nCach su dung : \nHienfile "); else if ((fp = fopen(argv[1], "r")) == NULL) printf("\nKhong the mo tap tin %s", argv[1]); else while (fgets(s, 255, fp)) { s[strlen(s) - 1] = 0; if (strlen(s)) puts(s); } getch(); } ---------------------------------- #include void main(int argc, char *argv[]) { FILE *fp; char c; if (argc <= 1) printf("\nCach su dung : \nHienfile "); else if ((fp = fopen(argv[1], "r")) == NULL) printf("\nKhong the mo tap tin %s", argv[1]); else { while ((c = fgetc(fp)) != EOF) putc(c, stdout); } getch(); } -------------------------------------- /* Bai tap 1_15 - Tinh dien tich cac hinh hoc */ #include #include #include int menu() { int c; printf("\n----- Menu -----"); printf("\n1. Hinh tam giac"); printf("\n2. Hinh tron"); printf("\n3. Hinh vuong"); printf("\n4. Hinh chu nhat"); printf("\n0. Thoat"); printf("\n----------------"); printf("\nChon muc : "); do { c = getch(); } while (c>'4' || c<'0'); putc(c, stdout); return c -'0'; } void hinhtamgiac() { int a, b, c; float p; printf("\n\nNhap vao ba canh tam giac : "); scanf("%d%d%d", &a, &b, &c); printf("\nChu vi tam giac = %d", a+b+c); p = (float)(a + b + c)/2; printf("\nDien tich tam giac = %lf", sqrt(p*(p-a)*(p-b)*(p-c))); getch(); } void hinhtron() { int r; printf("\n\nNhap vao ban kinh hinh tron : "); scanf("%d", &r); printf("\nChu vi hinh tron = %f", 2 * M_PI * r); printf("\nDien tich hinh tron = %f", M_PI * r * r); getch(); } void hinhvuong() { int a; printf("\n\nNhap vao canh hinh vuong : "); scanf("%d", &a); printf("\nChu vi hinh tron = %d", 4 * a); printf("\nDien tich hinh vuong = %d", a * a); getch(); } void hinhchunhat() { int a, b; printf("\n\nNhap vao cac canh hinh chu nhat : "); scanf("%d%d", &a, &b); printf("\nChu vi hinh chu nhat = %d", 2 * (a+b)); printf("\nDien tich hinh chu nhat = %d", b * a); getch(); } void main() { int chon; do { chon = menu(); switch(chon) { case 1 : hinhtamgiac(); break; case 2 : hinhtron(); break; case 3 : hinhvuong(); break; case 4 : hinhchunhat(); break; } } while (chon != 0); } ------------------------------------ /* Bai tap 1_47 - Liet ke tat cac cac hoan vi cua n phan tu */ #include #define MAX 10 int mang[MAX], n; void swap (int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void hoanvi(int k) { int j; if (k==1) { printf("\n"); for (j=0; j<n; j++) printf("%d ", mang[j]); } else for (j=k-1; j>=0; j--) { swap(&mang[k-1], &mang[j]); hoanvi(k-1); swap(&mang[j], &mang[k-1]); } } void main() { int i; printf("\nCho biet so phan tu (N < 10) : "); scanf("%d", &n); for (i=0; i<n; i++) mang[i] = i; hoanvi(n); getch(); } -------------------------------------------- /* Giai he phuong trinh bac nhat */ #include void main() { int a, b, c, d, e, f, dthuc; float x, y; printf("\nNhap vao cac he so a,b,c,d,e,f : "); scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f); dthuc = b*d - e*a; if (dthuc != 0) { y = (float)(c*d-a*f)/dthuc; x = (float)(b*f-c*e)/dthuc; printf("Nghiem x = %f, y = %f", x, y); } else printf("\nHe phuong trinh vo ngiem."); getch(); } --------------------------------------- /* In chuoi theo cac tu, moi tu mot dong */ #include void main() { char s[50]; int i, len; printf("\nNhap vao mot chuoi : "); gets(s); len = strlen(s); i = 0; while (i<len) { while (s[i] == ' ' && i<len) i++; while (s[i] != ' ' && i<len) putc(s[i++], stdout); putc('\n', stdout); } getch(); } ------------------------------------ /* Cho biet ma ban phim */ #include void main() { char c; printf("\nNhap vao mot phim muon biet ma cua no. ESC de thoat\n"); do { c = getch(); if (c == 0) { c = getch(); printf("\nMa mo rong : %d", c); } else printf("\nMa thuong : %d", c); } while (c != 27); } ---------------------------------- /* Bai tap 2_4 - In chu so phan tram, chuc, don vi */ #include void main() { int n, tram, chuc, donvi; printf("\nNhap vao mot so tu 100 - 999 : "); scanf("%d", &n); tram = n; donvi = tram % 10; tram /= 10; chuc = tram % 10; tram /= 10; printf("\nSo hang tram = %d", tram); printf("\nSo hang chuc = %d", chuc); printf("\nSo hang don vi = %d", donvi); getch(); } ----------------------------------- /* Bai tap 1_86 - Tro choi Logic */ #include #include #include #include #include int day[7] = {-1,-1,-1,-1,-1,-1,-1}; int sonhap[4]; int somay[4]; int solanthu; int timthay; void khoitao() { char tieude[] = "L O G I C G A M E"; int j, i, dich; textmode(C40); clrscr(); textcolor(YELLOW); dich = (40 - strlen(tieude)) / 2; for (i=strlen(tieude)-1; i>=0; i--) { movetext(1,3,39,3,2,3); gotoxy(1,3); putch(tieude[i]); delay(40); } for (i=1; i<=dich; i++) { movetext(1,3,39,3,2,3); gotoxy(1,3); putch(' '); delay(40); } randomize(); for (i=0; i<4; i++) { do { j = random(6)+1; } while (day[j] != -1); day[j] = i; somay[i] = j; // printf ("%d",&j); } gotoxy(14,5); cprintf("ÉÍÍÍÍÍÍÑÍÍÍÍÍÍ»"); for (i=0; i<6; i++) { gotoxy(14,5+i*2+1); cprintf("º ³ º"); gotoxy(14,5+i*2+2); cprintf("ÇÄÄÄÄÄÄÅÄÄÄÄÄÄ"); } gotoxy(14,17); cprintf("ÈÍÍÍÍÍÍÏÍÍÍÍÍͼ"); solanthu = 1; timthay = 0; textcolor(WHITE); gotoxy(1, 25); cprintf("Nhap chuoi 4 so khac nhau tu 1 den 6"); } void nhapso() { char c; int i; for (i=0; i<4; i++) sonhap[i]=0; gotoxy(16,5+solanthu*2-1); do { c = getch(); switch(c) { case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : textcolor(c-39); cprintf("%c",c); sonhap[wherex()-16-1] = c - 48; if (wherex()==20) gotoxy(wherex()-1, wherey()); break; case 0 : c = getch(); switch(c) { case 75 : if (wherex()>16) gotoxy(wherex()-1,wherey()); break; case 77 : if (wherex()<19) gotoxy(wherex()+1,wherey()); break; } break; default : sound(880); delay(100); nosound(); } } while (!((c=='\r') && (sonhap[0] != 0) && (sonhap[1] != 0) && (sonhap[2] != 0) && (sonhap[3] != 0))); } void kiemtra() { int diem = 0, i; for (i=0; i<4; i++) if (day[sonhap[i]] != -1) if (day[sonhap[i]] == i) diem += 10; else diem ++; if (diem==40) timthay=1; else { int sotrung, sotrat; sotrung=diem / 10; sotrat = diem % 10; textcolor(GREEN); gotoxy(23,5+solanthu*2-1); for (i=1; i<=sotrung; i++) cprintf("+"); textcolor(WHITE); for (i=1; i<=sotrat; i++) cprintf("-"); solanthu ++; } } void main() { int i; khoitao(); do { nhapso(); kiemtra(); } while ((solanthu <= 6) && (timthay==0)); if (timthay==1) { textcolor(YELLOW+BLINK); gotoxy(1,23); cprintf("Ban da thang."); } else { textcolor(GREEN+BLINK); gotoxy(1,23); cprintf("Ban da thua."); textcolor(YELLOW); } cprintf(" Ket qua dung = "); for (i=0; i<4; i++) { textcolor(somay[i]+9+BLINK); cprintf("%c",somay[i]+48); } getch(); } ------------------------------- /* Ma hoa dung XOR */ #include void main() { char chuoi[50], khoa; int i, len; printf("\nNhap vao chuoi can ma hoa : "); gets(chuoi); printf("Cho biet khoa : "); khoa = getch(); len = strlen(chuoi); for (i=0; i<len; i++) chuoi[i] ^= khoa; printf("\nChuoi da duoc ma hoa thanh : %s", chuoi); printf("\nNhan phim bat ky de giai ma."); getch(); for (i=0; i<len; i++) chuoi[i] ^= khoa; printf("\nChuoi da duoc giai ma : %s", chuoi); getch(); } ------------------------------------ /* Bai 6_2 - Tim phan tu lon nhat va nho nhat trong mang 1 chieu */ #include #include void main() { int mang[20]; int i, minval, maxval; /* Khoi tao mang ngau nhien */ randomize(); for (i=0; i<20; i++) mang[i] = random(100); /* Tim gia tri lon nhat va nho nhat */ minval = maxval = mang[0]; for (i=1; i<20; i++) { if (maxval < mang[i]) maxval = mang[i]; else if (minval > mang[i]) minval = mang[i]; } /* In mang */ clrscr(); for (i=0; i<20; i++) { if (mang[i] == maxval) textcolor(YELLOW); else if (mang[i] == minval) textcolor(RED); else textcolor(WHITE); cprintf("%3d", mang[i]); } getch(); } ---------------------------------- /* Bai tap 1_70 - Ma hoa 1 file bang phep XOR */ #include void main() { char c, filein[50], fileout[50], key; FILE *fpin, *fpout; printf("\nCho biet ten tap tin nguon : "); gets(filein); printf("\nCho biet ten tap tin dich : "); gets(fileout); printf("\nCho biet khoa : "); scanf("%c", &key); if ((fpin = fopen(filein, "r")) == NULL) printf("Khong tim thay tap tin %s", filein); else if ((fpout = fopen(fileout, "w+")) == NULL) { printf("Khong the tao tap tin %s", fileout); fclose(fpin); } else { do { c = fgetc(fpin); if (c != EOF) fputc(c ^ key, fpout); } while (c != EOF); fclose(fpin); fclose(fpout); printf("\nCong viec hoan tat"); } getch(); } -------------------------------------- /* Bai tap 6_1 */ #include #include void main() { int matran[10][10]; int i, j, tonghang[10], tongcot[10], tongdcc=0, count=0; /* Khoi tao mang ngau nhien */ randomize(); for (i=0; i<10; i++) for (j=0; j<10; j++) matran[i][j] = random(10) + 1; /* Tinh tong cac hang */ for (i=0; i<10; i++) { tonghang[i] = 0; for (j=0; j<10; j++) tonghang[i] += matran[i][j]; } /* Tinh tong cac cot */ for (j=0; j<10; j++) { tongcot[j] = 0; for (i=0; i<10; i++) tongcot[i] += matran[i][j]; } /* Tinh tong duong cheo chinh */ for (i=0; i<10; i++) tongdcc += matran[i][i]; /* Dem so luong cac phan tu co gia tri nho hon 6 */ for (i=0; i<10; i++) for (j=0; j<10; j++) if (matran[i][j] < 6) count++; /* In ma tran va cac gia tri cua chung */ clrscr(); for (i=0; i<10; i++) { textcolor(WHITE); for (j=0; j<10; j++) cprintf ("%4d", matran[i][j]); textcolor(YELLOW); cprintf("%4d\n\r", tonghang[i]); } textcolor(LIGHTBLUE); for (j=0; j<10; j++) cprintf("%4d", tongcot[j]); textcolor(LIGHTGREEN); cprintf("%4d", tongdcc); textcolor(WHITE); cprintf("\n\r\n\rTong cac phan tu nho hon 6 = %3d", count); getch(); } ------------------------------------- /* Bai tap 1_99 - Minh hoa phep nhan tay */ #include void nhantay(unsigned long sobn, unsigned long son) { unsigned long k, n; int i=0; char s[20]; printf("\n%20ld\n", sobn); ltoa(son, s, 10); for (i=0; i<18-strlen(s); i++) printf(" "); printf("X %s", s); printf("\n ------------"); n = son; i = 0; while (n>0) { k = n%10; n /= 10; k *= sobn; ltoa(k, s, 10); printf("\n"); gotoxy(21 - strlen(s) - i, wherey()); cprintf("%s", s); i++; } ltoa(sobn*son, s, 10); printf("\n ------------\n"); gotoxy(21 - strlen(s), wherey()); cprintf("%s", s); } void main() { unsigned long sobinhan, sonhan; printf("\Nhap so bi nhan : "); scanf("%ld", &sobinhan); printf("\Nhap so nhan : "); scanf("%ld", &sonhan); nhantay(sobinhan, sonhan); getch(); } -------------------------------- #include void main() { FILE *fp1, *fp2, *fpout; char sf1[50], sf2[50], sfout[50]; int c; printf("\nNhap ten tap tin thu nhat : "); scanf("%s", &sf1); printf("\nNhap ten tap tin thu hai : "); scanf("%s", &sf2); printf("\nNhap ten tap tin ket qua : "); scanf("%s", &sfout); if ((fp1 = fopen(sf1, "r")) == NULL) fprintf(stderr, "Khong the mo tap tin %s\n", sf1); if ((fp2 = fopen(sf2, "r")) == NULL) fprintf(stderr, "Khong the mo tap tin %s\n", sf2); if ((fpout = fopen(sfout, "w")) == NULL) fprintf(stderr, "Khong the mo tap tin %s\n", sfout); while ((c = getc(fp1)) != EOF) putc(c, fpout); while ((c = getc(fp2)) != EOF) putc(c, fpout); fclose(fp1); fclose(fp2); fclose(fpout); printf("\nHoan tat! Nhan phim bat ky de ket thuc."); getch(); } ------------------------------------------- #include #include #define N 15 void main() { int i, j, tgpascal[N], tmp[N]; clrscr(); tgpascal[0] = 1; for (i=1; i<N; i++) tgpascal[i] = 0; for (i=0; i<N; i++) { for (j=0; j<=i; j++) tmp[j] = tgpascal[j]; printf("\n%5d", tmp[0]); for (j=1; j<=i; j++) { tmp[j] += tgpascal[j-1]; printf("%5d", tmp[j]); } for (j=0; j<=i; j++) tgpascal[j] = tmp[j]; } getch(); } -------------------------------------- /* Phan so */ #include int USCLN(int a, int b) { a = abs(a); b = abs(b); while (a != 0 && b != 0) if (a > b) a -= b; else b -= a; if (a == 0) return b; else return a; } int BSCNN(int a, int b) { return a * b / USCLN(a, b); } typedef struct tagphanso { int tuso, mauso; } PHANSO; PHANSO uocluoc(PHANSO a) { PHANSO c; c.tuso = a.tuso / USCLN(a.tuso, a.mauso); c.mauso = a.mauso / USCLN(a.tuso, a.mauso); return c; } PHANSO cong(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.mauso + a.mauso * b.tuso; c.mauso = a.mauso * b.mauso; c = uocluoc(c); return c; } PHANSO tru(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.mauso - a.mauso * b.tuso; c.mauso = a.mauso * b.mauso; c = uocluoc(c); return c; } PHANSO nhan(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.tuso; c.mauso = a.mauso * b.mauso; c = uocluoc(c); return c; } PHANSO chia(PHANSO a, PHANSO b) { PHANSO c; c.tuso = a.tuso * b.mauso; c.mauso = a.mauso * b.tuso; c = uocluoc(c); return c; }

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

  • doccac_bai_tap_c_can_ban.doc