МІНІСТЕРCТВО ОСВІТИ І НАУКИ УКРАЇНИ
НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ “ЛЬВІВСЬКА ПОЛІТЕХНІКА”
Кафедра ICM
Лабораторна робота №3
“ Структура програми на мові Сі ”
Виконав:
cтудент гр. КН-1
Львів 2007
Мета роботи: вивчити способи запису програм на мові Сі, ознайомитись з основними конструкціями мови Сі.
Приклад для вивчення структури програми:
/*риклад для вивчення структури програми:
/ /
/* ІГРОВА ПРОГРАМА "SNAKE" */
/* Директиви препроцесора для включення у текст програми
файлів з прототипами бібліотечних функцій */
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include <stdlib.h>
#include <time.h>
/* Директиви препроцесора для визначення макроконстант */
#define GameTime 20 /* Час роботи програми у секундах*/
#define Speed 900 /* Швидкість руху динамічного об'єкта 0..1000 */
/* Оголошення структури */
struct w {char x,y;
struct w *next;
struct w *before;
};
/* Прототипи розроблених функцій */
void cursor_off(void);
void cursor_on(void);
/* Головна функція*/
void main()
{
/* Локальні оголошення */
int x1=10,y1=9,x2=70,y2=19,x,y,mark=0;
char symbol,c,attrib,h=259,refresh;
struct w *head=NULL,*tail, *p, *q,*r;
time_t tl,t2;
time(&tl);
cursor_off();
srand(time(NULL));
/* Побудова статичного зображення екрану*/
textbackground(BLACK);
textcolor(0);
clrscr();
textcolor(10);
/* Тут буде виведено назву гри "SNAKE"*/
gotoxy(20,1); cputs(" *** * * ** * * *****");
gotoxy(20,2); cputs("* * ** * * * * * * ");
gotoxy(20,3); cputs(" * * * * * * ** **** ");
gotoxy(20,4); cputs(" * * * * ****** * * * ");
gotoxy(20,5); cputs("* * * ** * * * * * ");
gotoxy(20,6); cputs(" *** * * * * * * *****");
textcolor(12);
gotoxy(20,21); cputs("Press arrows key for moving the snake.");
gotoxy(7,22); cputs("If the head gets on a symbol'+' the length of the snake is increased.");
gotoxy(7,23); cputs("If the head gets on a symbol'-' the length of the snake decreases.");
gotoxy(15,24); cputs("Try to construct the greatest length of the snake.");
textbackground(3);
textcolor(WHITE);
x=x1-1; y=y1-1; gotoxy(x,y);putch(201);
y=y1-1;for(x=x1;x<=x2;x++){gotoxy(x,y);putch(205);}
x=x2+1; y=y1-1; gotoxy(x,y);putch(187);
x=x2+1;for(y=y1;y<=y2;y++){gotoxy(x,y);putch(186);}
x=x2+1; y=y2+1; gotoxy(x,y);putch(188);
y=y2+1;for(x=x2;x>=x1;x--){gotoxy(x,y);putch(205);}
x=x1-1; y=y2+1; gotoxy(x,y);putch(200);
x=x1-1;for(y=y1;y<=y2;y++){gotoxy(x,y);putch(186);}
window(x1,y1,x2,y2);
clrscr();
for(x=1 ;x<=x2-x1+1 ;x++)
for(y=1 ;y<=y2-y-1+1 ;y++)
if(!(y==y2-y1+1&&x==x2-x1+1))
if((float)rand()/RAND_MAX>0.9)putch('+');
else if((float)rand()/RAND_MAX>0.9) putch('-');
else putch(' ');
/* Виведення початкового зображення динамічного об'єкта */
/* Функції peekb() та pokeb() забезпечують прямий доступ до відеопам'яті */
textcolor(0);
x=wherex();
y=wherey();
attrib=peekb(0xB800,(y1+y-2)*80*2+(x1+x-2)*2+1); // читання з пам'яті
tail=(struct w*) malloc(sizeof(struct w));
tail->x=x;
tail->y=y;
tail->next=head;
head=tail;
pokeb(0xB800,(y1+head->y-2)*80*2+(x1+head->x-2)*2+1,0x30); // запис у пам'ять pokeb(0xB800,(yl+y-2)*80*2+(xl+x-2)*2,h);
/* Зміна зображення динамічного об'єкта, керованого з клавіатури */
do{
if(kbhit())
{
c=getch();
if(c==0)c=getch();
refresh=1;
}
switch(c)
{
case 72: y--;if(y<1) {y=1; refresh=0;} h=30; break;
case 80: y++;if(y>y2-y1+1) {y=y2-y1+1; refresh=0;} h=31; break;
case 75: x--;if(x<1) {x=1; refresh=0;} h=17; break;
case 77: x++;if(x>x2-x1+1) {x=x2-x1+1; refresh=0;} h=16; break;
default: refresh=0;
}
pokeb(0xB800,(y1+head->y-2)*80*2+(x1+head->x-2)*2,h);
if(refresh==1)
{
q=(struct w*) malloc(sizeof(struct w));
q->x=x;
q->y=y;
q->next=head; q->before=NULL; head->before=q; head=q;
symbol=peekb(0xB800,(y1+y-2)*80*2+(x1+x-2)*2);
if(symbol=='+')mark++;pokeb(0xB800,(y1+head->y-2)*80*2+(x1+head->x-2)*2,h);
pokeb(0xB800,(y1+head->y-2)*80*2+(x1+head->x-2)*2+1,0x30);
if(head->next!=NULL)
{pokeb(0xB800,(y1+head->next->y-2)*80*2+(x1+head->next->x-2)*2,' ');
pokeb(0xB800,(y1+head->next->y-2)*80*2+(x1+head->next->x-2)*2+1,0x30); }
if(symbol==' ') {r=tail;
tail=tail->before;
tail->next=NULL;
pokeb(0xB800,(y1+r->y-2)*80*2+(x1+r->x-2)*2+1, attrib);
free(r);
}
if(symbol=='-') {mark--;
r=tail;
if(tail->before!=head) {
tail=tail->before->before;
pokeb(0xB800,(y1+r->before->y-2)*80*2+(x1+r->
before->x-2)*2+1, attrib);
free(r->before);
}
else if(tail!=head) tail=tail->before; tail->next=NULL;
pokeb(0xB800,(y1+r->y-2)*80*2+(x1+r->x-2)*2+1,attrib); free(r);
}
gotoxy(x,y);
delay(Speed?abs(1000-Speed): 10000); }
time(&t2); }while(c!=27&&(t2-tl<GameTime));
/* Виведення результату гри */
textbackground(BLUE);
textcolor(WHITE);
clrscr();
gotoxy(25,5);cputs("GAME OVER");
gotoxy(16,6);cprintf("%s %d %s","Your prize makes",mark,"points");
getch();
cursor_on();
}
/* Функція виключення курсора */
void cursor_off()
{
_AH=0x1;
_CH=0x20;
geninterrupt(0x10);
}
/* Функція включення курсора */
void cursor_on()
{
_AH=0x1;
_CH=0x6;
_CL=0x7;
geninterrupt(0x10);
}
Результати виконання програми
Висновок: на даній лабораторній роботі я вивчив способи запису програм на мові Сі, ознайомився з основними конструкціями мови Сі.