hieutiny
12-23-2009, 12:13 AM
/* Bai tap 2 - Minh họa 1 trái banh nay */
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
/* he so doi tu do sang radian */
#define RADS 0.017453293
#define MASS 1.0
double x, y, omega, k, theta, a ;
double ham(double x)
{
return a * fabs(sin(omega*x+theta)) * exp(-k*x);
}
void main()
{
int gr_drive = DETECT, gr_mode;
randomize();
initgraph(&gr_drive, &gr_mode, "");
line(0, getmaxy()-96, getmaxx(), getmaxy()-96);
setfillstyle(SOLID_FILL, RED);
setcolor(RED);
do {
x = 0.0;
omega = M_PI-0.1;
k = random(50) / 100;
theta = M_PI/(random(20)+1);
a = random(100) + 100.0;
y = ham(x);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
do {
setfillstyle(SOLID_FILL, BLACK);
setcolor(BLACK);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
x+=0.001;
y = ham(x);
setfillstyle(SOLID_FILL, RED);
setcolor(RED);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
delay(1);
k += 0.0001;
} while (!kbhit() && x*100<getmaxx()+5);
} while (!kbhit());
getch();
closegraph();
}
************************************************** ******
/* Bai tap 2_14 - Minh họa chuyển động của một bánh xe lăn */
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#pragma warn -pro
float t = 0.0;
float x, y;
void vebanhxe()
{
int x1, y1, x2, y2;
circle(x, y, 20);
x1 = x - 20 * cos(t);
y1 = y - 20 * sin(t);
x2 = x + 20 * cos(t);
y2 = y + 20 * sin(t);
line(x1, y1, x2, y2);
}
void main()
{
int gd = DETECT, gm ;
initgraph(&gd, &gm, "");
line(0, 321, getmaxx(), 321);
x = 100;
y = 300;
while (!kbhit())
{
setcolor(YELLOW);
vebanhxe();
delay(10);
setcolor(BLACK);
vebanhxe();
t += 0.05;
x += 1 ;
if (x > getmaxx() + 20)
x = 0;
}
getch();
closegraph();
}
************************************************** *****
/* Bai tap 3 - Xoay đuờng thẳng*/
#include <conio.h>
#include <graphics.h>
#include <math.h>
/* he so doi tu do sang radian */
#define RADS 0.017453293
void xoay(double x1, double y1, double x2, double y2)
{
double xc, yc;
double goc1, goc2, r1, r2;
xc = getmaxx() / 2;
yc = getmaxy() / 2;
goc1 = atan2(y1 - yc, x1 - xc);
goc2 = atan2(y2 - yc, x2 - xc);
r1 = sqrt((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc));
r2 = sqrt((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc));
do {
setcolor(YELLOW);
line (x1, y1, x2, y2);
setcolor(RED);
line (x1, y1, xc, yc);
line (x2, y2, xc, yc);
setcolor(WHITE);
line(xc - 3, yc, xc + 3, yc);
line(xc, yc - 3, xc, yc + 3);
delay(10);
setcolor(BLACK);
line (x1, y1, x2, y2);
line (x1, y1, xc, yc);
line (x2, y2, xc, yc);
goc1 += 0.01;
goc2 += 0.01;
x1 = xc + r1 * cos(goc1);
y1 = yc + r1 * sin(goc1);
x2 = xc + r2 * cos(goc2);
y2 = yc + r2 * sin(goc2);
} while (!kbhit());
}
void main()
{
int gr_drive = DETECT, gr_mode;
double x1, x2, y1, y2;
printf("\nNhap toa do cua diem dau doan thang : ");
scanf("%lf%lf", &x1, &y1);
printf("\nNhap toa do cua diem cuoi doan thang : ");
scanf("%lf%lf", &x2, &y2);
initgraph(&gr_drive, &gr_mode, "");
xoay(x1, y1, x2, y2);
getch();
closegraph();
}
************************************************** ***
/* Bài tập 4: Pinball trong text mode */
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
char far *video = MK_FP(0xB800, 0);
void writechar(int x, int y, char c)
{
video[y*160+x*2] = c;
}
void main()
{
int xball, yball, dx, dy;
textattr(0x1E);
clrscr();
randomize();
xball = random(80);
yball = random(25);
do {
dx = random(3) - 1;
} while (dx == 0);
do {
dy = random(3) - 1;
} while (dy == 0);
do {
writechar(xball, yball, 4);
delay(100);
writechar(xball, yball, 32);
xball += dx;
yball += dy;
if (xball <= 0 || xball >=79)
dx = -dx;
if (yball <= 0 || yball >=24)
dy = -dy;
} while (!kbhit());
}
************************************************** ******
/* Bai tap 5 - Chuơng tŕnh bắn tàu */
#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("\nNhap vao toa do X cua tau ban (A-Z): ");
x = nhaptoado('A', 'V');
printf("\nNhap 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("\n\rMuon ban o nao, ta nhaptoa do hang roi cot. Vidu AZ");
cprintf(" de ban o o hang Acot Z.\n\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("\n\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("\n\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("\n\rBan da chet!");
done = 1;
}
board[xcomp][ycomp] = 1;
}
luot++;
} while (!done);
cprintf("\n\r\n\rNhan phim de ket thuc");
getch();
window(1, 1, 80, 25);
}
************************************************** ****
/* Bài tập 6 - Bảng mă 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("\n%2d ", i);
printf(" %c", i*16+j);
}
getch();
}
************************************************** **
/* Bài tập 7 - Nhập chuỗi và in chuỗi*/
#include <stdio.h>
#include <conio.h>
void main()
{
char name[80];
printf("\n hieutiny: ");
gets(name);
printf("Chao %s\n", name);
getch();
}
***********************************************
// Bài tập 8- Mạng
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void sinhmatran(int **,int,int);
void nhanmatran(int **,int,int,int **,int,int **);
void main() {
int hang,cot,cotb,i,j;
int **a,**b,**c;
clrscr();
printf(" \\\|/// \n");
printf(" \\ - - // \n");
[Only registered and activated users can see links]
printf(" (_) \n");
printf("nhap so hang of mang a:");
scanf("%d",&hang);
printf("nhap so cot of mang a:");
scanf("%d",&cot);
printf("nhap so cot of mang b:");
scanf("%d",&cotb);
sinhmatran(a,hang,cot);
sinhmatran(b,hang,cotb);
nhanmatran(a,hang,cot,b,cotb,c);
for(i=0;i<hang;i++){
for(j=0;j<cot;j++)
printf("%4d",a[i][j]);
printf("\n");
}
printf("\n\n\n");
for(i=0;i<hang;i++){
for(j=0;j<cotb;j++)
printf("%4d",b[i][j]);
printf("\n");
}
getch();
}
void sinhmatran(int **a,int hang,int cot) {
int i,j;
a=(int **)malloc(hang*cot*sizeof(int));
randomize();
for(i=0;i<hang;i++)
for(j=0;j<cot;j++)
a[i][j]=random(101);
}
void nhanmatran(int **a,int hang,int cot,int **b,int cotb,int **c) {
int i,j,k;
for(i=0;i<hang;i++)
for(j=0;j<cot;j++) {
c[i][j]=0.0;
for(k=0;k<cotb;k++)
c[i][j]=a[i][k]*b[k][j];
}
}
************************************************** ******
//Bài tập 9- T́m tất cả các uớc số nguyên tố of số N
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int kt(int);
void main() {
int i,n;
// int *a;
printf("nhap N:");
scanf("%d",&n);
// a=(int*)malloc(n*sizeof(int));// mang chua uoc so
// j=0;
for(i=2;i<n;i++)
if (n%i==0) {
if (kt(i)==0)
printf("%d ",i);
}
getch();
}
int kt(int a) {
int i;
for(i=2;i<a;i++)
if (a%i==0)
return 1;
return 0;
}
************************************************** *****
// Bài tập 10- Tính tổng 2 số của 1 số có 2 chữ số
#include<conio.h>
#include<stdio.h>
void main() {
int a,b,n,i;
printf("nhap so");
scanf("%d",&n);
a=n/10+n%10;
printf("tong:%d",a);
getch();
}
************************************************** **
//Bài tập 11- XoayMT 90.c
#include <stdio.h>
#define n 3
typedef struct
{ char ten[30];
int tuoi ;
float diem ;
} kieu_HV ;
void main( )
{ int i ; float tam ; kieu_HV hv;
/* nh?p d? li?u cách 1*/
clrscr();
for ( i = 0 ; i < n ; i++)
{ printf ("\n Nhap so lieu cho hoc vien thu %d", i ) ;
printf (" Ho va ten = " ) ; gets ( hv.ten);
printf ("tuii = "); scanf ( "%d" , &hv.tuoi);
printf("diem = "); scanf ("%f*c", &tam ); hv.diem = tam ;
}}
************************************************** *****
Ban cung
/* Skeleton */
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
#include <stdlib.h>
/* 1 / sqrt(2) */
#define FCT 0.7071067
/* he so doi tu do sang radian */
#define RADS 0.017453293
#define NUMBALL 5
#define INTR 0X1C /* Ngat thoi gian */
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt ( *oldhandler)(__CPPARGS);
int count=0;
void interrupt handler(__CPPARGS)
{
count++;
oldhandler();
}
/* Bien toan cuc */
int numhit = 0;
int banhcon = NUMBALL;
int yten, xten;
int xbong, ybong;
void drawhit()
{
char s[3];
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(65, getmaxy()-19, 90, getmaxy()-1);
sprintf(s, "%d", numhit);
setcolor(GREEN);
outtextxy(68, getmaxy()-13, s);
}
void drawbong(int color, int huong)
{
setcolor(color);
setfillstyle(SOLID_FILL, color);
fillellipse(xbong, ybong, 6, 8);
if (huong)
{
line(xbong, ybong+10, xbong-4, ybong+12);
line(xbong, ybong+9, xbong-4, ybong+12);
}
else
{
line(xbong, ybong+10, xbong+4, ybong+12);
line(xbong, ybong+9, xbong+4, ybong+12);
}
}
void drawcon()
{
char s[3];
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(140, getmaxy()-19, 180, getmaxy()-1);
sprintf(s, "%d", banhcon);
setcolor(GREEN);
outtextxy(145, getmaxy()-13, s);
}
void drawten(int color)
{
setcolor(color);
line(xten, yten, xten+13, yten);
line(xten+9, yten-3, xten+13, yten);
line(xten+9, yten+3, xten+13, yten);
}
void run()
{
char c, ban;
int huong, tocdo;
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
line(0, getmaxy()-20, getmaxx(), getmaxy()-20);
outtextxy(10, getmaxy()-13, "Trung :");
outtextxy(100, getmaxy()-13, "Con :");
drawhit();
yten = 5;
xten = 3;
drawten(WHITE);
ban = 0;
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
huong = 1;
drawbong(RED, huong);
drawcon();
randomize();
tocdo = random(3) + 1;
do {
if (kbhit() && !ban)
{
c = getch();
if (c == 0)
c = getch();
drawten(BLACK);
switch (c)
{
case 'H' : if (yten > 5)
yten -= 5;
break;
case 'P' : if (yten < getmaxy()-25)
yten += 5;
break;
case 32 : ban = 1;
}
drawten(WHITE);
}
if (ban)
{
drawten(BLACK);
xten +=3;
drawten(WHITE);
delay(10);
if ((abs(xbong-xten-13)<4 && abs(ybong-yten)<8))
{
drawbong(BLACK, huong);
ban = 0;
drawten(BLACK);
xten = 3;
drawten(WHITE);
numhit ++;
drawhit();
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
tocdo = random(3) + 1;
drawbong(RED, huong);
banhcon--;
drawcon();
count = 0;
}
else if (xten >= getmaxx() - 16)
{
ban = 0;
drawten(BLACK);
xten = 3;
drawten(WHITE);
}
}
if (count > 1)
{
count = 0;
drawbong(BLACK, huong);
ybong -= tocdo;
if (huong)
huong = 0;
else
huong = 1;
drawbong(RED, huong);
}
if (ybong < 10)
{
drawbong(BLACK, huong);
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
drawbong(RED, huong);
banhcon--;
drawcon();
count = 0;
tocdo = random(3) + 1;
}
} while (c != 27 && banhcon > 0);
}
void main()
{
int gr_drive = DETECT, gr_mode;
oldhandler = getvect(INTR);
setvect(INTR, handler);
initgraph(&gr_drive, &gr_mode, "");
run();
getch();
closegraph();
setvect(INTR, oldhandler);
}
Đuối!!!:(11):
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
/* he so doi tu do sang radian */
#define RADS 0.017453293
#define MASS 1.0
double x, y, omega, k, theta, a ;
double ham(double x)
{
return a * fabs(sin(omega*x+theta)) * exp(-k*x);
}
void main()
{
int gr_drive = DETECT, gr_mode;
randomize();
initgraph(&gr_drive, &gr_mode, "");
line(0, getmaxy()-96, getmaxx(), getmaxy()-96);
setfillstyle(SOLID_FILL, RED);
setcolor(RED);
do {
x = 0.0;
omega = M_PI-0.1;
k = random(50) / 100;
theta = M_PI/(random(20)+1);
a = random(100) + 100.0;
y = ham(x);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
do {
setfillstyle(SOLID_FILL, BLACK);
setcolor(BLACK);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
x+=0.001;
y = ham(x);
setfillstyle(SOLID_FILL, RED);
setcolor(RED);
fillellipse(x*100, getmaxy()-100-y, 4, 4);
delay(1);
k += 0.0001;
} while (!kbhit() && x*100<getmaxx()+5);
} while (!kbhit());
getch();
closegraph();
}
************************************************** ******
/* Bai tap 2_14 - Minh họa chuyển động của một bánh xe lăn */
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#pragma warn -pro
float t = 0.0;
float x, y;
void vebanhxe()
{
int x1, y1, x2, y2;
circle(x, y, 20);
x1 = x - 20 * cos(t);
y1 = y - 20 * sin(t);
x2 = x + 20 * cos(t);
y2 = y + 20 * sin(t);
line(x1, y1, x2, y2);
}
void main()
{
int gd = DETECT, gm ;
initgraph(&gd, &gm, "");
line(0, 321, getmaxx(), 321);
x = 100;
y = 300;
while (!kbhit())
{
setcolor(YELLOW);
vebanhxe();
delay(10);
setcolor(BLACK);
vebanhxe();
t += 0.05;
x += 1 ;
if (x > getmaxx() + 20)
x = 0;
}
getch();
closegraph();
}
************************************************** *****
/* Bai tap 3 - Xoay đuờng thẳng*/
#include <conio.h>
#include <graphics.h>
#include <math.h>
/* he so doi tu do sang radian */
#define RADS 0.017453293
void xoay(double x1, double y1, double x2, double y2)
{
double xc, yc;
double goc1, goc2, r1, r2;
xc = getmaxx() / 2;
yc = getmaxy() / 2;
goc1 = atan2(y1 - yc, x1 - xc);
goc2 = atan2(y2 - yc, x2 - xc);
r1 = sqrt((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc));
r2 = sqrt((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc));
do {
setcolor(YELLOW);
line (x1, y1, x2, y2);
setcolor(RED);
line (x1, y1, xc, yc);
line (x2, y2, xc, yc);
setcolor(WHITE);
line(xc - 3, yc, xc + 3, yc);
line(xc, yc - 3, xc, yc + 3);
delay(10);
setcolor(BLACK);
line (x1, y1, x2, y2);
line (x1, y1, xc, yc);
line (x2, y2, xc, yc);
goc1 += 0.01;
goc2 += 0.01;
x1 = xc + r1 * cos(goc1);
y1 = yc + r1 * sin(goc1);
x2 = xc + r2 * cos(goc2);
y2 = yc + r2 * sin(goc2);
} while (!kbhit());
}
void main()
{
int gr_drive = DETECT, gr_mode;
double x1, x2, y1, y2;
printf("\nNhap toa do cua diem dau doan thang : ");
scanf("%lf%lf", &x1, &y1);
printf("\nNhap toa do cua diem cuoi doan thang : ");
scanf("%lf%lf", &x2, &y2);
initgraph(&gr_drive, &gr_mode, "");
xoay(x1, y1, x2, y2);
getch();
closegraph();
}
************************************************** ***
/* Bài tập 4: Pinball trong text mode */
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
char far *video = MK_FP(0xB800, 0);
void writechar(int x, int y, char c)
{
video[y*160+x*2] = c;
}
void main()
{
int xball, yball, dx, dy;
textattr(0x1E);
clrscr();
randomize();
xball = random(80);
yball = random(25);
do {
dx = random(3) - 1;
} while (dx == 0);
do {
dy = random(3) - 1;
} while (dy == 0);
do {
writechar(xball, yball, 4);
delay(100);
writechar(xball, yball, 32);
xball += dx;
yball += dy;
if (xball <= 0 || xball >=79)
dx = -dx;
if (yball <= 0 || yball >=24)
dy = -dy;
} while (!kbhit());
}
************************************************** ******
/* Bai tap 5 - Chuơng tŕnh bắn tàu */
#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("\nNhap vao toa do X cua tau ban (A-Z): ");
x = nhaptoado('A', 'V');
printf("\nNhap 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("\n\rMuon ban o nao, ta nhaptoa do hang roi cot. Vidu AZ");
cprintf(" de ban o o hang Acot Z.\n\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("\n\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("\n\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("\n\rBan da chet!");
done = 1;
}
board[xcomp][ycomp] = 1;
}
luot++;
} while (!done);
cprintf("\n\r\n\rNhan phim de ket thuc");
getch();
window(1, 1, 80, 25);
}
************************************************** ****
/* Bài tập 6 - Bảng mă 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("\n%2d ", i);
printf(" %c", i*16+j);
}
getch();
}
************************************************** **
/* Bài tập 7 - Nhập chuỗi và in chuỗi*/
#include <stdio.h>
#include <conio.h>
void main()
{
char name[80];
printf("\n hieutiny: ");
gets(name);
printf("Chao %s\n", name);
getch();
}
***********************************************
// Bài tập 8- Mạng
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void sinhmatran(int **,int,int);
void nhanmatran(int **,int,int,int **,int,int **);
void main() {
int hang,cot,cotb,i,j;
int **a,**b,**c;
clrscr();
printf(" \\\|/// \n");
printf(" \\ - - // \n");
[Only registered and activated users can see links]
printf(" (_) \n");
printf("nhap so hang of mang a:");
scanf("%d",&hang);
printf("nhap so cot of mang a:");
scanf("%d",&cot);
printf("nhap so cot of mang b:");
scanf("%d",&cotb);
sinhmatran(a,hang,cot);
sinhmatran(b,hang,cotb);
nhanmatran(a,hang,cot,b,cotb,c);
for(i=0;i<hang;i++){
for(j=0;j<cot;j++)
printf("%4d",a[i][j]);
printf("\n");
}
printf("\n\n\n");
for(i=0;i<hang;i++){
for(j=0;j<cotb;j++)
printf("%4d",b[i][j]);
printf("\n");
}
getch();
}
void sinhmatran(int **a,int hang,int cot) {
int i,j;
a=(int **)malloc(hang*cot*sizeof(int));
randomize();
for(i=0;i<hang;i++)
for(j=0;j<cot;j++)
a[i][j]=random(101);
}
void nhanmatran(int **a,int hang,int cot,int **b,int cotb,int **c) {
int i,j,k;
for(i=0;i<hang;i++)
for(j=0;j<cot;j++) {
c[i][j]=0.0;
for(k=0;k<cotb;k++)
c[i][j]=a[i][k]*b[k][j];
}
}
************************************************** ******
//Bài tập 9- T́m tất cả các uớc số nguyên tố of số N
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int kt(int);
void main() {
int i,n;
// int *a;
printf("nhap N:");
scanf("%d",&n);
// a=(int*)malloc(n*sizeof(int));// mang chua uoc so
// j=0;
for(i=2;i<n;i++)
if (n%i==0) {
if (kt(i)==0)
printf("%d ",i);
}
getch();
}
int kt(int a) {
int i;
for(i=2;i<a;i++)
if (a%i==0)
return 1;
return 0;
}
************************************************** *****
// Bài tập 10- Tính tổng 2 số của 1 số có 2 chữ số
#include<conio.h>
#include<stdio.h>
void main() {
int a,b,n,i;
printf("nhap so");
scanf("%d",&n);
a=n/10+n%10;
printf("tong:%d",a);
getch();
}
************************************************** **
//Bài tập 11- XoayMT 90.c
#include <stdio.h>
#define n 3
typedef struct
{ char ten[30];
int tuoi ;
float diem ;
} kieu_HV ;
void main( )
{ int i ; float tam ; kieu_HV hv;
/* nh?p d? li?u cách 1*/
clrscr();
for ( i = 0 ; i < n ; i++)
{ printf ("\n Nhap so lieu cho hoc vien thu %d", i ) ;
printf (" Ho va ten = " ) ; gets ( hv.ten);
printf ("tuii = "); scanf ( "%d" , &hv.tuoi);
printf("diem = "); scanf ("%f*c", &tam ); hv.diem = tam ;
}}
************************************************** *****
Ban cung
/* Skeleton */
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
#include <stdlib.h>
/* 1 / sqrt(2) */
#define FCT 0.7071067
/* he so doi tu do sang radian */
#define RADS 0.017453293
#define NUMBALL 5
#define INTR 0X1C /* Ngat thoi gian */
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt ( *oldhandler)(__CPPARGS);
int count=0;
void interrupt handler(__CPPARGS)
{
count++;
oldhandler();
}
/* Bien toan cuc */
int numhit = 0;
int banhcon = NUMBALL;
int yten, xten;
int xbong, ybong;
void drawhit()
{
char s[3];
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(65, getmaxy()-19, 90, getmaxy()-1);
sprintf(s, "%d", numhit);
setcolor(GREEN);
outtextxy(68, getmaxy()-13, s);
}
void drawbong(int color, int huong)
{
setcolor(color);
setfillstyle(SOLID_FILL, color);
fillellipse(xbong, ybong, 6, 8);
if (huong)
{
line(xbong, ybong+10, xbong-4, ybong+12);
line(xbong, ybong+9, xbong-4, ybong+12);
}
else
{
line(xbong, ybong+10, xbong+4, ybong+12);
line(xbong, ybong+9, xbong+4, ybong+12);
}
}
void drawcon()
{
char s[3];
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(140, getmaxy()-19, 180, getmaxy()-1);
sprintf(s, "%d", banhcon);
setcolor(GREEN);
outtextxy(145, getmaxy()-13, s);
}
void drawten(int color)
{
setcolor(color);
line(xten, yten, xten+13, yten);
line(xten+9, yten-3, xten+13, yten);
line(xten+9, yten+3, xten+13, yten);
}
void run()
{
char c, ban;
int huong, tocdo;
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
line(0, getmaxy()-20, getmaxx(), getmaxy()-20);
outtextxy(10, getmaxy()-13, "Trung :");
outtextxy(100, getmaxy()-13, "Con :");
drawhit();
yten = 5;
xten = 3;
drawten(WHITE);
ban = 0;
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
huong = 1;
drawbong(RED, huong);
drawcon();
randomize();
tocdo = random(3) + 1;
do {
if (kbhit() && !ban)
{
c = getch();
if (c == 0)
c = getch();
drawten(BLACK);
switch (c)
{
case 'H' : if (yten > 5)
yten -= 5;
break;
case 'P' : if (yten < getmaxy()-25)
yten += 5;
break;
case 32 : ban = 1;
}
drawten(WHITE);
}
if (ban)
{
drawten(BLACK);
xten +=3;
drawten(WHITE);
delay(10);
if ((abs(xbong-xten-13)<4 && abs(ybong-yten)<8))
{
drawbong(BLACK, huong);
ban = 0;
drawten(BLACK);
xten = 3;
drawten(WHITE);
numhit ++;
drawhit();
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
tocdo = random(3) + 1;
drawbong(RED, huong);
banhcon--;
drawcon();
count = 0;
}
else if (xten >= getmaxx() - 16)
{
ban = 0;
drawten(BLACK);
xten = 3;
drawten(WHITE);
}
}
if (count > 1)
{
count = 0;
drawbong(BLACK, huong);
ybong -= tocdo;
if (huong)
huong = 0;
else
huong = 1;
drawbong(RED, huong);
}
if (ybong < 10)
{
drawbong(BLACK, huong);
ybong = getmaxy() - 40;
xbong = getmaxx() - 10;
drawbong(RED, huong);
banhcon--;
drawcon();
count = 0;
tocdo = random(3) + 1;
}
} while (c != 27 && banhcon > 0);
}
void main()
{
int gr_drive = DETECT, gr_mode;
oldhandler = getvect(INTR);
setvect(INTR, handler);
initgraph(&gr_drive, &gr_mode, "");
run();
getch();
closegraph();
setvect(INTR, oldhandler);
}
Đuối!!!:(11):