20
Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės pagal kompiuterio numerį (variantas tai kompiuterio numeris) d) Pavyzdžiui #include <iostream> //bibliotekos vardas, naudojamas įvedimo srautui cin //tipinis įvedimo srautas – klaviatūra Pastaba: komentuojame tas vietas, kurias žinome. 1 variantas ********* #include <stdio> #include <stdlib> #include <windows> #include <conio> #include <time> #include <iostream> void draw(char main[][75], int score); void reset(char main[][75]); void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int &score, int &quit); void check (int &direction); void directionn(int direction, int &pastCounter, int past[][2]); void apple (int &apples, char main[][75]); void quitGame (int score); int main() { int past[1000][2]; int parts = 3; char main[23][75]; int pastCounter = 6; int direction = 0; int apples = 0; int score = 0; int quit = 0; int playAgain = 1; unsigned time; srand(time(0)); for (int x = 0; x < 1000; x ++) { for (int y = 0; y < 2; y ++) { past [x][y] = 0; } } past[pastCounter][0] = 1; past[pastCounter][1] = 1; while(quit == 0) { draw(main, score);

Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

Užduotys:

a) Pakomentuoti kodo eilutes.

b) Paaiškinti ką daro programa.

c) Programą renkamės pagal kompiuterio numerį (variantas tai kompiuterio numeris)

d) Pavyzdžiui #include <iostream> //bibliotekos vardas, naudojamas įvedimo srautui

cin //tipinis įvedimo srautas – klaviatūra

Pastaba: komentuojame tas vietas, kurias žinome.

1 variantas

********* #include <stdio>

#include <stdlib>

#include <windows>

#include <conio>

#include <time>

#include <iostream>

void draw(char main[][75], int score);

void reset(char main[][75]);

void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int

&score, int &quit);

void check (int &direction);

void directionn(int direction, int &pastCounter, int past[][2]);

void apple (int &apples, char main[][75]);

void quitGame (int score);

int main()

{

int past[1000][2];

int parts = 3;

char main[23][75];

int pastCounter = 6;

int direction = 0;

int apples = 0;

int score = 0;

int quit = 0;

int playAgain = 1;

unsigned time;

srand(time(0));

for (int x = 0; x < 1000; x ++)

{

for (int y = 0; y < 2; y ++)

{

past [x][y] = 0;

}

}

past[pastCounter][0] = 1;

past[pastCounter][1] = 1;

while(quit == 0)

{

draw(main, score);

Page 2: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys check(direction);

directionn(direction, pastCounter, past);

reset(main);

move(main, parts, pastCounter, past, apples, score, quit);

if (apples == 0)

{

apple(apples, main);

}

}

quitGame(score);

}

void draw(char main[][75], int score)

{

system("cls");

cout<<"Score : %d\n"<<score;

for (int x = 0; x < 23; x ++)

{

for (int y = 0; y < 75; y ++)

{

cout << "%c"<< main[x][y];

}

cout<<"\n";

}

}

void reset(char main[][75])

{

for (int x = 0; x < 23; x++)

{

for (int y = 0; y < 75; y++)

{

if (main[x][y] == '@')

{

main[x][y] == '@';

}

else

{

if (x == 0 || x == 22 || y == 0 || y == 74)

{

main[x][y] = 177;

}

else

{

main[x][y] = ' ';

}

}

}

}

}

void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int

&score, int &quit)

{

if (past[pastCounter][0] == 22 || past[pastCounter][0] == 0)

Page 3: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys {

quit = 1;

}

if (past[pastCounter][1] == 74 || past[pastCounter][1] == 0)

{

quit = 1;

}

for (int x = 0; x < parts; x++)

{

if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == '@')

{

apples--;

parts++;

score += 10;

}

if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == 'o')

{

quit = 1;

}

else

{

main[past[pastCounter - x][0]][past[pastCounter - x][1]] = 'o';

}

}

}

void check (int &direction)

{

int key = 0;

if (kbhit())

{

key = -getch();

switch (key)

{

case -72:

direction = 2;

break;

case -77:

direction = 0;

break;

case -80:

direction = 3;

break;

case -75:

direction = 1;

break;

}

}

}

void directionn(int direction, int &pastCounter, int past[][2])

{

int down;

right = past[pastCounter][1];

down = past[pastCounter][0];

switch (direction)

{

case 0:

right ++;

break;

Page 4: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys case 1:

right --;

break;

case 2:

down --;

break;

case 3:

down ++;

}

pastCounter ++;

past[pastCounter][0] = down;

past[pastCounter][1] = right;

}

void apple (int &apples, char main[][75])

{

int up = 0;

int left = 0;

apples = 3;

for (int x = 0; x < apples; x ++)

{

up = (rand() % 22);

left = (rand() % 74);

if (main[up][left] == 'o')

{

apple(apples, main);

}

if (main[up][left] == '*')

{

apple(apples, main);

}

else

{

main[up][left] = '@';

}

}

}

void quitGame (int score)

{

int quit = 0;

system ("cls");

cout << "GAME OVER!!!!\n\n";

cout << "You got a score of %d\n"<<score;

}

*********************************************************

Variantas 2

********************************************************* #include<iostream.h>

int main()

{

for (int i=0; i<10; i++) //10 kartų kartojasi ciklas

cout<<"Get with program\n"; //išvedimas eilutės

return 0;

Page 5: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

*** // reverse_iterator::operator[] example

#include <iostream>

#include <iterator>

#include <vector>

using namespace std;

int main () {

vector<int> myvector;

for (int i=0; i<10; i++) myvector.push_back(i); // myvector: 0 1 2 3 4 5 6 7 8 9

typedef vector<int>::iterator iter_int;

reverse_iterator<iter_int> rev_iterator = myvector.rbegin();

cout << "The fourth element from the end is : " << rev_iterator[3] << endl;

return 0;

}

***********************************

Variantas 3

********************************** // pavyzdys 1

int a=0;

while (a<10)

{

a=(a+1)*2;

}

// pavyzdys 2

int numbers[100];

for (int i=0;i<100;i++)

numbers[i]=i;

// pavyzdys 3

int nums2[1000];

int j=0;

for (int i=999;i>-1;i--)

{

nums2[i]=j;

j++;

}

// pavyzdys 4

int c;

c=1;

while (c!=0)

{

cin>>c;

if (c==2)

continue;

if (c<0)

break

cout<<"OK, Good Number;)"<<endl;

Page 6: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

******************

Variantas 4

*******************

// countdown using a for loop

#include <iostream>

using namespace std;

int main ()

{

for (int n=10; n>0; n--) {

cout << n << ", ";

}

cout << "FIRE!\n";

return 0;

}

******************* // break loop example

#include <iostream>

using namespace std;

int main ()

{

int n;

for (n=10; n>0; n--)

{

cout << n << ", ";

if (n==3)

{

cout << "countdown aborted!";

break;

}

}

return 0;

}

**************************

Variantas 5

*************************

// continue loop example

#include <iostream>

using namespace std;

int main ()

{

for (int n=10; n>0; n--) {

if (n==5) continue;

cout << n << ", ";

}

cout << "FIRE!\n";

return 0;

}

Page 7: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

*************************

#include<iostream.h>

#include<dos.h>

#include<conio.h>

#include<graphics.h>

#include<stdio.h>

// GLOBAL VARIABLES

int a[5][5];

int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9};

int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

struct pos

{

int h,v;

}p[4][4];

int row=4,col=4;

// FUNCTION PROTOTYPES

void game(int); //MOVEMENT

void rec(); //DRAWING RECTANGLE

void pri(); //PRINTING NUMBERS INITIALLY

int getkey(); // TO TRACE KEY PRESSED

inline void space() { cout<<" "; }

inline void print(int r,int c) { cout<<a[r][c]; }

void init(); //TO STORE CO-ORDINATES

int stop(); // STOPING CRITERION

void gopr(int,int); //TO PRINT NUMBER IN GAME

void main()

{

Page 8: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

int gm=DETECT,gd=DETECT;

initgraph(&gm,&gd,"");

int d,cr=1;

init();

rec();

pri();

while(cr!=16)

{

d=getkey();

game(d);

cr=stop();

}

settextstyle(10,0,1);

outtextxy(400,300,"You are winner!");

getch();

}

void rec()

{

setcolor(5);

for(int i=0;i<200;i+=50)

{

for(int j=0;j<240;j+=60)

rectangle(j+100,i+100,j+50,i+60);

}

t k=1;

for(int x=0,i=6;x<4;x++,i+=3)

{

for(int y=0,j=10;y<4&&k<16;y++,j+=7,k++)

{

gotoxy(p

[y].h,p

[y].v);

cout<<a

[y];

}

}

Page 9: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

int getkey()

{

union REGS i,o;

while(!kbhit());

i.h.ah=0;

int86(22,&i,&o);

return(o.h.ah);

}

void init()

{

int k=1;

for(int x=0,i=6;x<4;x++,i+=3)

{

for(int y=0,j=10;y<4;y++,j+=7)

{

p

[y].h=j;

p

[y].v=i;

a

[y]=t[k++];

}

}

}

void game(int s)

{

int r=row-1;

int c=col-1;

if(s==77 &&c!=0) //right

{

col--;

a[r][c]=a[r][c-1];

Page 10: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

gopr(r,c-1);

space();

gopr(r,c);

print(r,c-1);

}

if(s==80 && r!=0) //down

{

row--;

a[r][c]=a[r-1][c];

gopr(r-1,c);

space();

gopr(r,c);

print(r-1,c);

}

if(s==75 && c!=3) //left

{

a[r][c]=a[r][c+1];

col++;

gopr(r,c+1);

space();

gopr(r,c);

print(r,c+1);

}

if(s==72 &&r!=3) //up

{

a[r][c]=a[r+1][c];

row++;

gopr(r+1,c);

Page 11: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

space();

gopr(r,c);

print(r+1,c);

}

}

void gopr(int x, int y)

{

gotoxy(p

[y].h,p

[y].v);

}

int stop()

{

int k=0,d=1;

for(int x=0;x<4;x++)

{

for(int y=0;y<4;y++)

{

if(a

[y]==test[k])

d++;

k++;

}

}

return d;

}

******************************************

Variatas 6

****************************************** #include <iostream>

using namespace std; int main() { int num=0; int count=0; int prime=1; char ans=' ';

Page 12: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys do { cout <<"Enter a number: "; cin >> num; for (int i=(num)/2; i>1; i--) { if (num%i==0) { count=count+1; if (count==1) cout <<"Internal Factor(s): "; prime=0; cout <<i<<" "; } } if (prime==0) { cout<<"\nNumber is not prime."<<endl; } if (prime==1) { cout <<"Number is prime."<<endl; } cout<<"Do you wish to find another prime: "; cin>>ans; cin.ignore(200,'\n'); count=0; prime=1; system("cls");

}while(ans =='y' || ans=='Y'); system("pause"); return 0; }

*******************************

Variantas 7 *******************************

#include <iostream> //usually std lib headers in other directories #include "triangle.h" //local header using namespace std;

int main()

{ triangle t1,t2, t3[10]; //just like declaring //a variable

//t1,t2, t3[10] are objects and not used triangle t4(5, 6); //the numbers stand for base and //height

//t3 sets up an array of 10 objects cout <<"Area of triangle t4 equals "<< t4.area()<<endl; t1.setBase(3); //can set values of variables using methods t1.setHeight(4);

cout<<"Area of triangle t1 equals " << t1.area()<<endl;

Page 13: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

t3[0].setBase(5); t3[0].setHeight(6);

cout<<"Area of triangle t3[0] equals " << t3[0].area()<<endl; system("pause"); return 0;

}

************************************************

Variantas 8 *********************************************** class triangle

{ //if you don't say public or private, by //default it is all private, can mix public/private

//no order public: //user accessed data, functions //data has state, functions have behavior //functions in classes are called methods

//just like prototypes triangle(); //creates triangle objects //constructor...never have return types

//if no parameters...it is called //the default constructor triangle(double, double); //not a default constructor //constructor allows you to set base and height

void setBase(double); void setHeight(double); double area();

private: //declaring variables...never make //variables something the user can declare //it gives him too much power...this is dangerous double base, height; //sort of global variable

//in the implementation

//protected: //usually use in cases of inheritance

};

***************************************

Variantas 9

*************************************** #include <iostream> #include "triangle.h" using namespace std;

//syntax: return type if there is one, write the class you are in ::

//scope resolution operator, then the name of the function triangle::triangle() //creates triangle objects

{//default constructor base=0; //typically in default constructor height=0; //initialize your variables

} triangle::triangle(double b, double h) //not a default constructor

{//never in your parameter use the variable names given in the header //or they will become local to the function

Page 14: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

base=b; height=h;

} void triangle::setBase(double b)

{ base=b;

} void triangle::setHeight(double h)

{ height=h;

} double triangle::area()

{// the 1./ required to create a decimal answer or truncation occurs return (1./2)*base*height;

}

Variantas 10

******************************************************************** #include <dos.h>

#include <stdio.h>

#include <stdlib.h>

int menu(void);

int main(void)

{

while(1)

{

/*get selection and execute the relevant statement*/

switch(menu())

{

case 1:

{

puts("sound the speaker 1\n");

sound(2000);

sleep(2);

nosound();

break;

}

case 2:

{

puts("sound that speaker 2\n");

sound(4000);

sleep(2);

nosound();

break;

}

case 3:

{

puts("You are quitting\n");

exit(0);

break;

}

default:

{

puts("Invalid menu choice\n");

break;

Page 15: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

}

}

return 0;

}

/*menu function*/

int menu(void)

{

int reply;

/*display menu options*/

puts("Enter 1 for beep 1.\n");

puts("Enter 2 for beep 2.\n");

puts("Enter 3 to quit.\n");

/*scan for user entry*/

scanf("%d", &reply);

return reply;

}

***************************

Variantas 11

**************** #include <graphics.h>

#include <dos.h>

#include <conio.h>

main()

{

int i, j = 0, gd = DETECT, gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);

outtextxy(25,240,"Press any key to view the moving car");

getch();

setviewport(0,0,639,440,1);

for( i = 0 ; i <= 420 ; i = i + 10, j++ )

{

rectangle(50+i,275,150+i,400);

rectangle(150+i,350,200+i,400);

circle(75+i,410,10);

circle(175+i,410,10);

setcolor(j);

delay(100);

if( i == 420 )

break;

clearviewport();

}

getch();

closegraph();

return 0;

}

***********************************

Variantas 12

Page 16: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

*****************************

#include <iostream>

#include <windows.h>

using namespace std;

//twinkle twinkle

//CC GG AA G

//FF EE DD C

//GG FF EE D

//GG FF EE D

//CC GG AA G

//FF EE DD C

void playnote (char g, float l)

{

char n = g;

if (n == 'A'||'a'){

Beep(2750,l*1000);

cout <<n;

}

else if(n == 'B'||'b'){

Beep(3087,l*1000);

cout << n;

}

else if(n == 'C'||'c'){

Beep(1637,l*1000);

cout << n;

Page 17: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

else if(n == 'D'||'d'){

Beep(1835,l*1000);

cout << n;

}

else if(n == 'E'||'e'){

Beep(2060,l*1000);

cout << n;

}

else if(n == 'F'||'f'){

Beep(2183,l*1000);

cout << n;

}

else if(n == 'G'||'g'){

Beep(2450,l*1000);

cout << n;

}

}

int main() {

playnote('C', 0.4);

playnote('C', 0.4);

playnote('G', 0.4);

playnote('G', 0.4);

playnote('A', 0.4);

Page 18: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

playnote('A', 0.4);

playnote('G', 0.4);

Sleep(400);

cout<<endl;

playnote('F', 0.4);

playnote('F', 0.4);

playnote('E', 0.4);

playnote('E', 0.4);

playnote('D', 0.4);

playnote('D', 0.4);

playnote('C', 0.4);

Sleep(400);

cout<<endl;

playnote('G', 0.4);

playnote('G', 0.4);

playnote('F', 0.4);

playnote('F', 0.4);

playnote('E', 0.4);

playnote('D', 0.4);

playnote('G', 0.4);

Sleep(400);

cout<<endl;

playnote('G', 0.4);

playnote('F', 0.4);

Page 19: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

playnote('F', 0.4);

playnote('E', 0.4);

playnote('D', 0.4);

playnote('C', 0.4);

playnote('C', 0.4);

Sleep(400);

cout<<endl;

playnote('G', 0.4);

playnote('G', 0.4);

playnote('A', 0.4);

playnote('A', 0.4);

playnote('G', 0.4);

playnote('F', 0.4);

playnote('F', 0.4);

Sleep(400);

cout<<endl;

playnote('E', 0.4);

playnote('E', 0.4);

playnote('D', 0.4);

playnote('D', 0.4);

playnote('C', 0.4);

cout<<endl;

cout << "thanks for listening <3";

return 0;

Page 20: Parengė ITMM Artūras Šakalys - mokytojas.eu · Parengė ITMM Artūras Šakalys Užduotys: a) Pakomentuoti kodo eilutes. b) Paaiškinti ką daro programa. c) Programą renkamės

Parengė ITMM Artūras Šakalys

}

*****************************

*****************************

http://www.caspercomsci.com/pages/cplussource.htm

*************************