CS3369 Lab 5 (ASSIGNMENT 2) IMPORTANT INFORMATION

advertisement
CS3369 Lab 5 (ASSIGNMENT 2)
IMPORTANT INFORMATION
Answer the first THREE questions in the FOUR exercises.
Exercise 4 is optional. The due time is 8:30 pm. MARCH 29,
2000 (week 8). Drop the assignment in MAIL BOX 52 or 53.
Hand in a hard copy of your answers.
Hints: (1) The layouts of your programs are important.
(2) The comments in the programs are important.
(3) You should have a cover page as follows:
STUDENT NAME: ______________________
STUDENT ID:
_______________________
CS3369 REAL TIME CONTROL SOFTWARE
ASSIGNMENT TWO
The cover page deserves 5%.
Exercise 1. (25%)Try the following program.
#include<dos.h>
#include <graphics.h>
#include<iostream.h>
#include<conio.h>
void show(int i,float h, float v);
void erease(int i, float h, float v);
void planeshow(int i,int k);
void ereasep(int i, int k);
void main(void)
{
int
driver = DETECT,mode;
int
i;
initgraph(&driver,&mode,"G:\\APPS\BC31.WIN\\bgi");
setcolor(WHITE);
line(1,400,400,400);
for ( i = 0; i < 80; i++ )
{
setcolor(BLUE); planeshow(5*i, 5);
show(i, 5.0, 9.0);
setcolor(YELLOW); planeshow(5*(i-8), 0);
show(i-10, 10.0, 8.0);
delay (300);
ereasep(5*i, 5); ereasep(5*(i-8), 0);
erease(i, 5.0, 9.0);
erease(i-10, 10.0, 8.0);
}
closegraph();
}
void show(int i, float h, float v)
{
int x, y;
x=h*i;
y=v*i-0.15*i*i;
setcolor(RED);
circle(400-x,400-y,2);
}
void erease(int i, float h, float v)
{
int x, y;
x=h*i;
y=v*i-0.15*i*i;
setcolor(BLACK);
circle(400-x,400-y,2);
}
void planeshow(int i,int k)
{
int j;
circle(i+5, 202+k, 2);
circle(i+3, 204+k, 2);
for (j=0; j<=8; j++)
circle(i+j, 200+k, 2);
circle(i+5, 198+k, 2);
circle(i+3, 196+k, 2);
}
void ereasep(int i, int k)
{
int j;
setcolor(BLACK);
circle(i+5, 202+k, 2);
circle(i+3, 204+k, 2);
for (j=0; j<=8; j++)
circle(i+j, 200+k, 2);
circle(i+5, 198+k, 2);
circle(i+3, 196+k, 2);
}
Question 1.1 What do the functions show(), erease(), planeshow() and ereasep() do?
Question 1.2. What are the purposes of the parameters int i and int k in function
void planeshow(int i,int k)?
Question 1.3. What are the purposes of the parameters int i, float h, and
float v in function void show(int i, float h, float v)?
Exercise 2. (35%) Modify the program in exercise 1 so that the user can use the key
‘u’ and ‘i’ to control the hight of the the second airplane, i.e, pressing u once will
increase the hight of the airplane by 8 positions and pressing ‘i’ once will decrease the
hight of the airplane by 8.
Hints: The following function will test whether a key is pressed and if yes, it returns
the pressed key, otherwise, it returns ‘?’.
char read_key()
{
int y=1;
char x;
_AH=0x01;
geninterrupt(0x16);
y=_FLAGS&0x40;
if(y == 0)
{
_AH=0x00;
geninterrupt(0x16);
x=_AL;
return x; }
return '?';
}
The following main function shows how to use read_key(). If user press the key ‘a’,
the program will show two airplanes, otherwise, it shows one airplane.
#include<dos.h>
#include <graphics.h>
#include<iostream.h>
#include<conio.h>
void show(int i,float h, float v);
void erease(int i, float h, float v);
void planeshow(int i,int k);
void ereasep(int i, int k);
char read_key();
void main(void)
{
int
driver = DETECT,mode;
int
i,f=0;
initgraph(&driver,&mode,"G:\\APPS\\BC31.WIN\\bgi");
setcolor(WHITE);
line(1,400,400,400);
for ( i = 0; i < 80; i++ )
{
setcolor(BLUE); planeshow(5*i, 5);
show(i, 5.0, 9.0);
if (read_key() =='a') f=1;
if (f==1)
{setcolor(YELLOW); planeshow(5*(i-8), 0);}
show(i-10, 10.0, 8.0);
delay (300);
ereasep(5*i, 5); ereasep(5*(i-8), 0);
erease(i, 5.0, 9.0);
erease(i-10, 10.0, 8.0);
}
closegraph();
}
Exercise 3. (35%) Modify your program for Exercise 2 so that whenever the user
press key ‘m’, it shoots one bullet. (The maximum number of bullets it can shoot is
100. See my demo.)
Hints: (1) The following codes can show up to j bullets. If a[k] is not equal to zero,
the program will show a bullet at “time” i-a[k], i.e., this bullet is a[k] steps behind the
earliest possible bullet. Initially, every element in a[] is assigned value 0.
int a[100];
int k;
for (k=0; k<=99; k++)
a[k]=0;
for (k=0; k<=j; k++)
if (a[k]!=0) show(i-a[k], 10.0+0.5*k, 8.0+0.1*k);
(2) if you press the key ‘m’, you should set a[j]=i, where (j-1) is the number of ‘m’’s
you have pressed and i the the index for the outer loop that controls the steps of the
moving process.
Exercise 4. (2 marks bonus in the final exam) Modify the program in Exercise 3 so
that the program can do some funny thing(s), e.g., the airplane can drop a bomb, the
airplane can shoot, or the airplane can explode if shot by a bullet, etc. Your program
should be very very funny in order to get the 2 marks bonus. You get either 0 or 2
for this exercise.
(If you decide to do exercise 4, you have to demonstrate your program in the lab
of week 7. The deadline for other exercises is still MARCH 29, 2000)
Download