Thứ Sáu, 5 tháng 2, 2016

Chương trình xuất ma trận theo chiều xoắn ốc.

Chú ý: Xoắn ốc theo chiều xuống, phải, lên trái.

#include<stdio.h>
#include<conio.h>
#define ROW 12
#define COL 12

void Nhapmn(int &m, int &n)
{
do
{
printf("\nNhap hang: ");
scanf("%d", &m);
printf("\nNhap cot: ");
scanf("%d", &n);
} while ((m < 1 || m>12) && (n<1 || n > 12));
}

void MakeSpiral(int a[ROW][COL], int m, int n)
{
int maxhang, maxcot, minhang, mincot, hang, cot;
int check = 0;
maxhang = m - 1;
maxcot = n - 1;
minhang = mincot = 0;
hang = cot = 0;

for (int i = 0; i < m*n; i++)
{
a[hang][cot] = i + 1;
switch (check)
{
case 0:
if (hang < maxhang)
hang++;
else
{
mincot++;
cot++;
check++;
}
break;
case 1:
if (cot < maxcot)
cot++;
else
{
maxhang--;
hang--;
check++;
}
break;
case 2:
if (hang>minhang)
hang--;
else
{
maxcot--;
cot--;
check++;
}
break;
case 3:
if (cot > mincot)
cot--;
else
{
minhang++;
hang++;
check = 0;
}
break;
}
}
}

void xuat(int a[ROW][COL], int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("\t%d", a[i][j]);
printf("\n");
}
}

void main()
{
int a[ROW][COL], m, n;
Nhapmn(m, n);
MakeSpiral(a, m, n);
xuat(a, m, n);
getch();
}

Thứ Sáu, 29 tháng 1, 2016

Cách sử dụng hàm gotoxy() trong Visual Studio

Hiện tại trong Visual Studio có những hàm của Borland C không thể sử dụng được bởi vì nó quá cũ, không được hỗ trợ trong các thư viện chuẩn của VS nữa, trong đó gotoxy(). Các bạn chỉ cần khai báo thư viện:

#include<windows.h>

Và sau đó thêm cái hàm này vào:

void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = { x, y };
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}

Rồi các bạn sử dụng hàm gotoxy() bình thường :D

Thứ Ba, 26 tháng 1, 2016

Cách gõ tiếng việt trong Borland C

Hướng dẫn sau đây chỉ hoạt động được trên Borland C hoặc Turbo C nền Dos, tức là phải chạy thông qua phần mềm giả lập DosBox mình có up ở dưới, vì nếu các bạn chạy BC trên win 7, 8, 10 thì font chữ sẽ bị lỗi và không chạy được trên Visual Studio hay các phần mềm build code C khác nhé !

Trước hết, phải khai báo thư viện dos.h để có thể chạy được.

#include<dos.h>

Sau đó các bạn copy hàm này vào bài code của bạn, nên để nó sau phần khai báo thư viện để mặc định luôn cho dễ làm.

void tiengviet(void)
 {
         int    i, j;
         char    vnfont[16*256]={       
     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     24,24,24,24,24,24,24,248,0,0,0,0,0,0,0,0,
     240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,
     255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,
     0,254,2,3,63,3,3,3,3,3,3,3,255,255,0,0,
     0,254,254,254,254,126,62,30,14,6,254,254,254,254,254,0,
     255,1,1,1,1,1,1,249,249,1,1,1,1,1,1,255,
     0,0,0,0,0,0,24,60,60,24,0,0,0,0,0,0,
     0,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,
     0,254,254,126,62,30,14,6,126,126,126,126,126,254,254,0,
     0,254,254,126,126,126,126,126,6,14,30,62,126,254,254,0,
     0,0,248,4,254,2,3,3,3,3,3,3,255,255,0,0,
     255,128,129,131,135,143,159,191,131,131,131,131,131,128,128,255,
     0,254,2,3,3,255,3,3,3,3,3,3,255,255,0,0,
     0,254,2,115,131,131,131,115,3,3,3,3,255,255,0,0,
     0,254,2,3,195,67,251,67,195,3,3,3,255,255,0,0,
     0,0,64,96,112,120,124,126,124,120,112,96,64,0,0,0,
     0,0,1,3,7,15,31,63,31,15,7,3,1,0,0,0,
     0,254,2,243,139,243,147,139,3,3,3,3,255,255,0,0,
     0,254,2,251,131,243,131,251,3,3,3,3,255,255,0,0,
     255,1,125,125,171,215,239,239,239,215,187,109,85,41,1,255,
     0,254,130,130,130,84,40,16,16,40,68,130,170,214,254,0,
     0,0,0,0,0,0,0,255,24,24,24,24,24,24,24,24,
     0,1,3,2,6,4,12,136,216,80,112,32,0,0,0,0,
     0,0,0,0,8,28,62,127,28,28,28,28,0,0,0,0,
     0,0,0,0,28,28,28,28,127,62,28,8,0,0,0,0,
     0,0,0,0,0,0,24,12,254,12,24,0,0,0,0,0,
     0,0,0,0,0,0,48,96,254,96,48,0,0,0,0,0,
     0,127,126,124,127,111,79,0,0,79,111,127,124,126,127,0,
     0,126,62,30,126,118,114,0,0,114,118,126,30,62,126,0,
     0,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,
     255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,24,60,60,60,24,24,0,24,24,0,0,0,
     0,0,0,102,102,102,36,0,0,0,0,0,0,0,0,0,
     0,0,0,0,108,108,254,108,108,108,254,108,108,0,0,0,
     0,0,24,24,124,198,194,192,124,6,134,198,124,24,24,0,
     0,0,0,0,0,0,194,198,12,24,48,102,198,0,0,0,
     0,0,0,0,56,108,108,56,118,220,204,204,118,0,0,0,
     0,0,0,48,48,48,96,0,0,0,0,0,0,0,0,0,
     0,0,0,0,12,24,48,48,48,48,48,24,12,0,0,0,
     0,0,0,0,48,24,12,12,12,12,12,24,48,0,0,0,
     0,0,0,0,0,0,102,60,255,60,102,0,0,0,0,0,
     0,0,0,0,0,0,24,24,126,24,24,0,0,0,0,0,
     0,0,0,0,0,0,0,0,0,0,24,24,24,48,0,0,
     0,0,0,0,0,0,0,0,254,0,0,0,0,0,0,0,
     0,0,0,0,0,0,0,0,0,0,0,24,24,0,0,0,
     0,0,0,0,2,6,12,24,48,96,192,128,0,0,0,0,
     0,0,0,0,124,198,206,222,246,230,198,198,124,0,0,0,
     0,0,0,0,24,56,120,24,24,24,24,24,126,0,0,0,
     0,0,0,0,124,198,6,12,24,48,96,198,254,0,0,0,
     0,0,0,0,124,198,6,6,60,6,6,198,124,0,0,0,
     0,0,0,0,12,28,60,108,204,254,12,12,30,0,0,0,
     0,0,0,0,254,192,192,192,252,6,6,198,124,0,0,0,
     0,0,0,0,60,96,192,192,252,198,198,198,124,0,0,0,
     0,0,0,0,254,198,6,12,24,48,48,48,48,0,0,0,
     0,0,0,0,124,198,198,198,124,198,198,198,124,0,0,0,
     0,0,0,0,124,198,198,198,126,6,6,12,120,0,0,0,
     0,0,0,0,0,24,24,0,0,0,24,24,0,0,0,0,
     0,0,0,0,0,24,24,0,0,0,24,24,48,0,0,0,
     0,0,0,0,6,12,24,48,96,48,24,12,6,0,0,0,
     0,0,0,0,0,0,0,126,0,0,126,0,0,0,0,0,
     0,0,0,0,96,48,24,12,6,12,24,48,96,0,0,0,
     0,0,0,0,124,198,198,12,24,24,0,24,24,0,0,0,
     0,0,0,0,124,198,198,222,222,222,220,192,124,0,0,0,
     0,0,0,0,16,56,108,198,198,254,198,198,198,0,0,0,
     0,0,0,0,252,102,102,102,124,102,102,102,252,0,0,0,
     0,0,0,0,60,102,194,192,192,192,194,102,60,0,0,0,
     0,0,0,0,248,108,102,102,102,102,102,108,248,0,0,0,
     0,0,0,0,254,102,98,104,120,104,98,102,254,0,0,0,
     0,0,0,0,254,102,98,104,120,104,96,96,240,0,0,0,
     0,0,0,0,60,102,194,192,192,222,198,102,58,0,0,0,
     0,0,0,0,198,198,198,198,254,198,198,198,198,0,0,0,
     0,0,0,0,60,24,24,24,24,24,24,24,60,0,0,0,
     0,0,0,0,30,12,12,12,12,12,204,204,120,0,0,0,
     0,0,0,0,230,102,108,108,120,108,108,102,230,0,0,0,
     0,0,0,0,240,96,96,96,96,96,98,102,254,0,0,0,
     0,0,0,0,198,238,254,254,214,198,198,198,198,0,0,0,
     0,0,0,0,198,230,246,254,222,206,198,198,198,0,0,0,
     0,0,0,0,56,108,198,198,198,198,198,108,56,0,0,0,
     0,0,0,0,252,102,102,102,124,96,96,96,240,0,0,0,
     0,0,0,0,124,198,198,198,198,214,222,124,12,14,0,0,
     0,0,0,0,252,102,102,102,124,108,102,102,231,0,0,0,
     0,0,0,0,124,198,198,96,56,12,198,198,124,0,0,0,
     0,0,0,0,126,126,90,24,24,24,24,24,60,0,0,0,
     0,0,0,0,198,198,198,198,198,198,198,198,124,0,0,0,
     0,0,0,0,198,198,198,198,198,198,108,56,16,0,0,0,
     0,0,0,0,198,198,198,198,214,214,254,124,108,0,0,0,
     0,0,0,0,198,198,108,56,56,56,108,198,198,0,0,0,
     0,0,0,0,102,102,102,102,60,24,24,24,60,0,0,0,
     0,0,0,0,254,198,140,24,48,96,194,198,254,0,0,0,
     0,0,0,0,60,48,48,48,48,48,48,48,60,0,0,0,
     0,0,0,0,128,192,224,112,56,28,14,6,2,0,0,0,
     0,0,0,0,60,12,12,12,12,12,12,12,60,0,0,0,
     0,0,16,56,108,198,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,
     0,0,48,48,24,0,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,0,0,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,224,96,96,120,108,102,102,102,124,0,0,0,
     0,0,0,0,0,0,0,124,198,192,192,198,124,0,0,0,
     0,0,0,0,28,12,12,60,108,204,204,204,118,0,0,0,
     0,0,0,0,0,0,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,56,108,100,96,240,96,96,96,240,0,0,0,
     0,0,0,0,0,0,0,118,204,204,204,124,12,204,120,0,
     0,0,0,0,224,96,96,108,118,102,102,102,230,0,0,0,
     0,0,0,0,24,24,0,56,24,24,24,24,60,0,0,0,
     0,0,0,0,6,6,0,14,6,6,6,6,102,102,60,0,
     0,0,0,0,224,96,96,102,108,120,108,102,230,0,0,0,
     0,0,0,0,56,24,24,24,24,24,24,24,60,0,0,0,
     0,0,0,0,0,0,0,236,254,214,214,214,198,0,0,0,
     0,0,0,0,0,0,0,220,102,102,102,102,102,0,0,0,
     0,0,0,0,0,0,0,124,198,198,198,198,124,0,0,0,
     0,0,0,0,0,0,0,220,102,102,102,124,96,96,240,0,
     0,0,0,0,0,0,0,118,204,204,204,124,12,12,30,0,
     0,0,0,0,0,0,0,220,118,102,96,96,240,0,0,0,
     0,0,0,0,0,0,0,124,198,112,28,198,124,0,0,0,
     0,0,0,0,16,48,48,252,48,48,48,54,28,0,0,0,
     0,0,0,0,0,0,0,204,204,204,204,204,118,0,0,0,
     0,0,0,0,0,0,0,102,102,102,102,60,24,0,0,0,
     0,0,0,0,0,0,0,198,198,214,214,254,108,0,0,0,
     0,0,0,0,0,0,0,198,108,56,56,108,198,0,0,0,
     0,0,0,0,0,0,0,198,198,198,198,126,6,12,248,0,
     0,0,0,0,0,0,0,254,204,24,48,102,254,0,0,0,
     0,0,0,0,14,24,24,24,112,24,24,24,14,0,0,0,
     0,0,0,0,24,24,24,24,0,24,24,24,24,0,0,0,
     0,0,0,0,112,24,24,24,14,24,24,24,112,0,0,0,
     0,0,0,0,118,220,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,0,0,16,56,108,198,198,254,0,0,0,0,
     0,0,0,0,0,0,124,198,192,192,198,124,24,12,248,0,
     0,0,0,0,204,204,0,204,204,204,204,204,118,0,0,0,
     0,0,0,0,108,108,0,120,12,124,204,204,118,0,0,0,
     0,0,48,72,72,48,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,108,108,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,108,108,0,56,24,24,24,24,60,0,0,0,
     0,0,0,24,60,102,0,56,24,24,24,24,60,0,0,0,
     0,0,0,0,108,222,26,26,126,216,216,218,108,0,0,0,
     0,0,0,0,204,204,0,124,198,198,198,198,124,0,0,0,
     0,0,0,48,120,204,0,204,204,204,204,204,118,0,0,0,
     0,0,0,0,198,198,0,198,198,198,198,126,6,12,120,0,
     0,0,24,24,24,124,198,192,192,198,124,24,24,24,0,0,
     0,0,0,56,108,100,96,240,96,96,102,230,188,0,0,0,
     0,0,0,102,102,102,60,24,126,24,60,24,24,0,0,0,
     0,0,0,252,102,102,124,98,102,111,102,102,243,0,0,0,
     0,0,0,0,118,220,0,220,102,102,102,102,102,0,0,0,
     0,0,0,0,0,0,54,108,216,108,54,0,0,0,0,0,
     0,0,0,0,0,0,216,108,54,108,216,0,0,0,0,0,
     0,0,0,0,24,24,24,126,24,24,24,0,126,126,0,0,
     0,0,0,0,96,48,24,12,24,48,96,0,124,124,0,0,
     0,0,0,0,12,24,48,96,48,24,12,0,124,124,0,0,
     24,24,24,60,60,126,126,255,24,24,24,24,24,24,24,24,
     24,24,24,24,24,24,24,24,255,126,126,60,60,24,24,24,
     0,0,0,0,0,64,112,124,255,124,112,64,0,0,0,0,
     0,0,0,0,0,2,14,62,255,62,14,2,0,0,0,0,
     0,224,32,48,48,48,48,48,48,48,48,48,240,240,0,0,
     255,128,128,128,128,128,128,159,159,128,128,128,128,128,128,255,
     192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,
     255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     0,240,24,28,30,30,3,3,3,3,3,3,255,255,0,0,
     0,254,1,0,124,254,254,254,254,124,0,1,254,0,0,0,
     0,0,0,128,128,64,64,64,64,128,128,0,0,0,0,0,
     0,0,0,0,0,0,0,255,192,192,192,192,192,192,192,192,
     0,0,108,56,0,56,108,198,198,254,198,198,198,0,0,0,
     0,16,56,108,0,56,108,198,198,254,198,198,198,0,0,0,
     0,16,56,68,254,102,98,104,120,104,98,102,254,0,0,0,
     0,16,56,68,56,108,198,198,198,198,198,108,56,0,0,0,
     0,0,3,1,58,108,198,198,198,198,198,108,56,0,0,0,
     0,0,3,1,199,198,198,198,198,198,198,198,124,0,0,0,
     0,0,0,0,248,108,102,102,254,102,102,108,248,0,0,0,
     0,0,0,0,108,56,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,56,108,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,56,108,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,56,108,0,124,198,198,198,198,124,0,0,0,
     0,0,0,0,0,3,1,126,198,198,198,198,124,0,0,0,
     0,0,0,0,0,3,1,206,204,204,204,204,118,0,0,0,
     0,0,0,0,12,62,12,60,108,204,204,204,118,0,0,0,
     0,224,96,112,112,112,112,112,112,112,112,112,240,240,0,0,
     0,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,
     0,255,192,192,195,194,223,194,195,192,192,192,255,63,0,0,
     0,255,192,192,192,192,192,192,192,192,192,192,255,63,0,0,
     24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,
     255,255,192,192,192,192,192,192,192,192,192,192,192,192,192,192,
     0,0,192,96,48,0,0,120,12,124,204,204,118,0,0,0,
     0,0,56,8,16,0,0,120,12,124,204,204,118,0,0,0,
     0,0,0,118,220,0,0,120,12,124,204,204,118,0,0,0,
     0,0,6,12,24,0,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,0,0,0,120,12,124,204,204,118,0,48,48,
     54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,
     0,0,96,16,76,56,0,120,12,124,204,204,118,0,0,0,
     0,0,7,1,110,56,0,120,12,124,204,204,118,0,0,0,
     0,0,118,220,108,56,0,120,12,124,204,204,118,0,0,0,
     0,0,12,16,108,56,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,0,0,0,248,24,24,24,24,24,24,24,24,
     255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,255,
     255,255,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     0,7,6,6,6,6,6,6,6,6,6,6,7,1,0,0,
     0,255,0,6,6,12,12,152,216,240,112,0,255,255,0,0,
     192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,255,
     0,0,0,0,108,56,0,120,12,124,204,204,118,0,48,48,
     0,0,192,96,56,108,0,120,12,124,204,204,118,0,0,0,
     0,0,7,1,58,108,0,120,12,124,204,204,118,0,0,0,
     0,0,118,220,56,108,0,120,12,124,204,204,118,0,0,0,
     0,0,6,12,56,108,0,120,12,124,204,204,118,0,0,0,
     0,0,0,0,56,108,0,120,12,124,204,204,118,0,48,48,
     0,0,192,96,48,0,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,
     0,0,56,8,16,0,0,124,198,254,192,198,124,0,0,0,
     0,0,0,118,220,0,0,124,198,254,192,198,124,0,0,0,
     0,0,6,12,24,0,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,0,0,0,124,198,254,192,198,124,0,24,24,
     0,48,24,12,56,108,0,124,198,254,192,198,124,0,0,0,
     0,0,7,1,58,108,0,124,198,254,192,198,124,0,0,0,
     0,0,118,220,56,108,0,124,198,254,192,198,124,0,0,0,
     0,0,6,12,56,108,0,124,198,254,192,198,124,0,0,0,
     0,0,0,0,56,108,0,124,198,254,192,198,124,0,24,24,
     0,0,192,96,48,0,0,56,24,24,24,24,60,0,0,0,
     0,0,56,8,16,0,0,56,24,24,24,24,60,0,0,0,
     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     255,255,192,192,192,192,192,192,192,192,192,192,192,192,192,192,
     255,128,128,128,128,128,128,159,159,128,128,128,128,128,128,255,
     0,0,0,118,220,0,0,56,24,24,24,24,60,0,0,0,
     0,0,6,12,24,0,0,56,24,24,24,24,60,0,0,0,
     0,0,0,0,24,24,0,56,24,24,24,24,60,0,24,24,
     0,0,192,96,48,0,0,124,198,198,198,198,124,0,0,0,
     0,0,0,0,62,96,192,192,254,192,192,96,62,0,0,0,
     0,0,56,8,16,0,0,124,198,198,198,198,124,0,0,0,
     0,0,118,220,0,0,0,124,198,198,198,198,124,0,0,0,
     0,0,6,12,24,0,0,124,198,198,198,198,124,0,0,0,
     0,0,0,0,0,0,0,124,198,198,198,198,124,0,24,24,
     0,0,192,96,56,108,0,124,198,198,198,198,124,0,0,0,
     0,0,7,1,58,108,0,124,198,198,198,198,124,0,0,0,
     0,0,118,220,56,108,0,124,198,198,198,198,124,0,0,0,
     0,0,6,12,56,108,0,124,198,198,198,198,124,0,0,0,
     0,0,0,0,56,108,0,124,198,198,198,198,124,0,24,24,
     0,0,192,96,48,3,1,126,198,198,198,198,124,0,0,0,
     0,0,56,8,16,3,1,126,198,198,198,198,124,0,0,0,
     0,0,118,220,0,3,1,126,198,198,198,198,124,0,0,0,
     0,0,6,12,24,3,1,126,198,198,198,198,124,0,0,0,
     0,0,0,0,0,3,1,126,198,198,198,198,124,0,24,24,
     0,0,192,96,48,0,0,204,204,204,204,204,118,0,0,0,
     0,0,0,0,0,254,0,0,254,0,0,254,0,0,0,0,
     0,0,56,8,16,0,0,204,204,204,204,204,118,0,0,0,
     0,0,118,220,0,0,0,204,204,204,204,204,118,0,0,0,
     0,0,6,12,24,0,0,204,204,204,204,204,118,0,0,0,
     0,0,0,0,0,0,0,204,204,204,204,204,118,0,48,48,
     0,0,192,96,48,3,1,206,204,204,204,204,118,0,0,0,
     0,0,56,8,16,3,1,206,204,204,204,204,118,0,0,0,
     0,0,118,220,0,3,1,206,204,204,204,204,118,0,0,0,
     0,0,6,12,24,3,1,206,204,204,204,204,118,0,0,0,
     0,0,0,0,0,3,1,206,204,204,204,204,118,0,48,48,
     0,0,192,96,48,0,0,198,198,198,198,126,6,12,248,0,
     0,0,56,8,16,0,0,198,198,198,198,126,6,12,248,0,
     0,0,0,118,220,0,0,198,198,198,198,126,6,12,248,0,
     0,0,6,12,24,0,0,198,198,198,198,126,6,12,248,0,
     0,0,0,0,0,0,0,198,198,198,198,126,4,12,251,3,
     0,254,1,0,0,0,0,0,0,0,0,1,254,0,0,0
 };
  textmode(C80);
  asm{
   cli
   mov dx,0x3c4
   mov ax,0x100
   out dx,ax
   mov ax,0x402
   out dx,ax
   mov ax,0x704
   out dx,ax
   mov ax,0x300
   out dx,ax

   mov dx,0x3ce
   mov ax,0x204
   out dx,ax
   mov ax,0x5
   out dx,ax
   mov ax,0x406
   out dx,ax
      }

     for(i=0;i<256;++i)
   for(j=0;j<16;++j)
       pokeb(0xa000, i*32+j, vnfont[i*16+j]);


  asm{
   mov dx,0x3c4
   mov ax,0x100
   out dx,ax
   mov ax,0x302
   out dx,ax
   mov ax,0x304
   out dx,ax
   mov ax,0x300
   out dx,ax

   mov dx,0x3ce
   mov ax,0x4
   out dx,ax
   mov ax,0x1005
   out dx,ax
   mov ax,0xe06
   out dx,ax
   cli
      }

     }

Tiếp sau đó ở trong hàm main, các bạn gọi hàm tiengviet() ngay đầu tiên luôn, và khi gõ tiếng việt thì sử dung bang mã TCVN3 (ABC) nhé, sẽ hiện kiểu kí tự vớ vẩn như "«I dêi ¬I..." trong lúc soạn thảo, nhưng đừng lo, khi debug nó sẽ chuyển sang kí tự tiếng việt đọc được.

Demo:


DosBox - Phần mềm giả lập chạy chương trình trên nền Dos

Đây là một chương trình giả lập chạy các ứng dụng trên nền dos, công dụng như thế nào thì các bạn có thể tự tìm hiểu, còn đây là mình hướng dẫn sử dụng nó để chạy Borland C, vì sao ? Đơn giản bây giờ chúng ta sử dụng hầu hết trên nền windows 7, 8, 10 và hệ thống đồ họa trên C nó chỉ tương tích đối với hđh DOS mà thôi, cho nên nếu ai chạy BC trên các win nói trên mà ko sử dụng đc hệ thống đồ họa thì đừng hỏi vì sao nữa nhé. Vì thế mình phải sử dụng DosBox để gián tiếp sử dụng nền đồ họa của C trên BC.
Link: https://www.mediafire.com/?u
9uz5la45jnndca

Hướng dẫn cách sử dung:
Cài đặt xong mở DosBox lên nó có giao diện y như dos, chỉ cần nhập các dòng lệnh cơ bản để mở BC lên sau đây, sau mỗi 1 dòng các bạn nhấn enter: (Bạn nên để thư mục BC ở ngay ổ D, hoặc ổ E tùy ý, hạn chế đừng để nó sâu quá thì dòng lệnh sẽ nhiều)

mount d d:  (nếu là ổ E thì bạn chỉ cần thay d là e là đc)
d:
cd bc
cd bin
bc

Thứ Bảy, 2 tháng 1, 2016

Chương trình sử dụng đồ họa vẽ đồng hộ kim chạy đúng giờ hệ thống

 Chỉ chạy được trên BC, sử dụng Dosbox để có thể chạy được nền độ họa !

#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>

int x0,y0,rgio,rphut,rgiay,mgio,mphut,mgiay;
unsigned phutgioht,gioht,phutht,giayht;

void ke(int ddo, unsigned dai, unsigned day, unsigned mau)
{
    unsigned x,y;
   float goc;
   while(ddo>=360)
       ddo=ddo-360;
      goc=(M_PI/180)*ddo;
      x=x0+(int)(dai*cos(goc)+0.5);
      y=y0-(int)(dai*sin(goc)+0.5);
      setcolor(mau);
      setlinestyle(0,0,day);
      line(x0,y0,x,y);
}

void ke_giay(unsigned giay)
{
    int ddo;
   ddo=(90-6*giay);
   ke(ddo,rgiay,1,mgiay);
}

void ke_phut(unsigned phut)
{
    int ddo;
   ddo=(90-6*phut);
   ke(ddo,rphut,3,mphut);
}

void ke_gio(unsigned gio, unsigned phut)
{
    int ddo;
   ddo=360+90-30*(gio%12)-(phut+1)/2;
   ke(ddo,rgio,3,mgio);
}

void chay_kim_giay(void)
{
    unsigned giay;
   struct time t;
   gettime(&t);
   giay=t.ti_sec;
   if(giay!=giayht)
   {
       ke_giay(giayht);
      giayht=giay;
      ke_giay(giayht);
   }
}

void chay_kim_phut(void)
{
    unsigned phut;
   struct time t;
   gettime(&t);
   phut=t.ti_min;
   if(phut!=phutht)
   {
       ke_phut(phutht);
      phutht=phut;
      ke_phut(phutht);
   }
}

void chay_kim_gio(void)
{
    unsigned h,gio,phut,sophut,sophutht;
   struct time t;
   gettime(&t);
   gio=t.ti_hour;
   phut=t.ti_min;
   sophut=gio*60+phut;
   sophutht=gioht*60+phutgioht;
    if(sophut<sophutht)
       sophut=sophut+12*60;
      h=sophut-sophutht;
   if(h>=12)
   {
       ke_gio(gioht,phutgioht);
      phutgioht=phut;
      gioht=gio;
      ke_gio(gioht,phutgioht);
   }
}

void main()
{
    struct time t;
   char *dso[]={"","12","1","2","3","4","5","6","7","8","9","10","11"};
   int i, mh=0, mode, r,x,y;
   float goc;
   initgraph(&mh,&mode,"E:\\BC\\BGI");
   x0=(getmaxx()/2)-1;
   y0=(getmaxy()/2)-1;
   r=y0-2;
   rgiay=r-10;
   rphut=r-50;
   rgio=r-90;
   mgiay=BROWN;
   mphut=RED;
   mgio=YELLOW;
   setcolor(BLUE);
   setlinestyle(0,0,3);
   circle(x0,y0,r);
   setfillstyle(1,YELLOW);
   floodfill(0,0,BLUE);
   setfillstyle(1,WHITE);
   floodfill(x0,y0,BLUE);
   setlinestyle(0,0,1);
   circle(x0,y0,10);
   setfillstyle(1,GREEN);
   floodfill(x0,y0,BLUE);
   settextjustify(1,1);
   setcolor(MAGENTA);
   outtextxy(x0,y0+120,"TAH-JR");
   settextstyle(3,0,3);
   settextjustify(1,1);
   setcolor(BLUE);
   for(i=1;i<=12;i++)
   {
       goc=(2*M_PI+M_PI/2)-(i-1)*(M_PI/6);
      x=x0+(int)(rphut*cos(goc)+0.5);
      y=y0-(int)(rphut*sin(goc)+0.5);
      outtextxy(x,y,dso[i]);
   }
   gettime(&t);
   gioht=t.ti_hour;
   phutht=t.ti_min;
   giayht=t.ti_sec;
   phutgioht=phutht;
   setwritemode(XOR_PUT);
   ke_gio(gioht,phutgioht);
   ke_phut(phutht);
   ke_giay(giayht);
   do
   {
       chay_kim_giay();
      chay_kim_phut();
      chay_kim_gio();
   }while(!kbhit());
   closegraph();
}

Thứ Ba, 29 tháng 12, 2015

Chương trình tổng hợp các bài toán cơ bản về cây tìm kiếm nhị phân

#include<stdio.h>
#include<conio.h>

typedef struct tree
{
    int info;
    tree *l;
    tree *r;
};
   
tree *getnode(int x)
{
    tree *p=new tree;
    if(!p) return NULL;
    p->info= x;
    p->l=NULL;
    p->r=NULL;
    return p;
}

void addx(tree *&t, int x)
{
    if(!t)
    {
        t=getnode(x);
        return;
    }
    if(t->info ==x) return;
    if(t->info>x)
        addx(t->l,x);
    else   
        addx(t->r,x);
}

void list(tree *&t)
{
    int n,x;
    do
    {
        printf("\nNhap n: ");
        scanf("%d",&n);
        if(n<0)    printf("\nNhap sai Nhap lai");
    }while(n<0);
   
    for(int i=0; i<n; i++)
    {
        printf("\nNhap x: ");
        scanf("%d",&x);
        addx(t,x);
    }
}

void NLR(tree *t)
{
   
    if(!t) return;
    printf("%5d",t->info);   
    NLR(t->l);
    NLR(t->r);
}

void LNR(tree *t)
{
   
    if(!t) return;
    LNR(t->l);
    printf("%5d",t->info);   
    LNR(t->r);
}

void LRN(tree *t)
{
   
    if(!t) return;
    LRN(t->l);
    LRN(t->r);
    printf("%5d",t->info);   
}

tree *timx(tree *t, int x)
{
    if(!t) return NULL;
    if(t->info==x) return t;
    if(t->info >x)
            return(timx(t->l,x));
    return(timx(t->r,x));
}

tree *timx2(tree *t, int x)
{
    tree *p=t;
    if(!t) return NULL;
    while(p && p->info !=x)
    {
        if(p->info >x)
            p=p->l;
        else p=p->r;
    }
    return p;
}

void calltimx(tree *t)
{
    int x;
    printf("\nNhap x can tim: ");
    scanf("%d",&x);
    if(!t)
        printf("\nCay rong");
    else
    {
        tree *p=timx(t,x);
        if(!p)
            printf("\nKhong tim thay");
        else printf("\nTim thay");
    }
}

int ktsnt(int n)
{
    if(n<2) return 0;
    if(n==2) return 1;
    for(int i =2; i*i<=n; i++)
        if(n%i == 0) return 0;
    return 1;
}

void ktam(tree *t)
{
    if(!t) return;
    if(t->info<0)
        printf("%5d",t->info);
    ktam(t->l);
    ktam(t->r);
}

void ktduong(tree *t)
{
    if(!t) return;
    if(t->info>0)
        printf("%5d",t->info);
    ktduong(t->l);
    ktduong(t->r);
}

void ktchan(tree *t)
{
    if(!t) return;
    if(t->info%2==0)
        printf("%5d",t->info);
    ktchan(t->l);
    ktchan(t->r);
}

void ktle(tree *t)
{
    if(!t) return;
    if(t->info%2!=0)
        printf("%5d",t->info);
    ktle(t->l);
    ktle(t->r);
}

void xuatsnt(tree *t)
{
    if(!t) return;
    if(ktsnt(t->info))
        printf("%5d",t->info);
    xuatsnt(t->l);
    xuatsnt(t->r);
}

int tong(tree *t)
{
    if(!t) return 0;
    return t->info + tong(t->l) +tong(t->r);
}
   
int tongam(tree *t)
{
    if(!t) return 0;
    if(t->info <0)
        return t->info + tongam(t->l) + tongam(t->r);
    return tongam(t->l) + tongam(t->r);
}

int tongduong(tree *t)
{
    if(!t) return 0;
    if(t->info >0)
        return t->info + tongduong(t->l) + tongduong(t->r);
    return tongduong(t->l) + tongduong(t->r);
}

int tongchan(tree *t)
{
    if(!t) return 0;
    if(t->info %2 == 0)
        return t->info + tongchan(t->l) + tongchan(t->r);
    return tongchan(t->l) + tongchan(t->r);
}

int tongle(tree *t)
{
    if(!t) return 0;
    if(t->info %2 != 0)
        return t->info + tongle(t->l) + tongle(t->r);
    return tongle(t->l) + tongle(t->r);
}

int tongsnt(tree *t)
{
    if(!t) return 0;
    if(ktsnt(t->info))
        return t->info + tongsnt(t->l) + tongsnt(t->r);
    return tongsnt(t->l) + tongsnt(t->r);
}

int demnode(tree *t)
{
    if(!t) return 0;
    return 1+ demnode(t->l)+demnode(t->r);
}

int demnodela(tree *t)
{
    if(!t) return 0;
    if(t->l == NULL && t->r == NULL)
        return 1+demnodela(t->l) + demnodela(t->r);
    return demnodela(t->l) + demnodela(t->r);
}

int demnodeam(tree *t)
{
    if(!t) return 0;
    if(t->info <0)
        return 1+ demnodeam(t->l)+ demnodeam(t->r);
    return demnodeam(t->l)+ demnodeam(t->r);
}

int demnodeduong(tree *t)
{
    if(!t) return 0;
    if(t->info >0)
        return 1+ demnodeduong(t->l)+ demnodeduong(t->r);
    return demnodeduong(t->l)+ demnodeduong(t->r);
}

int demnodechan(tree *t)
{
    if(!t) return 0;
    if(t->info %2 == 0)
        return 1 + demnodechan(t->l) + demnodechan(t->r);
    return demnodechan(t->l) + demnodechan(t->r);
}

int demnodele(tree *t)
{
    if(!t) return 0;
    if(t->info %2 != 0)
        return 1 + demnodele(t->l) + demnodele(t->r);
    return demnodele(t->l) + demnodele(t->r);
}

int demnodesnt(tree *t)
{
    if(!t) return 0;
    if(ktsnt(t->info))
        return 1 + demnodesnt(t->l) + demnodesnt(t->r);
    return demnodesnt(t->l) + demnodesnt(t->r);
}

int max(tree *t)
{
    tree *p=t;
    while(p->r)
        p=p->r;
    return p->info;
}

int min(tree *t)
{
    tree *p=t;
    while(p->l)
        p=p->l;
    return p->info;
}

tree *nodemax(tree *t)
{
    tree *p=t;
    while(p->r)
        p=p->r;
    return p;
}

tree *nodemin(tree *t)
{
    tree *p=t;
    while(p->l)
        p=p->l;
    return p;
}

int demnodelonx(tree *t, int x)
{
    if(!t) return 0;
    if(t->info >x)
        return 1+demnodelonx(t->l,x) + demnodelonx(t->r,x);
    return demnodelonx(t->l,x) + demnodelonx(t->r,x);
}

int demnodenhox(tree *t, int x)
{
    if(!t) return 0;
    if(t->info <x)
        return 1+demnodenhox(t->l,x) + demnodenhox(t->r,x);
    return demnodenhox(t->l,x) + demnodenhox(t->r,x);
}

int Max(int a, int b)
{
    return a>b?a:b;
}

int height(tree *t)
{
    if(t)
        return 1+ Max(height(t->l), height(t->r));
    return 0;
}

int xuatxy(tree *t, int x, int y)
{
    if(!t) return 0;
    if(t->info>=x && t->info <=y)
        return 1+xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
    return xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
}

void xuatxy1(tree *t)
{
    int x,y;
    printf("\nNhap x: ");
    scanf("%d",&x);
    printf("\nNhap y: ");
    scanf("%d",&y);
    printf("\nSo nut tu x den y trong cay la: %d",xuatxy(t,x,y));
}


void deltree(tree *&t)
{
    if(!t) return;
    deltree(t->l);
    deltree(t->r);
    delete t;
}
   


void main(void)
{
    tree *t =NULL;
    list(t);
    printf("\nDuyet NLR: ");
    NLR(t);
    printf("\nDuyet LNR: ");
    LNR(t);
    printf("\nDuyet LRN: ");
    LRN(t);
    calltimx(t);
    printf("\nCac so duong: ");
    ktduong(t);
    printf("\nCac so am: ");
    ktam(t);
    printf("\nCac so chan: ");
    ktchan(t);
    printf("\nCac so le: ");
    ktle(t);
    printf("\nCac so nguyen to: ");
    xuatsnt(t);
    printf("\nTong cac so  duong trong cay: %d",tongduong(t));
    printf("\nTong cac so le trong cay: %d",tongle(t));
    printf("\nTong cac so chan trong cay: %d",tongchan(t));
    printf("\nTong cac so am trong cay: %d",tongam(t));
    printf("\nTong cac so nt trong cay: %d",tongsnt(t));
    printf("\nTong so node tren cay: %d",demnode(t));
    printf("\nTong so node la tren cay: %d",demnodela(t));
    printf("\nTong cac so node duong trong cay: %d",demnodeduong(t));
    printf("\nTong cac so node le trong cay: %d",demnodele(t));
    printf("\nTong cac so node chan trong cay: %d",demnodechan(t));
    printf("\nTong cac so node am trong cay:%d",demnodeam(t));
    printf("\nTong cac so node nt trong cay: %d",demnodesnt(t));
    printf("\nMax: %d",max(t));
    printf("\nMin: %d",min(t));
    int x;
    printf("\nNhap x can tim: ");
    scanf("%d",&x);
    printf("\nTong cac node lon hon x: %d",demnodelonx(t,x));
    printf("\nTong cac node nho hon x: %d",demnodenhox(t,x));
    printf("\nChieu cao cua cay: %d",height(t));
    xuatxy(t);
    deltree(t);
    if(t)
        printf("\nCay da xoa !");
    xuatxy1(t);
    getch();
}

Thứ Sáu, 25 tháng 12, 2015

Chương trình mô phỏng bài toán Tháp Hà Nội sử dụng đệ quy.



Lưu ý: Code này chỉ chạy được trên Borland C !

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

#define move(x,y,z) makeblocks(x,y,z); delay(t); delblocks(x, y, z);

int t;
int a[] = { 0, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
int color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int bg = 0;


void makeblocks(int x, int y, int z)
{
    int h = a[z] / 2;
    window(x - h, y, x + h, y);
    textbackground(color[z]);
    textcolor(color[z]);
    clrscr();
}

void delblocks(int x, int y, int z)
{
    int h = a[z] / 2;
    window(x - h, y, x + h, y);
    textbackground(bg);
    clrscr();
}

void tower(int x, int y, int m)
{
    for (int i = m; i >= 1; i--)
        makeblocks(x, y - m + i, i);
}

void moveblocks(int x1, int y1, int x2, int y2, int z)
{
    static int count = 0;
    int x, y;
    count++;
    window(1, 1, 24, 1);
    textbackground(BLUE);
    clrscr();
    textcolor(WHITE);
    cprintf("\nLan dich chuyen thu: ");
    textcolor(YELLOW + BLINK);
           cprintf("%4d", count);
    for (y = y1; y >= 4; y--)
    {
        move(x1, y, z);
        }
    if (x1<x2)
    for (x = x1; x <= x2; x++)
    {
        move(x, 4, z);
    }
    else
    for (x = x1; x >= x2; x--)
    {
        move(x, 4, z );
    }
    for (y = 4; y <= y2; y++)
    {
        move(x2, y, z);
    }
    makeblocks(x2, y2,z);
}

void movetower(int x1, int y1, int x2, int y2, int x3, int y3, int m)
{
    if (m < 1)    return;
    else
    if (m == 1)
        moveblocks(x1, y1, x2, y2, 1);
    else
    {
        movetower(x1, y1 - 1, x3, y3, x2, y2, m - 1);
        moveblocks(x1, y1, x2, y2, m);
        movetower(x3, y3, x2, y2 - 1, x1, y1, m - 1);
    }
}

void main(void)
{
    int m;
    int x1, y1, x2, y2, x3, y3;
    x1 = 13;
    x2 = 40;
    x3 = 67;
    y1 = y2 = y3 = 24;
    textmode(C80);
    clrscr();
    printf("\nSo tang: ");
    scanf("%d", &m);
    printf("\nTime delay: ");
    scanf("%d", &t);
    textbackground(bg);
    clrscr();
    tower(x1, y1, m);
        getch();
    movetower(x1, y1, x2, y2, x3, y3, m);
    getch();
    window(1, 1, 80, 25);
    textbackground(bg);
    textcolor(WHITE);
    clrscr();

}

Thứ Năm, 24 tháng 12, 2015

Chương trình xử lí chuỗi cơ bản.


#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void info(char a[], int &leng)
{
    char ten[20], que[20];
    int i = 0;
    leng = 0;
    printf("\nNhap ten: ");
    gets(ten);
    printf("\nNhap que: ");
    gets(que);

    while (ten[i] != '\0')
    {
        if (ten[i] != ' ')
            a[leng++] = tolower(ten[i++]);
        else
        {
            i++;
            continue;
        }
    }
    i = 0;
    while (que[i] != '\0')
    {
        if (que[i] != ' ')
            a[leng++] = tolower(que[i++]);
        else
        {
            i++;
            continue;
        }
    }
    a[leng] = '\0';

    printf("\nChuyen het thanh 1 day ki tu thuong: %s", a);
}

void swap(char &a, char &b)
{
    char c = a;
    a = b;
    b = c;
}

void sxtang(char a[], int leng)
{
    for (int i = 0; i < leng - 1; i++)
        for (int j = i + 1; j < leng; j++)
            if (a[i]>a[j])
                swap(a[i], a[j]);
    printf("\nDay ki sau khi sx tang: %s", a);
}

int dem(char a[], char x)
{
    int dem = 0, i = 0;
    while (a[i] != '\0')
    {
        if (a[i++] == x)
            dem++;
    }
    return dem;
}

void solanxuathien(char a[], int leng)
{
    int x;
    int max = dem(a, a[0]);
    for (int i = 0; i < leng; i++)
    {
        if (a[i] == a[i + 1])
            continue;
        x = dem(a, a[i]);
        printf("\n%c xuat hien %d lan", a[i], x);
          
        if (max < x)
            max = x;
    }

    for (int i = 0; i < leng; i++)
    {
        if (a[i] == a[i + 1])
            continue;
        x = dem(a, a[i]);
        if (x == max)
            printf("\nKi tu xuat hien nhieu nhat la: %c", a[i]);
    }
}

void chuyenhoa(char a[], int leng)
{
    for (int i = 0; i < leng; i++)
        a[i] = toupper(a[i]);
    printf("\nChuoi chuyen thanh ki tu hoa: %s", a);
}

void main(void)
{
    char a[30];
    int leng;
    info(a, leng);
    sxtang(a, leng);
    solanxuathien(a, leng);
    chuyenhoa(a, leng);
    getch();
}

Thứ Sáu, 18 tháng 12, 2015

MS Visual Studio 2013 - Trình build code chuyên nghiệp

Khỏi phải bàn nhiều về thằng này, sản phẩm của ông lớn MS có thể code trên nhiều ngôn ngữ, đặc biệt là C và C++, dưới đây mình share link bản Ultimate 2013 chạy trên win 7, hiện tại mình đang dùng thì rất ổn, ai muốn thì có thể tìm đến bản 2015 nhưng yêu cần phải là win 8.1 trở lên, RAM tối thiểu là 2gb nhưng 4 gb mới ổn định, bạn nào vẫn còn một dạ với XP thì hay tìm các bản VS 2010 trở xuống nhé. Link từ MS nên không phải lo cái gì vớ vẩn.

Link: http://download.microsoft.com/download/C/F/B/CFBB5FF1-0B27-42E0-8141-E4D6DA0B8B13/VS2013_RTM_ULT_ENU.iso

Viết chương trình sử dụng danh sách liên kết đơn xử lí các công việc sau.

Công việc:
1. Nhập/xuất random danh sách các số nguyên gồm n số, mỗi số mới thêm vào cuối dánh sách.
2. Thêm vào đầu 1 số random.
3. Xóa số đầu danh sách.
4. Xóa 1 số sau số X đầu tiên tùy chọn (nếu có)
5. Xóa hết các số X tùy chọn.
6. Xóa 1 số sau số MIN đầu tiên (nếu được)
7. Xóa các số sau số MAX (nếu được)
8. Xóa hết các số MAX.
9. Xóa số gần kế cuối (nếu được)

CODE NÀY CHẠY CHUẨN TRÊN Visual Studio, NẾU CHẠY TRÊN Borland C THÌ SẼ PHẢI FIX MỘT VÀI CHỖ !


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct node
{
int info;
node *next;
};

node *getnode(int x)
{
node *p = new node;
if (!p) return NULL;
p->info = x;
p->next = NULL;
return p;
}

void addtail(node *&h, node *&t, int x)
{
node *p = getnode(x);
if (!h) h = t = p;
else
{
t->next = p;
t = p;
}
}

void addhead(node *&h, node *&t, int x)
{
node *p = getnode(x);
if (!h) h = t = p;
else
{
p->next = h;
h = p;
}
}

void delhead(node*&h, node *&t)
{
if (!h) return;
node *p = h;
h = h->next;
delete p;
if (!h) t = NULL;
}

node *search(node *h, int x)
{
node *p = h;
while (p && p->info != x)
p = p->next;
return p;
}

void delafterX(node *q, node *&t)
{
node *p = q->next;
q->next = p->next;
delete p;
if (q->next == NULL)
t = q;
}

void delX(node *&h, node *&t, int x)
{
if (h->info == x)
{
delhead(h, t);
return;
}
node *p = h;
while (p->next != NULL && p->next->info != x)
p = p->next;
if (p->next->info == x)
delafterX(p, t);
}

int min(node *h)
{
int min = h->info;
for (node *p = h; p; p=p->next)
if (min > p->info)
min = p->info;
return min;
}

int max(node *h)
{
int max = h->info;
for (node *p = h; p; p = p->next)
if (max < p->info)
max = p->info;
return max;
}

//

void list(node *&h, node *&t)
{
int x, n;
do
{
printf("\nNhap so phan tu: ");
scanf("%d", &n);
if (n < 0)
printf("\nNhap sai, nhap lai.");
} while (n < 0);

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

x = rand() % 10;
addtail(h, t, x);
}

}

void xuat(node *h)
{
printf("\nDanh sach hien tai: ");
if (!h)
{
printf("Rong!");
return;
}

for (node *p = h; p; p = p->next)
printf("%5d", p->info);
}

void themdau(node *&h, node *&t)
{
int x;
printf("\nNhap so muon them vao dau: ");
scanf("%d", &x);
addhead(h, t, x);
}

void xoadau(node *&h, node *&t)
{
printf("\nDa xoa 1 phan tu dau danh sach !");
delhead(h, t);
}

void xoasauX(node *h, node *&t)
{
int x;
node *q;
printf("\nNhap so can tim: ");
scanf("%d", &x);
q = search(h, x);
if (q && q != t)
{
printf("\nDa xoa phan tu sau X !");
delafterX(q, t);
}
else printf("\nKhong tim thay hoac X la phan tu cuoi cung !");
}

void xoahetX(node *&h, node *&t)
{
int x;
printf("\nNhap so can tim: ");
scanf("%d", &x);
node *p = h;
while (p)
{
if (p->info == x)
{
p = p->next;
delX(h, t, x);
continue;
}
p = p->next;
}
}

void xoasaumin(node *h, node *&t)
{
int Min = min(h);
node *p = search(h, Min);
if (p && p != t)
{
printf("\nDa xoa 1 phan tu sau min dau tien!");
delafterX(p, t);
}
else printf("\nMin la tail !");

}

void xoahetsaumax(node *h, node *&t)
{
int Max = max(h);
node *p = h;
while (p && p!=t)
{
if (p->info == Max)
delafterX(p, t);
p = p->next;
}
}

void xoahetmax(node *&h, node *&t)
{
int Max = max(h);
node *p = h;
while (p && p != t)
while (p)
{
if (p->info == Max)
{
p = p->next;
delX(h, t, Max);
continue;
}
p = p->next;
}
}

void xoasogancuoi(node *&h, node *t)
{
if (!h && h == t) return;
if (h->next == t)
{
delhead(h, t);
return;
}
node *p = h;
while (p->next->next != t)
p = p->next;
delafterX(p, t);

}

////////////////////////////////////
void main(void)
{
node *h = NULL;
node *t = NULL;
list(h, t);
xuat(h);
themdau(h, t);
xuat(h);

xoadau(h, t);
xuat(h);

xoasauX(h, t);
xuat(h);

xoahetX(h, t);
xuat(h);

xoasaumin(h, t);
xuat(h);

xoahetsaumax(h, t);
xuat(h);

xoahetmax(h, t);
xuat(h);

xoasogancuoi(h, t);
xuat(h);
getch();
}