Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

BT C can ban

/* Bai tap 9_1 - Dem so lan xuat hien cac ky tu trong chuoi */

#include <stdio.h>

#include <ctype.h>

void main()

{

char chuoi[80];

int i = 0, count = 0;

printf("

Nhap vao mot chuoi bat ky : ");

gets(chuoi);

while (chuoi[i] != 0)

{

if (isalpha(chuoi[i++]))

count++;

}

printf("So ky tu trong chuoi = %d", count);

getch();

}

#include <stdio.h>

void main()

{

int num, tram, chuc, donvi, count = 0;

for (num = 100; num < 1000; num++)

{

donvi = num % 10;

chuc = (num / 10) % 10;

tram = num / 100;

if (donvi * donvi * donvi + chuc * chuc * chuc + tram * tram * tram == num)

{

printf("

So %d thoa man", num);

count++;

}

}

printf("

Tong cong co %d so thoa man yeu cau", count);

}

/* Bai tap - Bai toan Ancarokhi */

#include <stdio.h>

void main()

{

int dai, rong;

printf("

Bai toan Ancarokhi : Tim dien tich hinh chu nhat co chieu dai gap hai");

printf("

chieu rong va dien tich = chu vi");

for (dai = 1; dai < 100; dai ++)

for (rong=1; rong < 100; rong++)

if (dai == 2 * rong && (dai + rong)*2 == dai*rong)

printf("

Dai = %d; Rong = %d", dai, rong);

getch();

}

/* Bai tap - CHung minh dang thuc An Casi*/

#include <stdio.h>

unsigned long vetrai(unsigned long n)

{

unsigned long tmp=0, i;

for (i=1; i<=n; i++)

tmp += i*i*i*i;

return tmp;

}

unsigned long vephai(unsigned long n)

{

unsigned long tmp;

tmp = (long)6*n*n*n*n*n + (long)15*n*n*n*n + (long)10*n*n*n - n;

tmp = tmp / (long)30 ;

return tmp;

}

void main()

{

unsigned long tong1, tong2, n;

for (n=1; n<=50; n++)

{

tong1 = vetrai(n);

tong2 = vephai(n);

if (tong1 == tong2)

{

printf("

So %d thoa man dang thuc An Casi. ", n);

printf("Tong1 = %ld - Tong2 = %ld", tong1, tong2);

}

else

{

printf("

So %d khong thoa man dang thuc An Casi. ", n);

printf("Tong1 = %ld - Tong2 = %ld", tong1, tong2);

}

}

getch();

}

/* Bai tap 5_4 - Bang ma ASCII */

#include <stdio.h>

#include <conio.h>

void main()

{

int i, j;

clrscr();

printf(" ");

for (j=0; j<16; j++)

printf("%3d", j);

for (i=2; i<16; i++)

for (j=0; j<16; j++)

{

if (j == 0)

printf("

%2d ", i);

printf(" %c", i*16+j);

}

getch();

}

/* Bai tap 4_1 - Giai phuong trinh bac hai AXư + BX + C = 0 */

#include <stdio.h>

#include <math.h>

void main()

{

float a, b, c, delta;

printf("

Giai phuong trinh bac hai AXư + BX + C = 0");

printf("

Cho biet ba he so A B C : ");

scanf("%f%f%f", &a, &b, &c);

delta = b * b - 4 * a * c;

if (delta<0)

printf("Phuong trinh vo nghiem");

else if (delta == 0)

printf("Phuong trinh co nghiem kep x1 = x2 = %f", -b/(2*a));

else

{

printf("Phuong trinh co hai nghiem phan biet

x1 = %f", (-b + sqrt(delta))/(2*a));

printf("

x2 = %f", (-b - sqrt(delta))/(2*a));

}

getch();

}

/* Bai tap 4_1 - Giai phuong trinh bac nhat AX + B = 0 */

#include <stdio.h>

void main()

{

float a, b;

printf("

Giai phuong trinh bac nhat AX + B = 0");

printf("

Cho biet ba he so A B : ");

scanf("%f%f", &a, &b);

if (a==0)

if (b!=0)

printf("Phuong trinh vo nghiem");

else

printf("Phuong trinh co nghiem khong xac dinh");

else

printf("Dap so cua phuong trinh tren = %f", -b/a);

getch();

}

/* Bai tap 1_42 - Chuong trinh ban tau */

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#define MAXCOL 26

#define MAXROW 22

void drawboard()

{

int i, j;

textattr(0x1E);

clrscr();

gotoxy(4, 1);

for (i=0; i<MAXCOL; i++)

cprintf("%c ", 'A'+ i);

gotoxy(2, 2);

cprintf("%c",'É');

for (i=0; i<MAXCOL; i++)

cprintf("%s", "ÍÑ");

cprintf("%s", "Í»");

for (i=0; i<MAXROW; i++)

{

gotoxy(1, i+3);

cprintf("%c%s",'A'+i, "Ç");

for (j=0; j<MAXCOL; j++)

cprintf("%s", "ÄÅ");

cprintf("%s", "Ķ");

}

gotoxy(2, 3+MAXROW);

cprintf("%c",'È');

for (i=0; i<MAXCOL; i++)

cprintf("%s", "ÍÏ");

cprintf("%s", "ͼ");

}

char nhaptoado(char min, char max)

{

char c;

do {

c = toupper(getch());

} while (c < min || c > max);

putc(c, stdout);

return c;

}

void main()

{

char x, y, xplayer, yplayer, xcomp, ycomp, xc, yc;

int done = 0, luot = 1, i, j, oldx, oldy;

int board[MAXROW][MAXCOL];

printf("

Nhap vao toa do X cua tau ban (A-Z): ");

x = nhaptoado('A', 'V');

printf("

Nhap vao toa do Y cua tau ban (A-V): ");

y = nhaptoado('A', 'Z');

drawboard();

window((MAXCOL+3) * 2, 1, 80, 25);

textattr(0x0E);

clrscr();

cprintf("Quy tac choi : ");

cprintf("

\rMuon ban o nao, ta nhaptoa do hang roi cot. Vidu AZ");

cprintf(" de ban o o hang Acot Z.

\rMoi di truoc :");

randomize();

do {

xc = random(MAXROW)+'A';

} while (xc == x);

yc = random(MAXCOL)+'A';

for (i=0; i<MAXROW; i++)

for (j=0; j<MAXCOL; j++)

board[i][j] = 0;

do {

cprintf("

\r%d - ", luot);

xplayer = nhaptoado('A','V');

yplayer = nhaptoado('A','Z');

oldx = wherex();

oldy = wherey();

window(1,1,80,25);

gotoxy((yplayer-'A'+2) * 2, xplayer - 'A' + 3);

cprintf("%c", 4);

window((MAXCOL+3) * 2, 1, 80, 25);

gotoxy(oldx, oldy);

if (xplayer == xc && yplayer == yc)

{

textattr(2);

cprintf("

\rOi chet toi roi!");

done = 1;

}

else

{

do {

xcomp = random(MAXROW);

ycomp = random(MAXCOL);

} while (board[xcomp][ycomp] == 1);

cprintf(" - %c%c", xcomp+'A', ycomp+'A');

if (xcomp+'A' == x && ycomp+'A' == y)

{

textattr(3);

cprintf("

\rBan da chet!");

done = 1;

}

board[xcomp][ycomp] = 1;

}

luot++;

} while (!done);

cprintf("

\rNhan phim de ket thuc");

getch();

window(1, 1, 80, 25);

}

/* Bai tap 1_91 - The hien cac gia tri bit cua so */

#include <stdio.h>

void main()

{

unsigned int mang[24], i;

int bit[16], k, index;

printf("

Nhap vao 23 gia tri nguyen : ");

for (i=0; i<23; i++)

scanf("%d",&mang[i]);

printf(" FEDCBA9876543210");

for (i=0; i<23; i++)

{

k = mang[i];

for (index = 0; index < 16; index++)

bit[index] = 0;

index = 15;

while (k)

{

bit[index--] = k%2;

k /= 2;

}

printf("

%5d ",mang[i]);

for (index=0; index<16; index++)

if (bit[index] == 1)

printf("*");

else

printf("-");

}

getch();

}

/* Bai tap 2_7 - In ra nam am lich tuong ung voi nam nhap vao */

#include <stdio.h>

void main()

{

unsigned nam;

char can[][5] = {"Giap", "At", "Binh", "Dinh", "Mau", "Ky",

"Canh", "Tan", "Nham", "Quy"};

char chi[][5] = {"Ty", "Suu", "Dan", "Meo", "Thin", "Ty",

"Ngo", "Mao", "Than", "Dau", "Tuat", "Hoi"};

printf("

Nhap nam can biet : ");

scanf("%d", &nam);

printf("Nam am lich cua %d la %s %s", nam, can[(nam+6)%10], chi[(nam+8)%12]);

getch();

}

/* Bai tap 1_31 - Tinh can bac hai theo phep lap Newton */

#include <stdio.h>

#include <math.h>

void main()

{

double a, xn, ketqua;

printf("

Nhap vao so muon tinh can bac hai : ");

scanf("%lf", &a);

xn = (a+1)/2;

do {

ketqua = xn;

xn = 0.5 * (xn + a/xn);

} while (fabs(xn-ketqua) > 0.0001);

printf("

Ket qua = %lf", xn);

getch();

}

/* Bai tap 5_1 - In bang cuu chuong */

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

{

int i, j;

char chuoi[] = "B A N G C U U C H U O N G";

char ten[10][5] = {"","","Hai", "Ba", "Bon", "Nam",

"Sau", "Bay", "Tam", "Chin"};

clrscr();

textcolor(YELLOW);

gotoxy((80 - strlen(chuoi)) / 2, 1);

cprintf("%s

", chuoi);

for (i=2; i<=9; i++)

{

gotoxy(10*(i-2) + (10 - strlen(ten[i]))/2, 4);

textcolor(i);

cprintf("%s", ten[i]);

}

for (j=1; j<=10; j++)

for (i=2; i<=9; i++)

{

gotoxy(10*(i-2) + 1, j+4);

textcolor(i);

cprintf("%dx%2d = %2d", i, j, i*j);

}

getch();

}

/* Bai tap 2_2 - Nhap chuoi va in chuoi */

#include <stdio.h>

void main()

{

char name[80];

printf("

Xin cho biet ten cua ban : ");

gets(name);

printf("Chao %s

", name);

getch();

}

/* Cau truc va cac ham thao tac len so phuc */

#include <math.h>

typedef struct tagcomplex {

float thuc, ao;

} complex;

complex tong(complex a, complex b)

{

complex c;

c.thuc = a.thuc + b.thuc;

c.ao = a.ao + b.ao;

return c;

}

complex hieu(complex a, complex b)

{

complex c;

c.thuc = a.thuc - b.thuc;

c.ao = a.ao - b.ao;

return c;

}

complex tich(complex a, complex b)

{

complex c;

c.thuc = a.thuc*b.thuc - a.ao*b.ao;

c.ao = a.thuc*b.ao + a.ao*b.thuc;

return c;

}

complex thuong(complex a, complex b)

{

complex c;

float tongbp;

tongbp = b.thuc*b.thuc + b.ao*b.ao;

c.thuc = (a.thuc*a.ao + b.thuc*b.ao)/tongbp;

c.ao = (a.ao*b.thuc - a.thuc*b.ao)/tongbp;

return c;

}

float argument(complex a)

{

return acos(a.thuc/sqrt(a.thuc*a.thuc + a.ao*a.ao));

}

float modul(complex a)

{

return sqrt(a.thuc*a.thuc + a.ao*a.ao);

}

void print_complex(complex a)

{

printf("%.2f + %.2fi", a.thuc, a.ao);

}

void main()

{

complex a, b, c;

printf("

Nhap he so thuc va phuc cua A : ");

scanf("%f%f", &a.thuc, &a.ao);

printf("

Nhap he so thuc va phuc cua B : ");

scanf("%f%f", &b.thuc, &b.ao);

printf("

So phuc A = ");

print_complex(a);

printf("

So phuc B = ");

print_complex(b);

printf("

Tong cua chung = ");

c = tong(a, b);

print_complex(c);

printf("

Hieu cua chung = ");

c = hieu(a, b);

print_complex(c);

printf("

Tich cua chung = ");

c = tich(a, b);

print_complex(c);

printf("

Thuong cua chung = ");

c = thuong(a, b);

print_complex(c);

printf("

Argument cua a = %f", argument(a));

printf("

Modul cua a = %f", modul(a));

getch();

}

/* Bai tap 1_59 - Tim so dong, so tu, so ky tu nhap tu ban phim */

#include <stdio.h>

void main()

{

int sokytu=0, dautu = 0, sotu=0, sodong=1;

char c;

puts("

");

do {

c = getchar();

if (c != '

' && c != EOF)

sokytu++;

if (isalnum(c) && dautu == 0)

{

sotu++;

dautu=1;

}

if (!isalnum(c) && dautu == 1)

dautu = 0;

if (c == '

')

sodong++;

} while (c != EOF);

printf("

So dong : %d", sodong);

printf("

So tu : %d", sotu);

printf("

So ky tu : %d", sokytu);

getch();

}

/* Ma hoa thong diep */

#include <stdio.h>

#include <ctype.h>

char *crypt(char *tdiep, int column)

{

char tam[255], *result;

int i = 0, k = 0, n, j=0;

while(tdiep[i] != 0)

{

if (isalnum(tdiep[i]))

tam[k++] = tdiep[i];

i++;

}

tam[k] = 0;

result = (char *)malloc(k+1);

for (i=0; i<column; i++)

{

n = 0;

while(n+i < k)

{

result[j++] = tolower(tam[n+i]);

n += column;

}

}

result[k] = 0;

return result;

}

void main()

{

char thongdiep[255], *mahoa;

int col;

printf("

Nhap thong diep can ma hoa : ");

gets(thongdiep);

printf("

Cho biet so cot : ");

scanf("%d", &col);

mahoa = crypt(thongdiep, col);

printf("

Thong diep da duoc ma hoa thanh : %s", mahoa);

getch();

}

/* Cuon dong chu ngang man hinh */

#include <stdio.h>

void main()

{

char qcao[81], i=0, len;

textattr(0x1e);

printf("

Nhap vao dong quang cao : ");

gets(qcao);

len = strlen(qcao);

while (!kbhit())

{

movetext(2, 1, 80, 1, 1, 1);

gotoxy(80, 1);

cprintf("%c", qcao[i++]);

delay(100);

i %= len;

}

getch();

}

/* Bai tap 9_5 - Tim so tu trong chuoi */

#include <stdio.h>

#include <string.h>

#include <conio.h>

#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("

Nhap chuoi mau : ");

textattr(0x1e);

gets(chuoi);

trim(chuoi);

if (strlen(chuoi))

{

while(chuoi[i] != 0)

if (chuoi[i++] == ' ')

count++;

count++;

}

printf("

So tu co trong chuoi : %d", count);

getch();

}

/* Xem tap tin cho phep cuon */

#include <stdio.h>

#include <conio.h>

void main()

{

FILE *fp;

char filename[50], s[255], *hang[1000], c;

int nline = 0, line = 0, i;

printf("

Nhap ten tap tin muon xem : ");

gets(filename);

if ((fp = fopen(filename,"r")) == NULL)

printf("

Khong 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);

}

}

#include <stdio.h>

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("

Nhap hai vao so nguyen duong : ");

scanf("%u%u", &n, &m);

printf("

USCLN cua %u va %u = %u", n, m, USCLN(n,m));

printf("

BSCNN cua %u va %u = %u", n, m, BSCNN(n,m));

getch();

}

/* Bai tap 1_93 - In tat ca cac uoc so cua so n */

#include <stdio.h>

#include <conio.h>

void main()

{

int n, i;

printf("Cho gia tri N = ");

scanf("%d", &n);

printf("Cac uoc so cua %d la :

", n);

for (i=1; i<n; i++)

if ((n % i) == 0)

printf("%5d", i);

getch();

}

/* Tron hai mang tang dan thanh 1 mang tang dan */

#include <stdio.h>

#define MAX 10

void main()

{

int a[MAX], b[MAX], c[2*MAX], n1, n2, i, i1, i2;

printf("

Cho biet so phan tu cua mang thu nhat : ");

scanf("%d", &n1);

printf("Nhap vao cac phan tu (giam dan) cua mang thu nhat : ");

for (i=0; i<n1; i++)

scanf("%d", &a[i]);

printf("

Cho biet so phan tu cua mang thu hai : ");

scanf("%d", &n2);

printf("Nhap vao cac phan tu (giam dan) cua mang thu hai : ");

for (i=0; i<n2; i++)

scanf("%d", &b[i]);

i1 = n1-1;

i2 = n2-1;

for (i=0; i<n1 + n2; i++)

{

if (i1 < 0 || i2 < 0)

break;

if (a[i1] < b[i2])

{

c[i] = a[i1];

i1--;

}

else

{

c[i] = b[i2];

i2--;

}

}

if (i1 >= 0)

while (i1 >= 0)

c[i++] = a[i1--];

if (i2 >= 0)

while (i2 >= 0)

c[i++] = b[i2--];

printf("

Cac phan tu cua mang tron : ");

for (i=0; i<n1+n2; i++)

printf("%d ", c[i]);

getch();

}

/* Tron hai mang tang dan thanh 1 mang tang dan */

#include <stdio.h>

#define MAX 10

void main()

{

int a[MAX], b[MAX], c[2*MAX], n1, n2, i, i1, i2;

printf("

Cho biet so phan tu cua mang thu nhat : ");

scanf("%d", &n1);

printf("Nhap vao cac phan tu (tang dan) cua mang thu nhat : ");

for (i=0; i<n1; i++)

scanf("%d", &a[i]);

printf("

Cho biet so phan tu cua mang thu hai : ");

scanf("%d", &n2);

printf("Nhap vao cac phan tu (tang dan) cua mang thu hai : ");

for (i=0; i<n2; i++)

scanf("%d", &b[i]);

i1 = i2 = 0;

for (i=0; i<n1 + n2; i++)

{

if (i1 >= n1 || i2 >= n2)

break;

if (a[i1] < b[i2])

{

c[i] = a[i1];

i1++;

}

else

{

c[i] = b[i2];

i2++;

}

}

if (i1 < n1)

while (i1 < n1)

c[i++] = a[i1++];

if (i2 < n2)

while (i2 < n2)

c[i++] = b[i2++];

printf("

Cac phan tu cua mang tron : ");

for (i=0; i<n1+n2; i++)

printf("%d ", c[i]);

getch();

}

/* Bai tap 9_3 - Viet thu tuc trim de loai bo cac khoang trong thua trong chuoi */

#include <stdio.h>

#include <string.h>

#include <conio.h>

#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];

printf("

Nhap chuoi mau : ");

textattr(0x1e);

gets(chuoi);

trim(chuoi);

printf("

Chuoi sau khi da trim : ");

textattr(0x1e);

cputs(chuoi);

getch();

}

/* Bai tap 1_94 - Bai toan tram trau */

/* Giai bai toan co :

Tram trau tram co

Trau dung an nam

Trau nam an ba

Trau gia an mot

*/

#include <stdio.h>

#include <conio.h>

void main()

{

int tdung, tnam, tgia, phuongan=0;

for (tdung = 1; tdung <= 98; tdung ++)

for (tnam = 1; tnam < 99 - tdung; tnam ++)

for (tgia = 1; tgia < 99 - (tdung + tnam); tgia++)

if ((tdung*5 + tnam*3 + tgia) == 100)

{

printf("

Trau dung : %5d ; Trau nam : %5d ; Trau gia : %5d",

tdung, tnam, tgia);

phuongan++;

}

printf("

Tong cong co %d phuong an.", phuongan);

getch();

}

/* Bai tap 2_3 - Tinh tong binh phuong hai so nguyen */

#include <stdio.h>

void main()

{

int a, b;

printf("

Nhap hai so nguyen : ");

scanf("%d%d", &a, &b);

printf("Tong %dư + %dư = %d", a, b, a * a + b * b);

getch();

}

/* Tinh tong ma tran */

#include <stdio.h>

#define MAX 10

void in_ma_tran(int A[MAX][MAX], int n, int m, char id)

{

int i, j;

printf("

Ma tran %c : ", id);

for (i=0; i<n; i++)

{

printf("

");

for(j=0; j<m; j++)

printf("%3d ",A[i][j]);

}

}

void nhap_so_nguyen(int *value, int min, int max, char id)

{

do {

printf("

Nhap gia tri %c (trong khoang %d den %d)", id, min, max);

scanf("%d", value);

} while (*value < min || *value > max);

}

void main()

{

int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX], n, m, i, j;

nhap_so_nguyen(&n, 2, MAX, 'n');

nhap_so_nguyen(&m, 2, MAX, 'm');

printf("

Nhap ma tran A : ");

for (i=0; i<n; i++)

for (j=0; j<m; j++)

{

printf("A[%d,%d] = ");

scanf("%d", &(A[i][j]));

}

printf("

Nhap ma tran B : ");

for (i=0; i<n; i++)

for (j=0; j<m; j++)

{

printf("B[%d,%d] = ");

scanf("%d", &(B[i][j]));

}

in_ma_tran(A, n, m, 'A');

in_ma_tran(B, n, m, 'B');

for (i=0; i<n; i++)

for (j=0; j<m; j++)

C[i][j] = A[i][j] + B[i][j];

in_ma_tran(C, n, m, 'C');

getch();

}

/* Bai tap 3_2 - Ham tinh to hop chap K cua N */

#include <stdio.h>

unsigned long giaithua(int n)

{

unsigned long ketqua = 1;

for (i=2; i<=n; i++)

ketqua *= i;

return ketqua;

}

unsigned long to_hop_chap(int k, int n)

{

return giaithua(n) / (giaithua(k) * giaithua(n-k));

}

void main()

{

int n, k;

printf("

Nhap vao gia tri N va K : ");

scanf("%d%d", &n, &k);

printf("Top hop chap %d cua %d = %lu", k, n, to_hop_chap(k, n));

getch();

}

/* Bai tap 1_48 - Chuong trinh trac nghiem don gian */

#include <stdio.h>

#include <conio.h>

#include <string.h>

#define MAX 100

typedef struct tagcauhoi {

char *cauhoi, *traloi[4];

int caudung;

} CAUHOI;

CAUHOI cautn[MAX];

int socau = 0;

int socaudung = 0;

char nhapcautraloi()

{

char c;

do {

c = toupper(getch());

} while (c <'A' || c>'D');

textattr(2);

cprintf("%c", c);

return c - 'A';

}

void tracnghiem()

{

int cauhientai= 0, i;

char c;

do {

clrscr();

textattr(1);

cprintf("%s

\r", cautn[cauhientai].cauhoi);

textattr(14);

for (i=0; i<4; i++)

cprintf("\r%c. %s", i+'A', cautn[cauhientai].traloi[i]);

textattr(15);

cprintf("

\rChon cau : ");

c = nhapcautraloi();

if (c == cautn[cauhientai].caudung)

{

cprintf("

\rDung roi!");

delay(500);

socaudung ++;

}

cauhientai++;

} while (cauhientai<socau);

}

void main()

{

FILE *fp;

int i, j;

char tmp[255];

if ((fp = fopen("TNGHIEM.DAT", "r")) == NULL)

printf("

Khong tim thay tap tin TNGHIEM.DAT");

else

{

fscanf(fp, "%d%*c", &socau);

for (i=0; i<socau; i++)

{

fgets(tmp, 255, fp);

cautn[i].cauhoi = strdup(tmp);

for (j=0; j<4; j++)

{

fgets(tmp, 255, fp);

cautn[i].traloi[j] = strdup(tmp);

}

fscanf(fp, "%d%*c", &cautn[i].caudung);

}

fclose(fp);

tracnghiem();

textattr(14);

cprintf("

\rKet qua : ");

textattr(2);

cprintf("%.2f", (float)socaudung*10 / socau);

}

getch();

}

/* Bai tap 1_29 - Tinh so PI voi sai so 0.0001 */

#include <stdio.h>

#include <math.h>

void main()

{

double PI = 1.0, n = 3.0;

int i = 0;

while (1.0/n > 0.0001)

{

if (i%2 == 0)

PI -= 1.0/n;

else

PI += 1.0/n;

n += 2;

i++;

}

PI *= 4.0;

printf("

So PI tinh toan duoc la : %lf", PI);

printf("

So M_PI trong C = %lf", M_PI);

getch();

}

/* Tong hai so chi gio */

void main()

{

int gio1, phut1, giay1, gio2, phut2, giay2, gio3 = 0, phut3 = 0, giay3;

printf("

Nhap vao gia tri chi gio thu nhat (hh:mm:gg): ");

scanf("%d:%d:%d", &gio1, &phut1, &giay1);

printf("

Nhap vao gia tri chi gio thu hai (hh:mm:gg): ");

scanf("%d:%d:%d", &gio2, &phut2, &giay2);

giay3 = giay1 + giay2;

if (giay3 >=60)

{

giay3 %=60;

phut3 = 1;

}

phut3 += phut1 + phut2;

if (phut3 >=60)

{

phut3 %=60;

gio3 = 1;

}

gio3 += gio1 + gio2;

gio3 %= 24;

printf("

Tong hai gia tri gio tren = %2d:%2d:%2d", gio3, phut3, giay3);

getch();

}

/* Tinh da thuc bac N */

#define MAX 100

float tinhdathuc(float heso[], float x, int n)

{

float tong = 0.0;

for (i=0; i<n; i++)

{

tong = (tong + heso[i]) * x;

}

tong += heso[n];

return tong;

}

void main()

{

float heso[MAX], x;

int n, i;

printf("

Cho biet so bac : ");

scanf("%d", &n);

printf("

Nhap vao cac he so :");

for (i=0; i<=n; i++)

scanf("%f", &heso[i]);

printf("

Nhap vao gia tri X : ");

scanf("%f", &x);

printf("

Ket qua tinh = %f", tinhdathuc(heso, x, n));

getch();

}

/* Tinh tich ma tran */

#include <stdio.h>

#define MAX 10

void in_ma_tran(int A[MAX][MAX], int n, int m, char id)

{

int i, j;

printf("

Ma tran %c : ", id);

for (i=0; i<n; i++)

{

printf("

");

for(j=0; j<m; j++)

printf("%3d ",A[i][j]);

}

}

void nhap_so_nguyen(int *value, int min, int max, char id)

{

do {

printf("

Nhap gia tri %c (trong khoang %d den %d)", id, min, max);

scanf("%d", value);

} while (*value < min || *value > max);

}

void main()

{

int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX], n, m, l, i, j, k;

nhap_so_nguyen(&n, 2, MAX, 'n');

nhap_so_nguyen(&m, 2, MAX, 'm');

nhap_so_nguyen(&l, 2, MAX, 'l');

printf("

Nhap ma tran A : ");

for (i=0; i<n; i++)

for (j=0; j<m; j++)

{

printf("A[%d,%d] = ", i, j);

scanf("%d", &(A[i][j]));

}

printf("

Nhap ma tran B : ");

for (i=0; i<m; i++)

for (j=0; j<l; j++)

{

printf("B[%d,%d] = ", i, j);

scanf("%d", &(B[i][j]));

}

in_ma_tran(A, n, m, 'A');

in_ma_tran(B, n, m, 'B');

for (i=0; i<n; i++)

for (j=0; j<l; j++)

{

C[i][j] = 0;

for (k=0; k<m; k++)

C[i][j] += A[i][k] * B[k][j];

}

in_ma_tran(C, n, m, 'C');

getch();

}

/* Bai tap 1_67 - Tim so dong, so tu, so ky tu nhap tu ban phim */

#include <stdio.h>

void main()

{

int sokytu=0, dautu = 0, sotu=0, sodong=1;

char c, filename[50];

FILE *fp;

printf("

Cho biet ten tap tin muon xem : ");

gets(filename);

if ((fp = fopen(filename, "r")) == NULL)

printf("Khong tim thay tap tin %s", filename);

else

{

do {

putc(c = fgetc(fp), stdout);

if (c != '

' && c != EOF)

sokytu++;

if (isalnum(c) && dautu == 0)

{

sotu++;

dautu=1;

}

if (!isalnum(c) && dautu == 1)

dautu = 0;

if (c == '

')

sodong++;

} while (c != EOF);

fclose(fp);

printf("

So dong : %d", sodong);

printf("

So tu : %d", sotu);

printf("

So ky tu : %d", sokytu);

}

getch();

}

/* Bai tap 1_92 - In hinh tam giac dac */

#include <stdio.h>

#include <conio.h>

void main()

{

int n, i, j;

printf("

Nhap chieu cao cua hinh tam giac : ");

scanf("%d", &n);

for (i=0; i<n; i++)

{

for (j=n-1; j>i; j--)

printf(" ");

for (j=0; j<i*2+1; j++)

printf("*");

printf("

");

}

getch();

}

/* Bai tap 4_3 - Chuong trinh tinh cuoc TAXI */

#include <stdio.h>

#include <math.h>

void main()

{

unsigned long sotien;

float sokm;

printf("

Ban hay cho biet so km da di duoc : ");

scanf("%f", &sokm);

if (sokm <= 1.0)

sotien = 5000;

else if (sokm <= 30.0)

sotien = 5000 + floor((sokm - 1.0) * 5.0) * 1000;

else

sotien = 150000 + (ceil(sokm) - 30) * 3000;

printf("

So tien can tra = %lu", sotien);

getch();

}

#include <stdio.h>

void main()

{

int a[10], i;

printf("

Nhap 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 tang");

else

printf("Day da cho khong la day tang");

getch();

}

/* Nhap co sap xep */

#include <stdio.h>

#include <mem.h>

#define MAX 100

void main()

{

int day[MAX], n, i, j, tam;

printf("

Cho biet so phan tu cua day : ");

scanf("%d", &n);

printf("Nhap vao cac phan tu : ");

for (i=0; i<n; i++)

{

scanf("%d", &tam);

j = 0;

while (j<i && day[j]<tam)

j++;

if (j<i)

memmove(&day[j+1], &day[j], (i-j)*sizeof(int));

day[j] = tam;

}

printf("

Sau khi nhap : ");

for (i=0; i<n; i++)

printf("%d ", day[i]);

getch();

}

#include <stdio.h>

#define MAX 100

void main()

{

int day[MAX], i, n, j, tam;

printf("

Cho biet so phan tu cua day : ");

scanf("%d", &n);

printf("Nhap vao cac phan tu : ");

for (i=0; i<n; i++)

scanf("%d", &day[i]);

for (i=0; i<n-1; i++)

for (j=i+1; j<n; j++)

if (day[j] < day[i])

{

tam = day[i];

day[i] = day[j];

day[j] = tam;

}

printf("

Sau khi sap xep : ");

for (i=0; i<n; i++)

printf("%d ", day[i]);

getch();

}

/* Bai tap 9_2 - Thu tuc tim chuoi con trong mot chuoi */

#include <stdio.h>

void main()

{

char chuoi_lon [125], chuoi_con [10];

int i, j;

printf("

Nhap vao chuoi lon : ");

gets(chuoi_lon);

printf("

Nhap vao chuoi can tim : ");

gets(chuoi_con);

i = 0;

while (chuoi_lon[i] != 0)

{

j = 0;

while (chuoi_lon[i++] == chuoi_con[j++] &&

chuoi_lon[i-1] != 0 && chuoi_con[j-1] != 0)

if (chuoi_lon[i-1] != 0 && chuoi_con[j-1] == 0)

printf("

Tim thay tai vi tri %d", i-j);

}

getch();

}

/* Bai tap 1_95 - Tim va thay the mot chuoi con */

/*

Tim kiem va thay the chuoi con trong mot chuoi lon

s : chuoi lon

s1 : chuoi con

s2 : chuoi se thay the

De chac chan khong bi loi thi chuoi s phai co kha nang chua du

*/

#include <stdio.h>

#include <string.h>

char *str_str(char *s, char *s1, char *s2)

{

int len = strlen(s);

int len1 = strlen(s1);

int len2 = strlen(s2);

int i=0, j, luu;

if (len1!=0)

while (i<len)

{

if (s[i] == s1[0])

{

j = 0;

luu = i;

while ((s[luu++] == s1[j++]) && (j < len1)) ;

if (j==len1)

{

memmove(&s[i+len2], &s[i+len1], len - i - len1+1);

memcpy(&s[i],s2,len2);

len = len + len2 - len1;

i += len2;

}

else i ++;

}

else i++;

}

return s;

}

void main()

{

char s[255], s1[20], s2[20];

printf("Nhap vao chuoi lon : "); gets(s);

printf("Nhap vao chuoi tim : "); gets(s1);

printf("Nhap vao chuoi thay the : "); gets(s2);

str_str(s, s1, s2);

printf("Chuoi sau khi tim va thay the la : %s", s);

getch();

}

/* Bai tap 1_66 - Sort tap tin */

#include <stdio.h>

#include <stdlib.h>

#define MAX 10

void main()

{

int i, j, mang[MAX], m;

FILE *fp;

randomize();

if ((fp = fopen("SFILE.DAT", "w+")) == NULL)

printf("

Khong the tao tap tin SFILE.DAT");

else

{

printf("

");

for (i=0; i<MAX; i++)

{ m = random(100);

fprintf(fp, "%d ", m);

printf("%d ", m);

}

fclose(fp);

fp = fopen("SFILE.DAT", "r");

for (i=0; i<MAX; i++)

fscanf(fp, "%d", &mang[i]);

fclose(fp);

for (i=0; i<MAX-1; i++)

for (j=i+1; j<MAX; j++)

if (mang[j] < mang[i])

{

m = mang[i];

mang[i] = mang[j];

mang[j] = m;

}

printf("

");

fp = fopen("SFILE2.DAT", "w+");

for (i=0; i<MAX; i++)

{

fprintf(fp, "%d ", mang[i]);

printf("%d ", mang[i]);

}

fclose(fp);

}

getch();

}

/* Bai tap 1_90 - Minh hoa phuong phap sap xep doi cho truc tiep */

#include <conio.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>

#include <dos.h>

#define MAX 10 // so luong phan tu toi da (khong duoc qua 10)

#define DONG 10 // dong bat dau in mang

#define DELAY 300

int mang[MAX]; // Bien toan cuc

int cot = (80 - 5 * MAX) / 2;

void hoandoi(int i, int j)

{

int xi = cot + 2 + i * 5;

int xj = cot + 2 + j * 5;

int xdest = xj;

int yi = DONG + 1;

int yj = DONG + 1;

int k, l;

textcolor(YELLOW);

for (k = 0; k<2; k++)

{

gotoxy(xi,yi--);

cprintf(" ");

gotoxy(xj,yj++);

cprintf(" ");

gotoxy(xi,yi);

cprintf("%2d",mang[i]);

gotoxy(xj,yj);

cprintf("%2d",mang[j]);

delay(DELAY);

}

while (xi<xdest)

{

gotoxy(xi++,yi);

cprintf(" ");

gotoxy(xj--,yj);

cprintf(" ");

gotoxy(xi,yi);

cprintf("%2d",mang[i]);

gotoxy(xj,yj);

cprintf("%2d",mang[j]);

delay(DELAY);

}

for (k = 0; k<2; k++)

{

gotoxy(xi,yi++);

cprintf(" ");

gotoxy(xj,yj--);

cprintf(" ");

gotoxy(xi,yi);

cprintf("%2d",mang[i]);

gotoxy(xj,yj);

cprintf("%2d",mang[j]);

delay(DELAY);

}

xdest = mang[i];

mang[i] = mang[j];

mang[j] = xdest;

}

void in_bien_i(int i)

{

gotoxy(cot+(i-1)*5+1,DONG-3);

textcolor(BLACK);

cprintf("%c",25);

gotoxy(cot+(i)*5+1,DONG-3);

textcolor(WHITE + BLINK);

cprintf("%c",25);

delay(DELAY);

}

void in_bien_j(int j)

{

gotoxy(cot+(j-1)*5+1,DONG+5);

textcolor(BLACK);

cprintf("%c",24);

gotoxy(cot+(j)*5+1,DONG+5);

textcolor(WHITE + BLINK);

cprintf("%c",24);

delay(DELAY);

}

void main()

{

// Phan khai bao bien

char tieude[]="CHUONG TRINH DEMO MOT PHUONG PHAP SAP XEP (SORT)";

char khungtrai[3] = {'É','º','È'};

char khungtrong[3] = {'Ë','º','Ê'};

char khungphai[3] = {'»','º','¼'};

char c;

int i, j;

// Xoa man hinh

textmode(C80);

textattr(14);

clrscr();

// In dong tieu de

gotoxy((80-strlen(tieude))/2,2);

cprintf("%s",tieude);

// Khoi tao mang mot cach ngau nhien

randomize();

for (i=0; i<MAX; i++)

mang[i] = random(100);

// In mang ra man hinh

textcolor(WHITE);

for (i=0;i<3;i++)

{

gotoxy(cot,DONG+i);

cprintf("%c", khungtrai[i]);

}

for (j=0; j<MAX; j++)

{

textcolor(WHITE);

gotoxy(cot+j*5+1,DONG);

cprintf("Í ÍÍ");

textcolor(YELLOW);

gotoxy(cot+j*5+2,DONG+1);

cprintf("%2d",mang[j]);

textcolor(WHITE);

gotoxy(cot+j*5+1,DONG+2);

cprintf("Í ÍÍ");

for (i=0;i<3;i++)

{

gotoxy(cot+(j+1)*5,DONG+i);

cprintf("%c", khungtrong[i]);

}

}

for (i=0;i<3;i++)

{

gotoxy(cot+MAX*5,DONG+i);

cprintf("%c", khungphai[i]);

}

// In cau thong bao va cho nguoi su dung nhan phim de thuc hien

textcolor(11);

gotoxy (1,24);

cprintf("Nhan phim A : Sap xep tang dan - D : Sap xep giam dan");

gotoxy (1,DONG-4);

cprintf("Bien i");

gotoxy (1,DONG+6);

cprintf("Bien j");

do

{

c = toupper(getch());

} while ((c != 'A') && (c != 'D'));

// Bat dau thuc hien sap xep

switch (c)

{

case 'A' :

for (i=0; i<MAX-1; i++)

{

in_bien_i(i);

for (j=i+1; j<MAX; j++)

{

in_bien_j(j);

if (mang[i] > mang[j])

hoandoi(i,j);

}

gotoxy(cot+(j-1)*5+1,DONG+5);

textcolor(BLACK);

cprintf("%c",24);

}

break;

case 'D' :

for (i=0; i<MAX-1; i++)

{

in_bien_i(i);

for (j=i+1; j<MAX; j++)

{

in_bien_j(j);

if (mang[i] < mang[j])

hoandoi(i,j);

}

gotoxy(cot+(j-1)*5+1,DONG+5);

textcolor(BLACK);

cprintf("%c",24);

}

break;

}

gotoxy(cot+(i-1)*5+1,DONG-3);

textcolor(BLACK);

cprintf("%c",25);

// Thong bao va cho nguoi dung nhan phim de ket thuc

gotoxy(1,24);

clreol();

textcolor(13+BLINK);

cprintf("Nhan phim bat ky de ket thuc");

getch();

}

/* Tim so nguyen to nho hon N */

#include <stdio.h>

void main()

{

int n, i, j;

printf("

Nhap gia tri N : ");

scanf("%d", &n);

printf("

Cac so nguyen to nho hon n la : ");

for (i=2; i<n; i++)

{

for (j=2; j<i; j++)

if (i%j == 0)

break;

if (j == i)

printf("%d ", i);

}

getch();

}

/* Bai tap 1_19 - Tim cac so hoan hao nho hon N */

#include <stdio.h>

void main()

{

int n, i, j, tong;

printf("

CHo biet gia tri N : ");

scanf("%d", &n);

for (i=2; i<=n; i++)

{

tong = 1;

for (j=2; j<=i/2; j++)

if (i%j == 0)

tong += j;

if (tong == i)

printf("

%d", i);

}

getch();

}

#include <stdio.h>

char s[50] = "";

char *bachuso(unsigned int n)

{

char kyso[10][5]= {"", "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;

}

void main()

{

unsigned long n;

unsigned ngan, trieu, ty, donvi;

char chuoi[200];

printf("

Nhap mot so khong dau : ");

scanf("%ld", &n);

printf("So %ld doc bang chu la :

", n);

if (n == 0)

printf("khong");

else

{

donvi = n % 1000;

n /= 1000;

ngan = n % 1000;

n /= 1000;

trieu = n % 1000;

ty = n / 1000;

chuoi[0] = 0;

if (ty > 0)

printf("%s ty ", bachuso(ty));

if (trieu > 0)

printf("%s trieu ", bachuso(trieu));

if (ngan > 0)

printf("%s ngan ", bachuso(ngan));

if (donvi > 0)

printf("%s", bachuso(donvi));

}

getch();

}

/* Bai tap 1_88 - Dien gia tri cho mot mang vuong theo chieu kim dong ho */

#include <stdio.h>

void main()

{

int n, w, hang, cot, chanhangtang, chanhanggiam, chancottang, chancotgiam;

int matran[12][12], tang, giam;

printf("

Chuong trinh in ma tran dac biet.");

do {

printf("

Nhap kich thuoc cua ma tran (3 <= n <= 12) : ");

scanf("%d%*c",&n);

} while (! (n >=3 && n<=12) );

hang = cot = 0;

chanhangtang = chancottang = n-1;

chanhanggiam = chancotgiam = 0;

/*

tang co 4 gia tri :

tang = 0 -> tang cot

tang = 1 -> tang hang

tang = 2 -> giam cot

tang = 3 -> giam cot

*/

tang = 0;

for (w=0; w<n*n; w++)

{

matran[hang][cot]= w + 1;

switch(tang)

{

case 0 : if (cot < chancottang)

cot++;

else

{

tang++; // Chuyen sang tang hang

chanhanggiam++;

hang++;

}

break;

case 1 : if (hang < chanhangtang)

hang++;

else

{

tang++; // Chuyen sang giam cot

chancottang--;

cot--;

}

break;

case 2 : if (cot > chancotgiam)

cot--;

else

{

tang++; // Chuyen sang giam hang

chanhangtang--;

hang--;

}

break;

case 3 : if (hang > chanhanggiam)

hang--;

else

{

tang=0; // Chuyen sang tang cot

chancotgiam++;

cot++;

}

break;

}

}

for (hang=0; hang < n; hang++)

{

printf("

");

for (cot=0; cot < n; cot++)

printf("%4d", matran[hang][cot]);

}

getch();

}

/* Bai tap 1_89 - In cac gia tri cua mot mang vuong theo chieu kim dong ho */

#include <stdio.h>

#include <conio.h>

#include <dos.h>

void main()

{

int m, n, w, hang, cot, chanhangtang, chanhanggiam, chancottang, chancotgiam;

int matran[12][12], tang, giam;

printf("

Chuong trinh in ma tran dac biet.");

do {

printf("

Nhap so hang cua ma tran (3 <= n <= 12) : ");

scanf("%d%*c",&n);

} while (! (n >=3 && n<=12) );

do {

printf("

Nhap so cot cua ma tran (3 <= m <= 12) : ");

scanf("%d%*c",&m);

} while (! (m >=3 && m<=12) );

/* Nhap ma tran */

for (hang=0; hang <n; hang++)

for (cot=0; cot<m; cot++)

{

printf("phan tu [%02d][%02d] = ",hang,cot);

scanf("%d",&matran[hang][cot]);

}

hang = cot = 0;

chanhangtang = n-1;

chancottang = m-1;

chanhanggiam = chancotgiam = 0;

/*

tang co 4 gia tri :

tang = 0 -> tang cot

tang = 1 -> tang hang

tang = 2 -> giam cot

tang = 3 -> giam cot

*/

tang = 0;

clrscr();

for (w=0; w<n*m; w++)

{

gotoxy(10 + cot * 4, 3 + hang);

printf("%4d",matran[hang][cot]);

delay(200);

switch(tang)

{

case 0 : if (cot < chancottang)

cot++;

else

{

tang++; // Chuyen sang tang hang

chanhanggiam++;

hang++;

}

break;

case 1 : if (hang < chanhangtang)

hang++;

else

{

tang++; // Chuyen sang giam cot

chancottang--;

cot--;

}

break;

case 2 : if (cot > chancotgiam)

cot--;

else

{

tang++; // Chuyen sang giam hang

chanhangtang--;

hang--;

}

break;

case 3 : if (hang > chanhanggiam)

hang--;

else

{

tang=0; // Chuyen sang tang cot

chancotgiam++;

cot++;

}

break;

}

}

getch();

}

/* Bai tap 1_101 - Chuong trinh sinh tu ngau nhien */

#include <stdio.h>

#include <stdlib.h>

#define MAX 7

#define NUM 100

void main()

{

int i, n, k;

char s[MAX+1];

randomize();

printf("

");

for (n=0; n<NUM; n++)

{

s[0] = random('Z'-'A'+1) + 'A';

k = random(MAX) + 1;

for (i=1; i<k; i++)

s[i] = random('z'-'a'+1) + 'a';

s[i] = 0;

printf("%-8s", s);

}

getch();

}

#include <stdio.h>

#include <math.h>

double giaithua(double n)

{

double i, ketqua=1;

for (i=2; i<=n; i++)

ketqua *= i;

return ketqua;

}

void main()

{

double ketqua, epsilon, x, n = 1;

printf("

Nhap gia tri x (radian) : ");

scanf("%lf", &x);

ketqua = x;

do {

epsilon = pow(x, 2*n+1) / giaithua(2*n+1);

ketqua += pow(-1, n) * epsilon;

n++;

} while (fabs(epsilon) > 0.0001);

printf("

Ket qua tinh theo cong thuc = %10.8lf", ketqua);

printf("

Ket qua tinh theo ham = %10.8lf", sin(x));

getch();

}

/* Bai tap 1_74 - Luu noi dung man hinh vao tap tin SCREEN.DAT */

#include <stdio.h>

#include <mem.h>

#include <dos.h>

void main()

{

char far *video;

FILE *fp;

video = (char far*)MK_FP(0xB800, 0);

if ((fp = fopen("SCREEN.DAT", "w+b")) == NULL)

printf("

Khong the tao tap tin SCREEN.DAT");

else

{

i = 0;

for (i=0; i<4000; i++)

fputc(video[i], fp);

fclose(fp);

printf("

Cong viec hoan tat");

}

getch();

}

/* Giai thuat sang Erastosthene */

#include <stdio.h>

void main()

{

static int NT[1000], i, k;

printf("

Cac so nguyen to nho hon 1000 :

");

for (i=2; i<1000; i++)

if (NT[i] == 0)

{

k = 1;

while (k*i<1000)

NT[i*k++] = 1;

printf("%4d ", i);

}

getch();

}

/* Bai tap 1_75 - Lay lai noi dung man hinh tu tap tin SCREEN.DAT */

#include <stdio.h>

#include <mem.h>

#include <dos.h>

void main()

{

char far *video;

FILE *fp;

video = (char far*)MK_FP(0xB800, 0);

if ((fp = fopen("SCREEN.DAT", "r+b")) == NULL)

printf("

Khong the tao tap tin SCREEN.DAT");

else

{

for (i=0; i<4000; i++)

video[i] = fgetc(fp);

fclose(fp);

}

getch();

}

/* Bai tap 1_61 - Quan ly danh sach hoc sinh */

#include <stdio.h>

#define MAX 100

typedef struct hocsinh {

char hoten[35];

char lop[7];

float diemtoan, diemly, diemhoa;

} HOCSINH;

HOCSINH danhsach[MAX];

int numrecords = 0;

char filename[] = "DANHSACH.DAT";

void main()

{

int done = 0, i;

char hoten[35];

float diem;

do {

printf("

Ho ten (trong de thoat) : ");

gets(hoten);

if (strlen(hoten) == 0)

done = 1;

else

{

strcpy(danhsach[numrecords].hoten, hoten);

printf("

Lop : ");

gets(danhsach[numrecords].lop);

printf("

Diem toan : ");

scanf("%f", &diem);

danhsach[numrecords].diemtoan = diem;

printf("

Diem ly : ");

scanf("%f", &diem);

danhsach[numrecords].diemly = diem;

printf("

Diem hoa : ");

scanf("%f%*c", &diem);

danhsach[numrecords].diemhoa = diem;

numrecords++;

}

} while (!done);

for (i=0; i<numrecords; i++)

{

printf("

+-----+------------------------------------+--------+------+------+------+");

printf("

| STT | H O V A T E N | L O P | TOAN | LY | HOA |");

printf("

+-----+------------------------------------+--------+------+------+------+");

for (i=0; i<numrecords; i++)

{

printf("

| %3d | ", i+1);

printf("%-35s| %-7s| %4.1f | %4.1f | %4.1f |", danhsach[i].hoten,danhsach[i].lop,

danhsach[i].diemtoan, danhsach[i].diemly, danhsach[i].diemhoa);

}

printf("

+-----+------------------------------------+--------+------+------+------+");

}

getch();

}

/* Bai tap 1_61 - Quan ly danh sach hoc sinh */

#include <stdio.h>

#define MAX 100

typedef struct hocsinh {

char hoten[35];

char lop[7];

float diemtoan, diemly, diemhoa;

} HOCSINH;

HOCSINH danhsach[MAX];

int numrecords = 0;

char filename[] = "DANHSACH.DAT";

int menu()

{

char c;

clrscr();

printf("+----- Menu -----+");

printf("

+----------------+");

printf("

+ 1. Nhap +");

printf("

+ 2. Xoa +");

printf("

+ 3. Sua doi +");

printf("

+ 4. Liet ke +");

printf("

+----------------+");

printf("

+ 5. Luu len dia +");

printf("

+ 6. Lay tu dia +");

printf("

+----------------+");

printf("

+ 0. Thoat +");

printf("

+----------------+");

printf("

Chon muc : ");

do {

c = getch();

} while (c<'0' || c>'6');

return c-'0';

}

void nhapmoi()

{

int done = 0;

char hoten[35];

float diem;

do {

printf("

Ho ten (trong de thoat) : ");

gets(hoten);

if (strlen(hoten) == 0)

done = 1;

else

{

strcpy(danhsach[numrecords].hoten, hoten);

printf("

Lop : ");

gets(danhsach[numrecords].lop);

printf("

Diem toan : ");

scanf("%f", &diem);

danhsach[numrecords].diemtoan = diem;

printf("

Diem ly : ");

scanf("%f", &diem);

danhsach[numrecords].diemly = diem;

printf("

Diem hoa : ");

scanf("%f%*c", &diem);

danhsach[numrecords].diemhoa = diem;

numrecords++;

}

} while (!done);

}

void xoa()

{

int i, done = 0, found, dump[MAX];

char hoten[35], hotentmp[35];

while (!done)

{

if (numrecords != 0)

{

printf("

Ho ten hoc sinh can xoa (trong de thoat) : ");

gets(hoten);

if (strlen(hoten) == 0)

done = 1;

else

{

i = found = 0;

do {

strcpy(hotentmp,danhsach[i].hoten);

if (strcmp(strupr(hoten), strupr(hotentmp)) == 0)

dump[found++] = i;

i++;

} while (i<numrecords);

if (!found)

printf("

Khong tim thay hoc sinh co ten %s", hoten);

else

{

for (i=0; i<found; i++)

{

printf("

%3d %-35s %-7s %4.1f %4.1f %4.1f", i+1, danhsach[dump[i]].hoten,danhsach[dump[i]].lop,

danhsach[dump[i]].diemtoan, danhsach[dump[i]].diemly, danhsach[dump[i]].diemhoa);

}

printf("

Tim thay %d hoc sinh.", found);

printf("

Chon thu tu muon xoa (neu muon huy bo thi chon so khong nam trong pham vi): ");

scanf("%d", &i);

if (i >= 1 && i <= found)

{

for (found=dump[i-1]; found<numrecords-1; found++)

danhsach[found] = danhsach[found+1];

numrecords--;

printf("

Da huy");

}

else

printf("

Viec xoa bi huy bo.");

}

done = 1;

}

getch();

}

else

done = 1;

}

}

void sua()

{

int i, done = 0, found, dump[MAX];

char hoten[35], hotentmp[35];

float diem;

while (!done)

{

if (numrecords != 0)

{

printf("

Ho ten hoc sinh can sua (trong de thoat) : ");

gets(hoten);

if (strlen(hoten) == 0)

done = 1;

else

{

i = found = 0;

do {

strcpy(hotentmp,danhsach[i].hoten);

if (strcmp(strupr(hoten), strupr(hotentmp)) == 0)

dump[found++] = i;

i++;

} while (i<numrecords);

if (!found)

printf("

Khong tim thay hoc sinh co ten %s", hoten);

else

{

for (i=0; i<found; i++)

{

printf("

%3d %-35s %-7s %4.1f %4.1f %4.1f", i+1, danhsach[dump[i]].hoten,danhsach[dump[i]].lop,

danhsach[dump[i]].diemtoan, danhsach[dump[i]].diemly, danhsach[dump[i]].diemhoa);

}

printf("

Tim thay %d hoc sinh.", found);

printf("

Chon thu tu muon sua doi (neu muon huy bo thi chon so khong nam trong pham vi): ");

scanf("%d%*c", &i);

if (i >= 1 && i <= found)

{

printf("

%s

Ho ten : ", danhsach[dump[i-1]].hoten);

gets(danhsach[dump[i-1]].hoten);

printf("

%s

Lop : ", danhsach[dump[i-1]].lop);

gets(danhsach[dump[i-1]].lop);

printf("

%4.2f

Diem toan : ",danhsach[dump[i-1]].diemtoan);

scanf("%f", &diem);

danhsach[dump[i-1]].diemtoan = diem;

printf("

%4.2f

Diem ly : ", danhsach[dump[i-1]].diemly);

scanf("%f", &diem);

danhsach[dump[i-1]].diemly = diem;

printf("

%4.2f

Diem hoa : ", danhsach[dump[i-1]].diemhoa);

scanf("%f%*c", &diem);

danhsach[dump[i-1]].diemhoa = diem;

printf("

Da thay doi");

}

else

printf("

Viec thay doi bi huy bo.");

}

done = 1;

}

getch();

}

else

done = 1;

}

}

void lietke()

{

if (numrecords != 0)

{

printf("

+-----+------------------------------------+--------+------+------+------+");

printf("

| STT | H O V A T E N | L O P | TOAN | LY | HOA |");

printf("

+-----+------------------------------------+--------+------+------+------+");

for (i=0; i<numrecords; i++)

{

printf("

| %3d | ", i+1);

printf("%-35s| %-7s| %4.1f | %4.1f | %4.1f |", danhsach[i].hoten,danhsach[i].lop,

danhsach[i].diemtoan, danhsach[i].diemly, danhsach[i].diemhoa);

}

printf("

+-----+------------------------------------+--------+------+------+------+");

}

getch();

}

void getdata()

{

char c;

int start, nrec;

FILE *fp;

long curpos, length;

start = 0;

if (numrecords != 0)

{

printf("

Dang co du lieu trong bo nho. Co muon noi them vao khong?(C/K)");

c = toupper(getch());

if (c == 'C')

start = numrecords;

else

start = 0;

}

if ((fp = fopen(filename, "r")) == NULL)

printf("

Khong the mo tap tin %s", filename);

else

{

curpos = ftell(fp);

fseek(fp, 0L, SEEK_END);

length = ftell(fp);

fseek(fp, curpos, SEEK_SET);

nrec = length / sizeof(HOCSINH);

fread(&danhsach[start], sizeof(HOCSINH), nrec, fp);

fclose(fp);

printf("

Them vao %d records.", nrec);

numrecords = numrecords + nrec;

getch();

}

}

void putdata()

{

FILE *fp;

if ((fp = fopen(filename, "w+")) == NULL)

printf("

Khong thay ghi tap tin %s", filename);

else

{

fwrite(&danhsach[0], sizeof(HOCSINH), numrecords, fp);

fclose(fp);

printf("

Ghi %d records.", numrecords);

getch();

}

}

void main()

{

int chon, done = 0;

do {

chon = menu();

switch(chon)

{

case 1 : nhapmoi();

break;

case 2 : xoa();

break;

case 3 : sua();

break;

case 4 : lietke();

break;

case 5 : putdata();

break;

case 6 : getdata();

break;

case 0 : done = 1;

}

} while (!done);

}

/* Bai tap 1_49 - Chuong trinh Puzzle 15 so */

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#define XSTART 30

int board[4][4];

int xspace, yspace;

void khoitao()

{

int i, j;

for (i=0; i<4; i++)

for (j=0; j<4; j++)

board[i][j] = i*4 + j + 1;

board[3][3] = 0;

}

void veboard()

{

int i, j;

textattr(14);

clrscr();

gotoxy(XSTART, 2);

cprintf("ÉÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍ»");

for (i=0; i<4; i++)

{

gotoxy(XSTART, 2*i+3);

cprintf("º º º º º");

gotoxy(XSTART, 2*i+4);

cprintf("̀ÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹");

}

gotoxy(XSTART, 10);

cprintf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ");

for(i=0; i<4; i++)

for (j=0; j<4; j++)

{

gotoxy(XSTART+1 + j*5, i*2+3);

cprintf("%3d", board[i][j]);

}

gotoxy(XSTART+16, 9);

cprintf(" ");

gotoxy(XSTART, 12);

cprintf("Nhan S de bat dau.");

gotoxy(XSTART, 13);

cprintf("ESC de ket thuc.");

xspace = 3;

yspace = 3;

}

void dichtrai()

{

if (xspace < 3 )

{

board[yspace][xspace] = board[yspace][xspace+1];

board[yspace][xspace+1] = 0;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf("%3d", board[yspace][xspace]);

xspace++;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf(" ");

}

}

void dichphai()

{

if (xspace > 0 )

{

board[yspace][xspace] = board[yspace][xspace-1];

board[yspace][xspace-1] = 0;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf("%3d", board[yspace][xspace]);

xspace--;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf(" ");

}

}

void dichlen()

{

if (yspace < 3 )

{

board[yspace][xspace] = board[yspace+1][xspace];

board[yspace+1][xspace] = 0;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf("%3d", board[yspace][xspace]);

yspace++;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf(" ");

}

}

void dichxuong()

{

if (yspace > 0 )

{

board[yspace][xspace] = board[yspace-1][xspace];

board[yspace-1][xspace] = 0;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf("%3d", board[yspace][xspace]);

yspace--;

gotoxy(XSTART+1 + xspace*5, yspace*2+3);

cprintf(" ");

}

}

{

int i, j;

for (i=0; i<4; i++)

for (j=0; j<4; j++)

if (i!=yspace || j!=xspace)

if (board[i][j] != i*4 + j + 1)

return 0;

textattr(15);

gotoxy(XSTART, 14);

cprintf("Hoan ho, ban da thang");

getch();

return 1;

}

void run()

{

int done = 0, sobuoc = 0, i, n = 0;

char c;

randomize();

while (n < 100)

{

i = random(4);

switch(i)

{

case(0) : dichtrai();

break;

case(1) : dichphai();

break;

case(2) : dichlen();

break;

case(3) : dichxuong();

break;

}

n++;

}

gotoxy(XSTART, 12);

cprintf("%c %c %c %c de di chuyen.", 24, 25, 26, 27);

do {

c = getch();

if (c == 0)

c = getch();

switch(c)

{

case 27 : done = 1;

break;

case 75 : dichtrai();

break;

case 77 : dichphai();

break;

case 72 : dichlen();

break;

case 80 : dichxuong();

break;

}

sobuoc++;

gotoxy(XSTART, 11);

cprintf("So buoc : %d", sobuoc);

if (!done)

done = test();

} while (!done);

}

void main()

{

char c;

khoitao();

veboard();

do {

c = toupper(getch());

} while (c != 27 && c != 'S');

if (c == 'S')

run();

}

#include <stdio.h>

void main()

{

int n, a[100], i, x;

printf("

Nhap vao so luong phan tu cua mang : ");

scanf("%d", &n);

printf("Nhap vao cac phan tu cua mang : ");

for (i=0; i<n; i++)

scanf("%d", &a[i]);

printf("

Nhap vao gia tri x : ");

scanf("%d", &x);

printf("

Cac phan tu lon hon %d gom co : ", x);

printf("

Chi so Gia tri");

for (i=0; i<n; i++)

if (a[i] > x)

printf("

%4d %6d", i, a[i]);

getch();

}

/* Tim nhung gia tri nguyen x, y, z thoa xư + yư = zư */

#include <math.h>

void main()

{

long x, y, z, count = 0;

for (x=1; x<100; x++)

for (y=1; y<100; y++)

{

z = sqrt(x*x+y*y);

if (z*z == x*x + y*y)

{

printf("

%6ld %6ld %6ld", x, y, z);

count++;

}

}

printf("

Tong cong co %ld cap so thoa man.", count);

getch();

}

/* Bai tap 1_12 - Chuong trinh Piano don gian */

#include <stdio.h>

#include <dos.h>

#include <ctype.h>

void main()

{

char c;

printf("

Nhan cac phim A, B, C, D, E, F, G de choi cac not nhac.");

printf("

Nhan phim ESC de ket thuc.");

do {

c = toupper(getch());

switch(c)

{

case 'A' : sound(440);

break;

case 'B' : sound(494);

break;

case 'C' : sound(523);

break;

case 'D' : sound(587);

break;

case 'E' : sound(659);

break;

case 'F' : sound(698);

break;

case 'G' : sound(784);

break;

}

delay(100);

nosound();

} while (c != 27);

}

/* Bai tap 1_102 - Tro choi pha san */

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

int player[2], luot = 0, y;

void giam0()

{

player[0]--;

gotoxy(player[0]%5*2+1, y + player[0]/5);

cprintf(" ");

gotoxy(player[1] % 5*2 + 25, y + player[1]/5);

cprintf("*");

player[1]++;

}

void giam1()

{

player[1]--;

gotoxy(player[1]%5*2+25, y + player[1]/5);

cprintf(" ");

gotoxy(player[0] % 5*2 + 1, y + player[0]/5);

cprintf("*");

player[0]++;

}

void main()

{

int i, n, k, dong, tudong = 0;

char c;

randomize();

player[0] = player[1] = 20;

clrscr();

printf("Tro choi tung dong tien sap ngua.");

printf("

Hai nguoi choi A va B se lan luot tung dong tien");

printf("

Neu den luot nguoi nao ma tung ra dong sap thi");

printf("

Nguoi kia se bi mat tien va nguoc lai.");

printf("

Tro choi se ket thuc neu mot nguoi het tien hoac nhan ESC.

");

printf("

A \t\t B

");

y = wherey();

for (i=0; i<4; i++)

printf("* * * * *\t\t* * * * *

");

gotoxy(1,y+11);

cprintf("Nhan phim bat ky de di hoac T de choi tu dong");

do {

gotoxy(1,y+10);

cprintf("Den luot %c di : ", luot + 'A');

if (tudong)

{

delay(100);

if (kbhit())

c = getch();

}

else

{

c = toupper(getch());

if (c == 'T')

tudong = 1;

}

if (c != 27)

{

n = random(100);

dong = random(2);

for (k=0; k<n; k++)

{

if (dong == 1)

dong = 0;

else

dong = 1;

gotoxy(18,y+10);

cprintf("%d", dong);

delay(10);

}

if (dong == 0)

{

if (luot == 1)

giam1();

else

giam0();

}

else

{

if (luot == 1)

giam0();

else

giam1();

}

if (luot == 0)

luot = 1;

else

luot = 0;

}

} while (player[0] > 0 && player[1] > 0 && c != 27);

gotoxy(1,y+12);

if (player[0] ==0)

cprintf("A da het tien roi!");

else if (player[1] ==0)

cprintf("B da het tien roi!");

else

cprintf("Khong bai bac la tot!!!");

getch();

}

/* Phan so */

#include <stdio.h>

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;

}

void print(PHANSO a)

{

printf("%d/%d", a.tuso, a.mauso);

}

void main()

{

PHANSO a, b, c;

printf("

Nhap phan so a : ");

scanf("%d%d", &a.tuso, &a.mauso);

printf("

Nhap phan so b : ");

scanf("%d%d", &b.tuso, &b.mauso);

printf("

Toi gian a ta duoc : ");

a = uocluoc(a);

print(a);

printf("

Toi gian b ta duoc : ");

b = uocluoc(b);

printf("

Tong cua hai phan so = ");

c = cong(a, b);

print(c);

printf("

Hieu cua hai phan so = ");

c = tru(a, b);

print(c);

printf("

Tich cua hai phan so = ");

c = nhan(a, b);

print(c);

printf("

Thuong cua hai phan so = ");

c = chia(a, b);

print(c);

getch();

}

#include <stdio.h>

#include <conio.h>

#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("

%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();

}

#include <stdio.h>

void main()

{

FILE *fp1, *fp2, *fpout;

char sf1[50], sf2[50], sfout[50];

int c;

printf("

Nhap ten tap tin thu nhat : ");

scanf("%s", &sf1);

printf("

Nhap ten tap tin thu hai : ");

scanf("%s", &sf2);

printf("

Nhap ten tap tin ket qua : ");

scanf("%s", &sfout);

if ((fp1 = fopen(sf1, "r")) == NULL)

fprintf(stderr, "Khong the mo tap tin %s

", sf1);

if ((fp2 = fopen(sf2, "r")) == NULL)

fprintf(stderr, "Khong the mo tap tin %s

", sf2);

if ((fpout = fopen(sfout, "w")) == NULL)

fprintf(stderr, "Khong the mo tap tin %s

", sfout);

while ((c = getc(fp1)) != EOF)

putc(c, fpout);

while ((c = getc(fp2)) != EOF)

putc(c, fpout);

fclose(fp1);

fclose(fp2);

fclose(fpout);

printf("

Hoan tat! Nhan phim bat ky de ket thuc.");

getch();

}

/* Bai tap 1_99 - Minh hoa phep nhan tay */

#include <stdio.h>

void nhantay(unsigned long sobn, unsigned long son)

{

unsigned long k, n;

int i=0;

char s[20];

printf("

%20ld

", sobn);

ltoa(son, s, 10);

for (i=0; i<18-strlen(s); i++)

printf(" ");

printf("X %s", s);

printf("

------------");

n = son;

i = 0;

while (n>0)

{

k = n%10;

n /= 10;

k *= sobn;

ltoa(k, s, 10);

printf("

");

gotoxy(21 - strlen(s) - i, wherey());

cprintf("%s", s);

i++;

}

ltoa(sobn*son, s, 10);

printf("

------------

");

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();

}

/* Bai tap 1_16 - Tinh xem so N co phai la so nguyen to khong? */

#include <stdio.h>

#include <math.h>

void main()

{

int N, i, OK = 1;

printf("

Nhap gia tri N : ");

scanf("%d", &N);

for (i=2; i<(int)sqrt(N); i++)

if (N%i == 0)

{

OK = 0;

break;

}

if (OK)

printf("

So %d la so nguyen to.", N);

else

printf("

So %d khong phai la so nguyen to.", N);

getch();

}

/* Bai tap 6_1 */

#include <conio.h>

#include <stdlib.h>

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

\r", tonghang[i]);

}

textcolor(LIGHTBLUE);

for (j=0; j<10; j++)

cprintf("%4d", tongcot[j]);

textcolor(LIGHTGREEN);

cprintf("%4d", tongdcc);

textcolor(WHITE);

cprintf("

\rTong cac phan tu nho hon 6 = %3d", count);

getch();

}

/* Bai tap 1_70 - Ma hoa 1 file bang phep XOR */

#include <stdio.h>

void main()

{

char c, filein[50], fileout[50], key;

FILE *fpin, *fpout;

printf("

Cho biet ten tap tin nguon : ");

gets(filein);

printf("

Cho biet ten tap tin dich : ");

gets(fileout);

printf("

Cho 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("

Cong viec hoan tat");

}

getch();

}

/* Bai 6_2 - Tim phan tu lon nhat va nho nhat trong mang 1 chieu */

#include <conio.h>

#include <stdlib.h>

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();

}

/* Ma hoa dung XOR */

#include <stdio.h>

void main()

{

char chuoi[50], khoa;

int i, len;

printf("

Nhap 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("

Chuoi da duoc ma hoa thanh : %s", chuoi);

printf("

Nhan phim bat ky de giai ma.");

getch();

for (i=0; i<len; i++)

chuoi[i] ^= khoa;

printf("

Chuoi da duoc giai ma : %s", chuoi);

getch();

}

/* Bai tap 1_86 - Tro choi Logic */

#include <conio.h>

#include <dos.h>

#include <string.h>

#include <stdlib.h>

#include<stdio.h>

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;

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()

{

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();

}

/* Bai tap 2_4 - In chu so phan tram, chuc, don vi */

#include <stdio.h>

void main()

{

int n, tram, chuc, donvi;

printf("

Nhap vao mot so tu 100 - 999 : ");

scanf("%d", &n);

tram = n;

donvi = tram % 10;

tram /= 10;

chuc = tram % 10;

tram /= 10;

printf("

So hang tram = %d", tram);

printf("

So hang chuc = %d", chuc);

printf("

So hang don vi = %d", donvi);

getch();

}

/* Cho biet ma ban phim */

#include <stdio.h>

void main()

{

char c;

printf("

Nhap vao mot phim muon biet ma cua no. ESC de thoat

");

do {

c = getch();

if (c == 0)

{

c = getch();

printf("

Ma mo rong : %d", c);

}

else

printf("

Ma thuong : %d", c);

} while (c != 27);

}

/* In chuoi theo cac tu, moi tu mot dong */

#include <stdio.h>

void main()

{

char s[50];

int i, len;

printf("

Nhap vao mot chuoi : ");

len = strlen(s);

i = 0;

while (i<len)

{

while (s[i] == ' ' && i<len)

i++;

while (s[i] != ' ' && i<len)

putc(s[i++], stdout);

putc('

', stdout);

}

getch();

}

/* Giai he phuong trinh bac nhat */

#include <stdio.h>

void main()

{

int a, b, c, d, e, f, dthuc;

float x, y;

printf("

Nhap 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("

He phuong trinh vo ngiem.");

getch();

}

/* Bai tap 1_47 - Liet ke tat cac cac hoan vi cua n phan tu */

#include <stdio.h>

#define MAX 10

int mang[MAX], n;

void swap (int *x, int *y)

{

tmp = *x;

*x = *y;

*y = tmp;

}

void hoanvi(int k)

{

int j;

if (k==1)

{

printf("

");

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()

{

printf("

Cho biet so phan tu (N < 10) : ");

scanf("%d", &n);

for (i=0; i<n; i++)

mang[i] = i;

hoanvi(n);

getch();

}

/* Bai tap 1_15 - Tinh dien tich cac hinh hoc */

#include <stdio.h>

#include <conio.h>

#include <math.h>

int menu()

{

int c;

printf("

----- Menu -----");

printf("

1. Hinh tam giac");

printf("

2. Hinh tron");

printf("

3. Hinh vuong");

printf("

4. Hinh chu nhat");

printf("

0. Thoat");

printf("

----------------");

printf("

Chon muc : ");

do {

c = getch();

} while (c>'4' || c<'0');

putc(c, stdout);

return c -'0';

}

void hinhtamgiac()

{

int a, b, c;

float p;

printf("

Nhap vao ba canh tam giac : ");

scanf("%d%d%d", &a, &b, &c);

printf("

Chu vi tam giac = %d", a+b+c);

p = (float)(a + b + c)/2;

printf("

Dien tich tam giac = %lf", sqrt(p*(p-a)*(p-b)*(p-c)));

getch();

}

void hinhtron()

{

printf("

Nhap vao ban kinh hinh tron : ");

scanf("%d", &r);

printf("

Chu vi hinh tron = %f", 2 * M_PI * r);

printf("

Dien tich hinh tron = %f", M_PI * r * r);

getch();

}

void hinhvuong()

{

int a;

printf("

Nhap vao canh hinh vuong : ");

scanf("%d", &a);

printf("

Chu vi hinh tron = %d", 4 * a);

printf("

Dien tich hinh vuong = %d", a * a);

getch();

}

void hinhchunhat()

{

int a, b;

printf("

Nhap vao cac canh hinh chu nhat : ");

scanf("%d%d", &a, &b);

printf("

Chu vi hinh chu nhat = %d", 2 * (a+b));

printf("

Dien 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);

}

#include <stdio.h>

void main(int argc, char *argv[])

{

FILE *fp;

char c;

if (argc <= 1)

printf("

Cach su dung :

Hienfile <ten tap tin>");

else

if ((fp = fopen(argv[1], "r")) == NULL)

printf("

Khong the mo tap tin %s", argv[1]);

else

{

while ((c = fgetc(fp)) != EOF)

putc(c, stdout);

}

getch();

}

#include <stdio.h>

#include <string.h>

void main(int argc, char *argv[])

{

FILE *fp;

char s[255];

if (argc <= 1)

printf("

Cach su dung :

Hienfile <ten tap tin>");

else

if ((fp = fopen(argv[1], "r")) == NULL)

printf("

Khong the mo tap tin %s", argv[1]);

else

while (fgets(s, 255, fp))

{

s[strlen(s) - 1] = 0;

if (strlen(s))

puts(s);

}

getch();

}

/* Bai tap 2_8 - In so Hex tuong ung voi mot so nguyen duong */

#include <stdio.h>

void main()

{

unsigned number;

char hex[] = "0123456789ABCDEF";

printf("

Nhap 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();

}

/* Bai tap 1_20 - Tinh n!! */

#include <stdio.h>

void main()

{

int n, start, i;

unsigned long gthua = 1;

printf("

Nhap 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("

%d!! = %ld", n, gthua);

getch();

}

/* Bai tap - Bai toan Gonbach */

#include <stdio.h>

#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("

%ld = %ld + %ld + %ld", nguyento[n], nguyento[i],

nguyento[j], nguyento[l]);

getch();

}

/* Bai tap 2_6 - Cho biet goc thuoc goc vuong nao */

#include <stdio.h>

void main()

{

int goc;

printf("

Nhap vao gia tri goc : ");

scanf("%d", &goc);

printf("Goc do thuoc goc vuong thu %d", (goc/90)%4 + 1);

getch();

}

#include <stdio.h>

void main()

{

int a[10], i;

printf("

Nhap 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 3_1 - Ham tinh N! */

#include <stdio.h>

unsigned long giaithua(int n)

{

unsigned long ketqua = 1;

for (i=2; i<=n; i++)

ketqua *= i;

return ketqua;

}

void main()

{

printf("

Nhap vao gia tri N : ");

scanf("%d", &n);

printf("%d! = %lu", n, giaithua(n));

getch();

}

/* Bai tap 1_86 - Doc va ghi len file mot mang 2 chieu 10x10 */

#include <stdio.h>

#define MAX 10

char filename[] = "MANG1010.DAT";

void main()

{

int mang[MAX][MAX], i, j;

FILE *fp;

if ((fp = fopen(filename, "w+")) == NULL)

printf("

Khong the tao tap tin %s", filename);

else

{

printf("

Nhap cac phan tu cua mang : ");

for (i=0; i<MAX; i++)

for (j=0; j<MAX; j++)

{

printf("

Mang[%d][%d] = ", i, j);

scanf("%d", &mang[i][j]);

}

fwrite(&mang, sizeof(int), MAX * MAX, fp);

fclose(fp);

printf("

Viec ghi hoan tat.");

}

getch();

}

/* Bai tap 1_68 - Tap hop thong tin */

#include <stdio.h>

void main()

{

char s[100], s1[100], s2[100], filename1[50], filename2[50];

FILE *fp1, *fp2;

printf("

Cho viet ten tap tin thu nhat : ");

gets(filename1);

if ((fp1 = fopen(filename1, "w+")) == NULL)

printf("

Khong the tao tap tin %s", filename1);

else

{

printf("

Cho viet ten tap tin thu nhat : ");

gets(filename2);

if ((fp2 = fopen(filename2, "w+")) == NULL)

printf("

Khong the tao tap tin %s", filename2);

else

{

do {

printf("

Ho ten (ENTER de ket thuc) : ");

if (strlen(s) > 0)

{

strcat(s, "

");

fputs(s, fp1);

printf("

Tuoi : ");

strcat(s, "

");

fputs(s, fp1);

printf("

Dia chi : ");

strcat(s, "

");

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_21 - In day so Fibonanci */

#include <stdio.h>

void main()

{

int n, i, fib1 = 1, fib2 = 1, fib = 2;

printf("

Nhap 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();

}

#include <stdio.h>

void main()

{

double fahrenheit, celsius;

printf("

Nhap 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_87 - Demo ve hop dung conio */

#include <conio.h>

#include <stdlib.h>

#include <dos.h>

char doublebox[] ="ÉÍ»ºÈ¼";

char singlebox[] ="ÚÄ¿³ÀÙ";

void swap(int *x, int *y)

{

temp = *y;

*y = *x;

*x = temp;

}

void drawbox(int x1, int y1, int x2, int y2, int type)

{

char *boxtype;

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("

\rDRAWBOX DEMO.

\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);

}

}

/* Bai tap 9_4 - Tim chuoi doi xung */

void main()

{

char chuoi[125];

int i = 0, j;

printf("

Nhap 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 5_3 - Tim phuong an doi tien */

#include <stdio.h>

#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("

%5ld - %5ld%5ld%5ld%5ld%5ld", ++count, i, j, k, l, m);

soluong++;

}

printf("so luong = %ld", soluong);

getch();

}

#include <stdio.h>

void main()

{

char s[100];

printf("

Nhap vao mot chuoi : ");

i = 0;

while (s[i] != 0)

{

if (isalpha(s[i]))

if (s[i] < 97)

s[i] += 32;

else

s[i] -= 32;

i++;

}

printf("

Chuoi bien thanh : %s", s);

getch();

}

/* Bai tap 2_9 - Doi so tu co so n sang gia tri so co so m */

#include <stdio.h>

#include <string.h>

char *doics(char *number, int n, int m)

{

static char ketqua[17];

char chuso[16] = "0123456789ABCDEF";

int i = 0, giatri = 0, len;

len = strlen(number);

while(i<len) {

giatri = giatri * n + (strchr(chuso, number[i]) - &chuso);

i++;

}

i=16;

ketqua[17] = 0;

do {

ketqua[i] = chuso[giatri % m];

giatri /= m;

i--;

} while (giatri > 0);

return (ketqua + i + 1);

}

void main()

{

unsigned n, m;

char number[17], *ketqua;

do {

printf("

Nhap gia tri N (2 - 16) : ");

scanf("%d", &n);

} while (n<2 || n>16);

getchar();

printf("Gia tri thuoc he %d : ", n);

gets(number);

do {

printf("Nhap gia tri M (2 - 16) : ");

scanf("%d", &m);

} while (m<2 || m>16);

printf("Gia tri %s o co so %u co gia tri o co so %u la : ", number, n, m);

ketqua = doics(number, n, m);

puts(ketqua);

getch();

}

/* Bai tap 1_86 - Doc tu file mot mang 2 chieu 10x10 */

#include <stdio.h>

#define MAX 10

char filename[] = "MANG1010.DAT";

void main()

{

int mang[MAX][MAX], i, j;

FILE *fp;

if ((fp = fopen(filename, "r")) == NULL)

printf("

Khong the mo tap tin %s", filename);

else

{

fread(&mang, sizeof(int), MAX * MAX, fp);

fclose(fp);

printf("

Viec doc hoan tat.");

for (i=0; i<MAX; i++)

{

printf("

");

for (j=0; j<MAX; j++)

printf("%d ", mang[i][j]);

}

}

getch();

}

/* Doan so */

#include <stdio.h>

void main()

{

int n, k = 128, i = 0, solan = 0;

char c;

printf("

Ban hay nghi mot so trong dau. So do phai nho hon 100.");

delay(1000);

printf("Xong chua! Roi ban hay tra loi nhung cau hoi sau day cua toi.");

printf("

Toi se tim ra con so ma ban doan.");

do {

n = (k+i)/2;

solan++;

printf("

Lan doan thu %d.", solan);

printf("

Co phai so ban nghi la %d (Dung, Nho hon, Lon hon) ? ", n);

do {

c = toupper(getch());

} while (c != 'D' && c != 'N' && c != 'L');

switch (c)

{

case 'L' : i = n;

break;

case 'N' : k = n;

break;

case 'D' : printf("

Tai khong?");

break;

}

} while (c != 'D');

getch();

}

/* Dao nguoc chuoi */

char *dnchuoi(char *s)

{

char *tmp, i;

i = 0;

tmp = (char *)malloc(strlen(s)+1);

while (i<strlen(s))

*(tmp+i) = *(s + strlen(s) - i++ - 1);

*(tmp+i) = 0;

return tmp;

}

void main()

{

char hello[] = "Hello World";

char *s;

printf("

Chuoi ban dau = %s", hello);

s = dnchuoi(hello);

printf("

Chuoi dao nguoc = %s", s);

getch();

}

#include <stdio.h>

#include <ctype.h>

void main()

{

FILE *fp;

char sf[50], chuoi[250];

int len, i;

long count = 0;

printf("

Nhap vao ten tap tin : ");

scanf("%s", sf);

if ((fp = fopen(sf, "r")) == NULL)

fprintf(stderr, "

Khong the mo tap tin %s", sf);

else

{

while (fgets(chuoi, 250, fp) != NULL)

{

len = strlen(chuoi) - 1;

i=0;

do {

while (chuoi[i] == ' ' && i <= len-1)

i++;

if (i<=len-1)

{

while (isalnum(chuoi[i]))

i++;

count++;

}

} while (i <= len-1);

}

printf("

So ky tu trong %s la %ld", sf, count);

}

getch();

}

#include <stdio.h>

#include <ctype.h>

void main()

{

FILE *fp;

char sf[50], c;

long count = 0;

printf("

Nhap vao ten tap tin : ");

scanf("%s", sf);

if ((fp = fopen(sf, "r")) == NULL)

fprintf(stderr, "

Khong the mo tap tin %s", sf);

else

{

while ((c = fgetc(fp)) != EOF)

if (isalpha(c))

count++;

printf("

So ky tu trong %s la %ld", sf, count);

}

getch();

}

/* Baitap 1_69 - Xem tap tin DBF */

#include <stdio.h>

typedef struct tagDBF_HEADER {

unsigned char signature; /* 03 = dbf, 83 dbf+dbt */

unsigned char ymd[3];

long num_records;

int data_off;

int rec_size;

unsigned char pad[20];

} DBF_HEADER;

typedef struct tagDBF_STRUCT {

char name[11];

char type;

long fstart;

unsigned char fsize;

unsigned char decimal;

char pad[14];

} DBF_STRUCT;

DBF_HEADER dbheader;

DBF_STRUCT dbstruct[255];

int numfields;

void main()

{

char filename[50];

char *dump, chuoi[250] ;

int i, j, offset, k;

FILE *fp;

printf("

Cho biet ten tap tin DBF muon xem : ");

gets(filename);

if ((fp=fopen(filename, "rb")) == NULL)

printf("Khong the mo tap tin %s", filename);

else

{

fread(&dbheader, sizeof(DBF_HEADER), 1, fp);

if (dbheader.signature == 3 || dbheader.signature == 0x83)

{

numfields = (dbheader.data_off - 33) / 32;

fread(&dbstruct, sizeof(DBF_STRUCT), numfields, fp);

fgetc(fp);

dump = (char *)malloc(dbheader.rec_size);

k = 7;

for (j=0; j<numfields; j++)

{

gotoxy(k, wherey());

textcolor(j+1);

cprintf("%s", dbstruct[j].name);

k += dbstruct[j].fsize;

}

for (i=0; i<dbheader.num_records; i++)

{

fread(dump, dbheader.rec_size, 1, fp);

printf("

%3d ", i+1);

if (dump[0] != 32)

printf("* ");

else

printf(" ");

offset= 1;

for (j=0; j<numfields; j++)

{

switch(dbstruct[j].type)

{

case 'C' : strncpy(chuoi, &dump[offset], dbstruct[j].fsize);

chuoi[dbstruct[j].fsize] = 0;

printf("%s ", chuoi);

break;

case 'D' : printf("%c%c/%c%c/%c%c%c%c ",dump[offset+6], dump[offset+7],

dump[offset+4],dump[offset+5],dump[offset],dump[offset+1],

dump[offset+2],dump[offset+3]);

break;

case 'N' : for (k=0; k<dbstruct[j].fsize; k++)

printf("%c", dump[offset+k]);

printf(" ");

break;

case 'L' : printf("%c ", dump[offset]);

break;

}

offset += dbstruct[j].fsize;

}

}

free(dump);

}

else

printf("Khong phai tap tin DBF");

fclose(fp);

}

getch();

}

#include <stdio.h>

void main()

{

int a[10], i, maxstart, maxend, maxtotal, tmpstart, tmpend, tmptotal;

printf("

Nhap vao 10 phan tu nguyen cua day :");

for (i=0; i<10; i++)

scanf("%d", &a[i]);

printf("Day da cho :

");

for (i=0; i<10; i++)

printf("%6d", a[i]);

maxstart = maxend = tmpstart = tmpend = 0;

maxtotal = tmptotal = a[0];

for (i=1; i< 10; i++)

{

if (a[i] < a[tmpend])

{

if (maxtotal < tmptotal)

{

maxstart = tmpstart;

maxend = tmpend;

maxtotal = tmptotal;

}

tmpstart = tmpend = i;

tmptotal = a[i];

}

else

{

tmptotal += a[i];

tmpend++;

}

}

if (maxtotal < tmptotal)

{

maxstart = tmpstart;

maxend = tmpend;

}

printf("

Day tang co tong nhieu nhat la :

");

for (i=maxstart; i<=maxend; i++)

printf("%6d", a[i]);

getch();

}

#include <stdio.h>

void main()

{

int a[10], i, maxstart, maxend, maxlen, tmpstart, tmpend, tmplen;

printf("

Nhap vao 10 phan tu nguyen cua day :");

for (i=0; i<10; i++)

scanf("%d", &a[i]);

printf("Day da cho :

");

for (i=0; i<10; i++)

printf("%6d", a[i]);

maxstart = maxend = tmpstart = tmpend = 0;

maxlen = tmplen = 1;

for (i=1; i< 10; i++)

{

if (a[i] < a[tmpend])

{

if (maxlen < tmplen)

{

maxstart = tmpstart;

maxend = tmpend;

maxlen = tmplen;

}

tmpstart = tmpend = i;

tmplen = 1;

}

else

{

tmplen++;

tmpend++;

}

}

if (maxlen < tmplen)

{

maxstart = tmpstart;

maxend = tmpend;

}

printf("

Day tang co so phan tu nhieu nhat la :

");

for (i=maxstart; i<=maxend; i++)

printf("%6d", a[i]);

getch();

}

/* Bai tap 1_100 - Tinh thu cua ngay */

#include <stdio.h>

struct date

{

int month;

int day;

int year;

} date_1;

long int funct1 (int y,int m)

{

long int result;

if ( m <= 2 )

y -= 1;

result = y;

return (result);

}

long int funct2 (int m)

{

long int result;

if ( m <= 2 )

result = m + 13;

else

result = m + 1;

return(result);

}

long int day_count (int m, int d, int y)

{

long int number;

number = 1461 * funct1(y,m) / 4 + 153 * funct2(m) / 5 + d;

return (number);

}

void main ()

{

long int number_of_days1;

int day_of_week;

printf ("Nhap vao mot ngay (dd mm yyyy), vd 12 03 1999

");

scanf ("%d %d %d", &date_1.day, &date_1.month, &date_1.year);

number_of_days1 = day_count (date_1.month, date_1.day, date_1.year);

printf ("

Ngay la : " );

day_of_week = (number_of_days1 - 621049) % 7;

switch (day_of_week)

{

case 0 :

printf ("Chu Nhat,");

break;

case 1 :

printf ("Thu Hai,");

break;

case 2 :

printf ("Thu Ba,");

break;

case 3 :

printf ("Thu Tu,");

break;

case 4 :

printf ("Thu Nam,");

break;

case 5 :

printf ("Thu Sau,");

break;

case 6 :

printf ("Thu Bay,");

break;

}

getch();

}

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: #hoangnam