Posts Tagged “programowanie”

Polecenie:
Policz silnię z n oraz wartość bezwzględna liczby.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <stdlib .h>
#include <stdio .h>
using namespace std;
float bez(float c)
{
        if (c&lt;0)
          c=-c;
 
return c;
 
}
int silnia(unsigned n)
{
 long int i;
 int s=1;
 
 for (i=1;i< =n;i++)
        s=s*i;
 
 return s;
}
 
main () {
  int a;
  printf("Policzymy Silnie! \nPodaj liczbe: ");
  cin>>a;
  cout< <endl<<"Silnia z "<<a<<" wynosi: "<<silnia(a)<<endl;
 
  printf("Policzymy wartosc bezwzgledna! \nPodaj liczbe: ");
  cin>>a;
  cout< <endl<<"Wartosc z "<<a<<" wynosi: "<<bez(a)<<endl;
 
 
  system("PAUSE");
}

Comments Brak komentarzy »

Polecenie:
Testujemy dwie pętle while oraz for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma hdrstop
#pragma argsused
#include <iostream .h>
#include <stdio .h>
 
main()
{
  int i,min,max;
  cout< <"Sprawdzamy Min i Max"<<endl;
  cout<<endl<<endl;
  cout<<"Testujemy dwie petle while oraz for: "<<endl;
  do
  {
  cout<<"Podaj min: ";
  cin>>min;
  cout< <"Podaj max: ";
  cin>>max;
          if(min>max)
         cout< <"\n\tBlad! Min musi byc < od Max!\n"<<endl;
  }while(min>max)
  cout< <"Podales min: "<<min<<", max: "<<max<<endl;
  cout<<"Wyswietl co 2 od min do max:"<<endl<<endl;
 
  cout<<"Wyswietl za pomoca petli for:"<<endl;
  for(i=min;i<=max;i=i+2)
  cout<<i<<endl;
 
  cout<<endl<<"Wyswietl za pomoca petli while:"<<endl;
  while(min<=max)
  {
  cout<<min<<endl;
  min=min+2;
  }
system("PAUSE");
}

Comments Brak komentarzy »

Polecenie
Ruszający się kolorowy pacman nie pozostawia za sobą śladu, zjada losowo porozrzucane kropki ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream .h>
#include <stdlib>
#include <stdio>
#include <time>
#include <conio>
 
 
main () {
char button;
int x,y,a,b;
 
cout< <"Poruszanie PacManem: "<<endl;
cout<<"a - lewo"<<endl<<"s - dol"<<endl<<"w - gora"<<endl<<"d - prawo"<<endl;
x=40;
y=20;
cout<<"Zbierz wszystkie * !"<<endl<<"Nacisnij klawsz aby zagrac!"<<endl;
getch();
system("cls");
for (int i=1;i&lt;20;i++)
 {
 a=rand()%40;
 b=rand()%40;
 gotoxy(a,b);
 cout<<"*";
 }
do
{
gotoxy(x,y);
textattr(1*14);
cprintf("C");
button = getch();
 
switch( button )
{ case 'a' :
{
gotoxy(x,y);
cout<<"  ";
x=x-1; break;
}
case 'w' :
{
gotoxy(x,y);
cout<<"  ";
y=y-1; break;
}
case 's' :
{
gotoxy(x,y);
cout<<"  ";
y=y+1; break;
}
case 'd' :
{
gotoxy(x,y);
cout<<"  ";
x=x+1; break;
}
}
 
}
while (button != 'q');
cout<<endl;cout<<endl;
system("PAUSE");
}

Comments 1 komentarz »

Polecenie
Wylosować elementy do tablicy prostokątnej X na Y. Obliczyć sumę wszystkich elementów dodatnich, ujemnych i zliczyć ile ich jest. Zliczyć ile jest elementów równych zero. Na koniec zapytać czy powtórzyć całość, dopuszczalne znaki to T/t/n/N. Program zabezpieczyć hasłem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <stdlib .h>
#include <stdio .h>
#include <time .h> //windows #include <conio>
#define X 5
#define Y 2
using namespace std;
 
 
int gotoxy(int x, int y) {
	char essq[100]; 
	char xstr[100]; 
	char ystr[100]; 
	sprintf(xstr, "%d", x);
	sprintf(ystr, "%d", y);
	essq[0] = '\0';
	strcat(essq, "\033[");
	strcat(essq, ystr);
	strcat(essq, "d");
	strcat(essq, "\033[");
	strcat(essq, xstr);
	strcat(essq, "G");
	printf("%s", essq);
	return 0;
} //tylko dla linuksa!
 
 
 
main () {
	system("clear"); //system("cls");
	int ix,iy,sd=0,sn=0,iled=0,ilen=0,ilez=0;
	char znak;
	int tab[X][Y];
	srand(time(NULL));
	string haslo="tajne";
	string wprowadzone;
	do {
	  cout < < "Podaj haslo: ";
	  cin >> wprowadzone;
	  if ( wprowadzone != haslo ) 
		  cout < < "Wprowadzone haslo jest bledne\n";
	} while ( wprowadzone != haslo );
 
      do {
	      system("clear"); //windows system("cls");
	sd=0;sn=0;iled=0;ilen=0;ilez=0;
	cout << "\t\t\tTablica" ;  
       for (iy=1; iy<=Y; iy++)
        for (ix=1; ix<=X; ix++) 
	{
		tab[ix][iy]=rand()%10-rand()%5;
		gotoxy(ix*5+15,iy+2);
		cout << tab[ix][iy];
	}
 
	 for (iy=1; iy<=Y; iy++)
          for (ix=1; ix<=X; ix++) 
	    if ( tab[ix][iy] > 0 ) 
	    {
               sd=sd+tab[ix][iy];
	       iled=iled+1;
	    }
	    else if ( tab[ix][iy] < 0 ) 
	    {
		sn=sn+tab[ix][iy];
		ilen=ilen+1;
	    }
	    else
		ilez=ilez+1;
 
cout<<endl<<"Liczb dodatnich jest: "<<iled<<" i ich suma wynosi: "<<sd<<endl;
cout<<"Liczb ujemnych jest: "<<ilen<<" i ich suma wynosi: "<<sn<<endl;
cout<<"Zer natomiast jest: "<<ilez<<endl;
 
      cout << "Czy chcesz kontynuowac? [T/N] "; 
      	do {
        	gotoxy(32,Y+6); 
		znak=toupper(getchar());
      	}while (znak != 'T' && znak != 'N');
      } while (znak != 'N');
}

Comments Brak komentarzy »

(c) 2007 by Michał Terbert