101
Instructiunea while o Afisati primele 10 numere naturale o Afisati primele 90 numere naturale sub forma unui tabel cu 9 coloane. Alinierea numerelor se va face la stanga. o Afisati primele 200 numere naturale sub forma unui tabel cu 10 coloane. Alinierea numerelor se va face la dreapta. o Afisati primele numere naturale sub forma unui tabel cu 7 coloane si 20 de randuri. Alinierea numerelor se va face la stanga.

Laborator instructiuni repetitive - UMFST

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Laborator instructiuni repetitive - UMFST

Instructiunea while o Afisati primele 10 numere naturale

o Afisati primele 90 numere naturale sub forma unui tabel cu 9 coloane. Alinierea

numerelor se va face la stanga.

o Afisati primele 200 numere naturale sub forma unui tabel cu 10 coloane.

Alinierea numerelor se va face la dreapta.

o Afisati primele numere naturale sub forma unui tabel cu 7 coloane si 20 de

randuri. Alinierea numerelor se va face la stanga.

Page 2: Laborator instructiuni repetitive - UMFST

o Afisati primele numere naturale sub forma unui tabel cu 7 coloane si 20 de

randuri. Alinierea numerelor se va face la dreapta.

o Afisati suma si produsul primelor 100 numere naturale

o Afisati primele 91 numere naturale astfel: pe prima linie un numar, pe linia a doua

2 nummere, pe linia a treia 3 numere... etc

Page 3: Laborator instructiuni repetitive - UMFST

o Afisati primele 91 numere naturale astfel: pe prima linie 13 numar, pe linia a doua

12 nummere, pe linia a treia 11 numere... pe ultima linie un numar

o Instructiuni while in WFA Utilizand instructiunea while realizati urmatoarele aplicatii

Realizati o aplicatie grafica care deseneaza linii orizontale intr-un

dreptunghi asemanator cu imaginea de jos:

Page 4: Laborator instructiuni repetitive - UMFST

Se dau coordonatele x0, y0, latimea w si inaltimea h ale dreptunghiului.

Realizati o aplicatie grafica care deseneaza linii verticale intr-un

dreptunghi asemanator cu imaginea de jos:

Se dau coordonatele x0, y0, latimea w si inaltimea h ale dreptunghiului.

Realizati o aplicatie grafica care deseneaza caroiaj intr-un dreptunghi

asemanator cu imaginea de jos:

Se dau coordonatele x0, y0, latimea w si inaltimea h ale dreptunghiului.

Realizati o aplicatie grafica pentru a desena axa ox oy cu diviziuni

asemanatoare imaginii de jos:

Page 5: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica pentru a desena dereptunghiuri asemanatoare

cu cele de jos:

Realizati o aplicatie grafica pentru a desena elipse si dreptunghiuri de

dimensiuni si culori aleatoare asemanatoare cu imaginea de jos:

Page 6: Laborator instructiuni repetitive - UMFST

Reluati aplicatia din curs cu roza polara dar de data aceasta coeficientul se

da repetitiv cu o bucla while nu din NumericUpDown

o Instructiunea do while Realizati un program care calculeaza integrala functiei x3 pe intervalul x1

x2

Realizati un program care calculeaza integrala functiei x3 pe intervalul 0-

10 utilizand metoda trapezului.

Realizati un program C# WFA care afiseaza graficul din figura de jos:

Page 7: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica in care veti simula un termometru asemanator

cu imaginea de jos:

o Instructiunea for Afisati primele 25 de numere

Afisati patratul primelor 10 numere naturale

Realizati un program care cere un numar si verifica daca acesta este prim

sau nu.

Afisati toate numerele prime pana la 1000

Realizati un program care afiseaza numerele din figura de jos:

Page 8: Laborator instructiuni repetitive - UMFST

o

o Instructiuni for in WFA Utilizand instructiunea for realizati urmatoarele aplicatii

Realizati o aplicatie grafica pentru a desena o tabla de sah asemanatoare

imaginii de jos:

Realizati o aplicatie grafica care deseneaza linii orizontale intr-un

dreptunghi asemanator cu imaginea de jos:

Se dau coordonatele x0, y0, latimea w si inaltimea h ale dreptunghiului.

Realizati o aplicatie grafica care deseneaza linii verticale intr-un

dreptunghi asemanator cu imaginea de jos:

Page 9: Laborator instructiuni repetitive - UMFST

Se dau coordonatele x0, y0, latimea w si inaltimea h ale dreptunghiului.

Realizati o aplicatie grafica care deseneaza caroiaj intr-un dreptunghi

asemanator cu imaginea de jos:

Avand x0, y0,w,h si numarul de patratele realizati o aplicatie

asemanatoare imaginii de jos:

Page 10: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica pentru a desena axa ox oy cu diviziuni

asemanatoare imaginii de jos:

Page 11: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica pentru a desena dereptunghiuri asemanatoare

cu cele de jos:

Realizati o aplicatie grafica pentru a desena cercuri inscrise asemanatoare

cu cele de jos:

Page 12: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica in care veti simula un termometru asemanator

cu imaginea de jos:

Realizati o aplicatie grafica reprezentand un instrument virtual de forma:

Page 13: Laborator instructiuni repetitive - UMFST

Deplasati acul indicator pe in arc de cerc nu pe o dreapta.

Generati valorile random.

Trasati intr-un cerc o raza intr-o pozitie oarecare

Sugestie:

Page 14: Laborator instructiuni repetitive - UMFST

Trasati 60 raze echidistante intr-un cerc

Trasati 60 raze echidistante intr-un cerc. Se vor evidentia razele din 5 in

cinci cu o culoare distincta astfel:

Page 15: Laborator instructiuni repetitive - UMFST

Realizati un cadran de forma:

Folosind un timer afisati cu temporizare de 1 secunda 60 de raze

echidistante intru-un cerc

Page 16: Laborator instructiuni repetitive - UMFST

Reluati aplicatia anterioara dar stergeti razele anterioare pentru a simula o

raza care se roteste

Simulati radarul din imaginea de sus

Realizati un ceas de forma:

Afisati rotatia pamantului in jurul soarelui sub forma:

Page 17: Laborator instructiuni repetitive - UMFST

Folosind un timer cu temporizare de 0,1 simlati rotatia pamantului in jurul

soarelui

Page 18: Laborator instructiuni repetitive - UMFST

Realizati o aplicatie grafica pentru a desena elipse si dreptunghiuri de

dimensiuni si culori aleatoare asemanatoare cu imaginea de jos:

Reluati aplicatia din curs cu roza polara dar de data aceasta coeficientul se

da repetitiv cu o bucla for nu din NumericUpDown

Realizati o aplicatie grafica in care veti exemplifica paradoxul lui ahile cu

broasca

Realizati o aplicatie grafica in care veti desena n cercuri de forma de jos.

N este dat de un trackbar

Page 19: Laborator instructiuni repetitive - UMFST

Programul afiseaza primele 10 numere naturale

// Program scris in C++ Visual Studio 2008 de tipul:CLR console application

// Programul afiseaza primele 10 numere naturale

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=1;

cout <<" \n\tNumerele sunt: ";

while (i<=10)

{

cout << i << " ";

i+=1;

}

cin.ignore();

cin.get();

return 0;

}

Page 20: Laborator instructiuni repetitive - UMFST

Afisarea primelor 90 numere naturale intr-un tabel cu 9 coloane. Alinierea numerelor

se face la stanga

// Program scris in C++ Visual Studio 2008 de tipul:CLR console application

// Programul afiseaza primele 90 numere naturale intr-un tabel cu 9 coloane

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

cout <<" \n\tNumerele sunt:\n\n ";

while (i<< i <<"\t";

i+=1;

if ((i%9)==0)

cout <<"\n ";

}

cin.ignore();

cin.get();

return 0;

}

cin.ignore();

cin.get();

return 0;

}

Afisarea primelor 200 numere naturale intr-un tabel cu 10 coloane. Alinierea

numerelor se face la stanga

// Program scris in C++ Visual Studio 2008 de tipul:CLR console application

// Programul afiseaza primele 200 numere naturale intr-un tabel cu 10

coloane.

// Alinierea numerelor se face la stanga

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

Page 21: Laborator instructiuni repetitive - UMFST

int i=0;

cout <<" \n\tNumerele sunt:\n\n ";

while (i<< i <<<"\n ";

}

cin.ignore();

cin.get();

return 0;

}

Afisarea primelor 200 numere naturale intr-un tabel cu 10 coloane. Alinierea

numerelor se face la dreapta

// Program scris in C++ Visual Studio 2008 de tipul:CLR console application

// Programul afiseaza primele 200 numere naturale intr-un tabel cu 10

coloane.

// Alinierea numerelor se face la dreapta

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

cout <<" \n\tNumerele sunt:\n\n ";

while (i<< " ";

if ((i>=10)&& (i<< " ";

if (i>99)

cout << " ";

cout << i ;

i+=1;

if ((i%10)==0)

cout <<"\n ";

}

cin.ignore();

cin.get();

return 0;

}

Afisati primele numere naturale sub forma unui tabel cu 7 coloane si 20 de randuri.

Alinierea numerelor se va face la stanga.

Page 22: Laborator instructiuni repetitive - UMFST

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (r <= 20)

{

while(c <= 7){

cout<< i <<"\t";

i+=1;

c++;

}

cout<<"\n";

c=1;

r++;

}

cin.ignore();

cin.get();

return 0;

}

Afisati primele numere naturale sub forma unui tabel cu 7 coloane si 20 de randuri.

Alinierea numerelor se va face la dreapta.

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (r<=20)

{

while(c <=7){

if(i > 99)

cout<<" ";

if((i > 9)&&(i < 100))

Page 23: Laborator instructiuni repetitive - UMFST

cout<<" ";

if(i < 10)

cout<<" ";

cout<< i <<" ";

i+=1;

c++;

}

cout<<"\n";

c=1;

r++;

}

cin.ignore();

cin.get();

return 0;

}

Afisare suma primelor 100 numere naturale

// Program scris in C++ Visual Studio 2008 de tipul:CLR console application

// Programul afiseaza suma primelor 100 numere naturale

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0,s=0;

cout <<" \n\tSuma este: ";

while (i <=100)

{

s=s+i;

i+=1;

}

cout << s;

cin.ignore();

cin.get();

return 0;

}

Ver 1: Afisare primele 90 numere naturale astfel: pe prima linie un numar, pe linia a

Page 24: Laborator instructiuni repetitive - UMFST

doua 2 nummere, pe linia a treia 3 numere... etc

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (i<=90)

{

while(c <=r){

if(i >=100)

cout<<" ";

if((i > 9)&&(i < 100))

cout<<" ";

if(i< 10)

cout<<" ";

cout<< i <<" ";

i++;

c++;

}

cout<<"\n";

c=1;

r++;

}

cin.ignore();

cin.get();

return 0;

}

Ver 2 gresita Afisare primele 90 numere naturale astfel: pe prima linie un numar, pe

linia a doua 2 nummere, pe linia a treia 3 numere... etc

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

Page 25: Laborator instructiuni repetitive - UMFST

{

int i=0;

int r=1;

int h=1;

cout <<" \n\tNumerele sunt:\n";

while (i <=90)

{

cout << i << " ";

if(h==r){

cout <<"\n";

r+=1;

h=1;

}

h+=1;

i+=1;

}

cin.ignore();

cin.get();

return 0;

}

Ver 2 corecta Afisare primele 90 numere naturale astfel: pe prima linie un numar, pe

linia a doua 2 nummere, pe linia a treia 3 numere... etc

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;

int h=1;

cout <<" \n\tNumerele sunt:\n";

while (i <= 90)

{

cout << i << " ";

if(h==r){

cout <<"\n";

r+=1;

h=1;

Page 26: Laborator instructiuni repetitive - UMFST

}

else{

h+=1;

}

i+=1;

}

cin.ignore();

cin.get();

return 0;

}

Afisare 91 numere naturale astfel: pe prima linie 13 numar, pe linia a doua 12

nummere, pe linia a treia 11 numere... pe ultima linie un numar

Programul nu functioneaza corect daca pun in loc de i>91 pun de ex i>100 din cauza

unei bucle infinite ce se creaza

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (i < 91)

{

while((15-r-c)>0){

if(i > 99)

cout<<" ";

if((i > 9)&&(i < 100))

cout<<" ";

if(i < 10)

cout<<" ";

cout<< i <<" ";

i+=1;

c++;

}

cout<<"\n";

c=1;

r++;

}

cin.ignore();

cin.get();

return 0;

Page 27: Laborator instructiuni repetitive - UMFST

}

Afisare 91 numere naturale astfel: pe prima linie 13 numar, pe linia a doua 12

nummere, pe linia a treia 11 numere... pe ultima linie un numar

Programul functioneaza corect chiar daca pun in loc de i>91 de ex i>155

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while ((i <= 155)&&((15-r-c) >= 0))

{

while((15-r-c) > 0){

if(i>99)

cout<<" ";

if((i > 9)&&(i < 100))

cout<<" ";

if(i < 10)

cout<<" ";

cout<< i <<" ";

i+=1;

c++;

}

cout<<"\n";

c=1;

r++;

}

cin.ignore();

cin.get();

return 0;

}

Integrala functiei x^2 pe intervalul [0-10], dupa care se compara cu rezultatul 333.333 .

Primitiva functiei x^2 este x^3/3. Pe intervalul [0-10] avem 1000/3=333.333

Se observa ca eroarea depinde de finetea pasului (numarul de pasi).

Page 28: Laborator instructiuni repetitive - UMFST

// Programul calculeaza integrala functiei x^2 pe intervalul 0-10

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

double s=0;

double x0=0;

double x1=10;

double nr_pasi=1000000;

const double pas=(x1-x0)/nr_pasi;

double x=x0;

while (x<=x1)

{

s=s+x*x*pas;

x=x+pas;

}

cout <<" \n\tIntegrala este: " << s;

cin.ignore();

cin.get();

return 0;

}

Integrala functiei x^2 pe intervalul 0-10

Metoda trapezului.

// Programul calculeaza integrala functiei x^2 pe intervalul 0-10 utilizand

metoda trapezului

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

double y=0;

double yv=0;

double s=0;

double x0=0;

double x1=10;

Page 29: Laborator instructiuni repetitive - UMFST

double x=x0;

double nr_pasi=1000;

double d=(x1-x0)/nr_pasi;

while (x<=x1)

{

y=x*x;

s=s+(y+yv)*d/2;

x=x+d;

yv=y;

}

cout <<" \n\tIntegrala este:"<< s;

cin.get();

}

Se cere numarul de randuri si coloane

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

int cmax;

int rmax;

cout <<"\nCate coloane:";

cin >> cmax;

cout <<"\nCate randuri:";

cin >> rmax;

while (r <=rmax)

{

cout << i << "\t";

c+=1;

i+=1;

if(c>cmax){

cout <<"\n";

r+=1;

c=1;

}

}

Page 30: Laborator instructiuni repetitive - UMFST

cin.ignore();

cin.get();

return 0;

}

If imbricat in while

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (r <=20)

{

cout << i << "\t";

c+=1;

i+=1;

if(c > 7){

cout <<"\n";

r+=1;

c=1;

}

}

cin.ignore();

cin.get();

return 0;

}

While imbricat intr-un while folosit ciudat

#include "stdafx.h"

Page 31: Laborator instructiuni repetitive - UMFST

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int j=1;//contor numere afisate pe un rand

int k=1;//contor linii

while (k < 21)

{

cout <<i<<"\t";

i++;

j++;

while (j > 7){

cout <<"\n";

j=1;

k++;

}

}

cin.ignore();

cin.get();

return 0;

}

While imbricat intr-un while corect

#include "stdafx.h"

#include < iostream >

using namespace std;

int main()

{

int i=0;

int c=1;// contor coloane

int r=1;// contor randuri

while (r < 21){

c=1;

while(c < 8);

{

cout << i <<"\t";

i++;

c++;

}

cout <<"\n";

r++;

}

cin.ignore();

cin.get();

Page 32: Laborator instructiuni repetitive - UMFST

return 0;

}

Afisare 1 pe randul 1 2 pe randul 2 3 p erandul3 etc- cu greseala

#include "stdafx.h"

#include < iostream >

using namespace std;

int main()

{

int i=0;

int c=1;// contor coloane

int r=1;// contor randuri

while (i <=r){

cout << i<<" ";

i++;

c++;

}

cout <<"\n";

r++;

}

cin.ignore();

cin.get();

return 0;

}

Afisare 1 pe randul 1 2 pe randul 2 3 p erandul3 etc utilizand if -cu greseala

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

Page 33: Laborator instructiuni repetitive - UMFST

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (i <=90)

{

cout << i << " ";

if(c==r){

cout <<"\n";

r+=1;

c=1;//greseala

}

c+=1;

i+=1;

}

cin.ignore();

cin.get();

return 0;

}

Afisare 1 pe randul 1 2 pe randul 2 3 p erandul3 etc utilizand if-corect

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=0;

int r=1;//randul

int c=1;//coloana

cout <<" \n\tNumerele sunt:\n";

while (i <=90)

{

cout << i << " ";

if(c==r){

cout <<"\n";

r+=1;

c=0;

}

c+=1;

i+=1;

}

cin.ignore();

cin.get();

return 0;

Page 34: Laborator instructiuni repetitive - UMFST

}

Afisare 13 pe randul 1 12 pe randul 2 11 pe randul3

#include "stdafx.h"

#include < iostream >

using namespace std;

int main()

{

int i=0;

int c=1;// contor coloane

int r=1;// contor randuri

while (r<=13){

c=1;

while(c<=(13-r+1)){

cout << i<<" ";

i++;

c++;

}

cout <<"\n";

r++;

}

cin.ignore();

cin.get();

return 0;

}

Afisare 13 pe randul 1 12 pe randul 2 11 pe randul3 alta metoda

#include "stdafx.h"

#include < iostream >

using namespace std;

int main()

{

int i=0;

int c=1;// contor coloane

Page 35: Laborator instructiuni repetitive - UMFST

int r=1;// contor randuri

while (i<=(13-r+1)){

cout << i<<" ";

i++;

c++;

}

cout <<"\n";

r++;

}

cin.ignore();

cin.get();

return 0;

}

Instructiunea WHILE in WFA

WFA-Caroiaj in dreptunghi

float x0=100,y0=100,w=250, h=150;

int i=0;

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

Desen->Clear(System::Drawing::Color(this->BackColor));

Desen->DrawRectangle(Creion_albastru,x0,y0,w,h);

//linii verticale

i=0;

while(i < w){

Page 36: Laborator instructiuni repetitive - UMFST

Desen->DrawLine(Creion_albastru,x0+i,y0,x0+i,y0+h);

i+=10;

}

// linii orizontale

i=0;

while(i < h){

Desen->DrawLine(Creion_albastru,x0,y0+i,x0+w,y0+i);

i+=10;

}

WFA-Aplicatie grafica pentru a desena axa ox oy cu diviziuni -realizat cu while

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_blu ;

System::Drawing::Pen^ Creion_rosu ;

Creion_blu=gcnew System::Drawing::Pen(System::Drawing::Color::Blue);

Creion_rosu=gcnew System::Drawing::Pen(System::Drawing::Color::Red);

Desen= this->CreateGraphics();

Desen->Clear(System::Drawing::Color(this->BackColor));

int w=this->Size.Width;

int h=this->Size.Height;

Desen->DrawLine( Creion_rosu,10,20, 10, h);

Desen->DrawLine(Creion_rosu,0,h-45, w, h-45);

int j=10;

while (j < w-30){

Page 37: Laborator instructiuni repetitive - UMFST

Desen->DrawLine(Creion_blu,10+j,h-43, 10+j, h-47);

j+=10;

}

j=10;

while (j < h-65){

Desen->DrawLine(Creion_blu,8,20+j,12,20+j);

j+=10;

}

}

Realizati un program C# WFA care afiseaza graficul din figura de jos:

namespace graph_01

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

Page 38: Laborator instructiuni repetitive - UMFST

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_gri;

System.Drawing.Pen Creion_pic;

int i, w = 400, h = 220, val, val_v, val_max = 1024, x0 = 75, y0 =

20, poz;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_pic = new System.Drawing.Pen(this.BackColor);

Creion_gri = new System.Drawing.Pen(Color.LightGray);

Desen.DrawRectangle(Creion_albastru, x0 - 2, y0 - 2, w + 10, h +

4);

this.trackBar1.Maximum = val_max;

}

private void timer1_Tick(object sender, EventArgs e)

{

val_v = val;

val = System.Convert.ToInt16(this.trackBar1.Value);

this.label1.Text =

System.Convert.ToString(this.trackBar1.Value);

Desen.DrawLine(Creion_pic, x0 + poz + 1, y0, x0 + poz + 1, y0 +

h);

Desen.DrawLine(Creion_albastru, x0 + poz + 2, y0, x0 + poz + 2,

y0 + h - 1);

val = System.Convert.ToInt16(System.Convert.ToDouble(val) *

(System.Convert.ToDouble(h) / System.Convert.ToDouble(val_max)));

Desen.DrawLine(Creion_rosu, x0 + poz - 1, y0 + h - val_v, x0 +

poz, y0 + h - val);

// grid orizontal

i = 0;

do

{

Desen.DrawLine(Creion_gri, x0, i, w + x0, i);

i = i + 30;

}

while (i <= h);

// grid vertical

i = 0;

do

{

Desen.DrawLine(Creion_gri, x0 + i, y0, x0 + i, y0 + h);

i = i + 30;

}

while (i <= w);

poz++;

if (poz - 2 > w)

{

Desen.DrawLine(Creion_pic, x0 + poz + 1, y0, x0 + poz + 1,

y0 + h);

poz = 0;

Page 39: Laborator instructiuni repetitive - UMFST

Desen.DrawLine(Creion_pic, x0 + poz, y0, x0 + poz, y0 + h);

poz = 1;

Desen.DrawLine(Creion_pic, x0 + poz, y0, x0 + poz, y0 + h);

i = 0;

}

}

}

}

Instructiunea FOR

Afisare 100 numere

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

cout <<" \n\tNumerele sunt: ";

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

{

cout << i << " ";

}

cin.ignore();

cin.get();

return 0;

}

Afisare n numere

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

Page 40: Laborator instructiuni repetitive - UMFST

{

int n;

cout <<" \n\tCate numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\tCele " << n << " numere\n";

for (int i=1;i <= n;i++)

{

cout << i << " ";

}

cin.ignore();

cin.get();

return 0;

}

Patratul primelor n numere

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n;

cout <<" \n\tPatratul cator numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\tPatratele celor "<< n << " numere\n";

for (int i=1;i <= n;i++)

{

cout << i*i << " ";

}

cin.ignore();

cin.get();

return 0;

}

Patratul primelor n numere pana se introduce n=0

#include "stdafx.h"

Page 41: Laborator instructiuni repetitive - UMFST

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

while(n!=0){

cout <<" \n\Patratul cator numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\Patratele celor "<< n <<" numere: \n\n";

for (int i=1;i <= n;i++)

{

cout << i*i << " ";

}

cin.ignore();

cin.get();

}

return 0;

}

Afisare n numere, cate 15 pe linie

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n;

int i;

int j;

const int nr=15;

cout <<" \n\Cate numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\Cele "<< n <<" numere sunt: \n\n";

while(i <= n){

for (j=1;j <= nr;j++)

{

cout << i << " ";

i+=1;

}

cout <<"\n";

}

cin.ignore();

cin.get();

return 0;

Page 42: Laborator instructiuni repetitive - UMFST

}

Afisare n numere, cate 15 pe linie pana se introduce 0

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

int i;

int j;

while(n!=0){

i=0;

const int nr=15;

cout <<" \n\Cate numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\Cele "<< n <<" numere sunt: \n\n";

while(i <= n){

for (j=1;j <= nr;j++)

{

cout << i << " ";

i+=1;

}

cout <<"\n";

}

cin.ignore();

cin.get();

}

return 0;

}

Aplicatia anterioara corectata (exact n numere nu mai mult)

#include "stdafx.h"

#include < iostream >

using namespace std;

Page 43: Laborator instructiuni repetitive - UMFST

int main(void)

{

int n=1;

int i;

int j;

while(n!=0){

i=0;

const int nr=15;

cout <<" \n\Cate numere vreti sa afisam?: ";

cin >> n;

cout <<" \n\Cele "<< n <<" numere sunt: \n\n";

while(i <= n){

for (j=1;((j <= nr)&&(i <= n));j++)

{

cout << i << " ";

i+=1;

}

cout <<"\n";

}

cin.ignore();

cin.get();

}

return 0;

}

Numar prim?

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

while(n!=0){

cout <<" \n\Ce numar vreti sa verificam?: ";

cin >> n;

int prim=1;

for (int i=2;i < n;i++)

{

if(n%i==0)

prim=0;

}

if (prim==1)

cout << "Numarul " << n << " este prim";

else

Page 44: Laborator instructiuni repetitive - UMFST

cout << "Numarul " << n << " nu este prim";

cin.ignore();

cin.get();

}

return 0;

}

Numarul nu este prim, si continua sa calculeze -demo pentru 7 si 8 de ex

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

while(n!=0){

cout <<" \n\Ce numar vreti sa verificam?: ";

cin >> n;

int prim=1;

for (int i=2;i < n;i++)

{

if(n%i==0)

prim=0;

cout << i << "\n";

}

if (prim==1)

cout << "Numarul " << n << " este prim";

else

cout << "Numarul " << n << " nu este prim";

cin.ignore();

cin.get();

}

return 0;

}

Numarul nu este prim, sa nu mai continue- demo pentru 7 si 8 de ex

Page 45: Laborator instructiuni repetitive - UMFST

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

while(n!=0){

cout <<" \n\Ce numar vreti sa verificam?: ";

cin >> n;

int prim=1;

for (int i=2;((i < n)&&(prim!=0));i++)

{

if(n%i==0)

prim=0;

cout << i << "\n";

}

if (prim==1)

cout << "Numarul " << n << " este prim";

else

cout << "Numarul " << n << " nu este prim";

cin.ignore();

cin.get();

}

return 0;

}

Toate numerele prime pana la n

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int n=1;

while(n!=0){

cout <<" \n\Pana la ce numar sa afisam numerele prime?: ";

cin >> n;

cout << "Numerele prime sunt: \n";

for (int j=1;j <= n;j++){

int prim=1;

for (int i=2;((i < j)&&(prim!=0));i++)

{

if(j%i==0){

prim=0;

i=n;

Page 46: Laborator instructiuni repetitive - UMFST

}

}

if (prim==1)

cout << j << " \n";

}

cin.ignore();

cin.get();

}

return 0;

}

100 de numere aleatoare

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

cout << " 100 de numere aleatoare:\n";

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

{

if(i%5==0)

cout<<"\n";

cout << rand() <<" \t";

}

cin.ignore();

cin.get();

return 0;

}

fix 23 de numere while while

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i;

Page 47: Laborator instructiuni repetitive - UMFST

int k=1;

while(k <= 23){

while ( (i < 5)&&(k <= 23)){

cout << k++ << "\t";

i++;

}

cout << ":\n";

i=0;

}

cin.ignore();

cin.get();

}

fix 23 de numere for for

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int k=1;

for(int j=1;j <= 5;j++){

for ( int i=1; ((i <= 5)&&(k <= 23)); i++){

cout << k++ << "\t";

}

cout <<":\n";

}

cin.ignore();

cin.get();

return 0;

}

73 numerele de la 100 la 172 pe 15 linii a cate 5 caractere.

// Se utilizeaza spatiul de nume std

Page 48: Laborator instructiuni repetitive - UMFST

// programul afiseaza 73 numerele de la 100 la 172 pe 15 linii a cate 5

caractere.

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int k=100;

for (int j=1; j<=10 ; j++){

for ( int i=1; i<=5; i++)

cout << k++ << " ";

cout <<":\n";

}

cin.ignore();

cin.get();

return 0;

}

Realizati un program care afiseaza numerele din figura de jos:

#include "stdafx.h"

#include < iostream >

using namespace std;

int main(void)

{

int i=1;

int k=100;

int m=200;

while ((k <= 149) || (m <= 239)) {

while ((i <= 5)&&(k <= 149)){

cout << k++ << " ";

i++;

Page 49: Laborator instructiuni repetitive - UMFST

}

i=1;

cout <<"\t\t";

while (( i<= 4)&&(m <= 239)){

cout << m++ << " ";

i++;

}

cout << "\n";

i=1;

}

cin.ignore();

cin.get();

return 0;

}

Instructiunea do while o Realizati un program care calculeaza integrala functiei x3 pe intervalul x1 x2

o Realizati un program care calculeaza integrala functiei x3 pe intervalul 0-10

utilizand metoda trapezului.

o Realizati un program C# WFA care afiseaza graficul din figura de jos:

Page 50: Laborator instructiuni repetitive - UMFST

namespace graph_01

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_gri;

System.Drawing.Pen Creion_pic;

int i, w = 400, h = 220, val, val_v, val_max = 1024, x0 = 75, y0 =

20, poz;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_pic = new System.Drawing.Pen(this.BackColor);

Creion_gri = new System.Drawing.Pen(Color.LightGray);

Desen.DrawRectangle(Creion_albastru, x0 - 2, y0 - 2, w + 10, h +

4);

this.trackBar1.Maximum = val_max;

}

private void timer1_Tick(object sender, EventArgs e)

{

val_v = val;

val = System.Convert.ToInt16(this.trackBar1.Value);

this.label1.Text =

System.Convert.ToString(this.trackBar1.Value);

Desen.DrawLine(Creion_pic, x0 + poz + 1, y0, x0 + poz + 1, y0 +

h);

Desen.DrawLine(Creion_albastru, x0 + poz + 2, y0, x0 + poz + 2,

y0 + h - 1);

val = System.Convert.ToInt16(System.Convert.ToDouble(val) *

(System.Convert.ToDouble(h) / System.Convert.ToDouble(val_max)));

Desen.DrawLine(Creion_rosu, x0 + poz - 1, y0 + h - val_v, x0 +

poz, y0 + h - val);

// grid orizontal

i = 0;

do

{

Desen.DrawLine(Creion_gri, x0, i, w + x0, i);

i = i + 30;

}

while (i <= h);

// grid vertical

i = 0;

do

{

Page 51: Laborator instructiuni repetitive - UMFST

Desen.DrawLine(Creion_gri, x0 + i, y0, x0 + i, y0 + h);

i = i + 30;

}

while (i <= w);

poz++;

if (poz - 2 > w)

{

Desen.DrawLine(Creion_pic, x0 + poz + 1, y0, x0 + poz + 1,

y0 + h);

poz = 0;

Desen.DrawLine(Creion_pic, x0 + poz, y0, x0 + poz, y0 + h);

poz = 1;

Desen.DrawLine(Creion_pic, x0 + poz, y0, x0 + poz, y0 + h);

i = 0;

}

}

}

}

o Realizati o aplicatie grafica in care veti simula un termometru asemanator cu

imaginea de jos:

Page 52: Laborator instructiuni repetitive - UMFST

namespace termo_01

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_gri;

System.Drawing.SolidBrush Pens_r;

System.Drawing.SolidBrush Pens_pic;

Int16 j,val, val_max = 500, x0 = 100, y0 = 50, w = 10, h = 200;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_gri = new System.Drawing.Pen(System.Drawing.Color.Gray);

Pens_r = new

System.Drawing.SolidBrush(System.Drawing.Color.Red);

Pens_pic = new System.Drawing.SolidBrush(this.BackColor);

Desen.Clear(this.BackColor);

this.trackBar1.Maximum = val_max;

Desen.DrawRectangle(Creion_albastru, x0-1, y0-1, w+1,

h+1);//desenez contur termometru

j = 0;

do// desenez gradatii

{

if (j % 25 == 0)

{

Desen.DrawLine(Creion_gri, x0 + w + 2, y0 + j, x0 + w +

12, y0 + j);

}

else

{

Desen.DrawLine(Creion_gri, x0 + w + 2, y0 + j, x0 + w +

7, y0 + j);

}

j += 5;

} while (j <= h);

}

private void trackBar1_Scroll(object sender, EventArgs e)

{

Page 53: Laborator instructiuni repetitive - UMFST

val = System.Convert.ToInt16(System.Convert.ToDouble(h) /

System.Convert.ToDouble(val_max) *

System.Convert.ToDouble(this.trackBar1.Value));

Desen.FillRectangle(Pens_pic, x0, y0, w - 1, h - 1);

this.label1.Text =

System.Convert.ToString(this.trackBar1.Value);

Desen.FillRectangle(Pens_r, x0 + 1, h + y0 - val, w - 2, val);

}

}

}

Instrunea FOR in WFA

Tabla de sah

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::SolidBrush^ Pensula1;

Pensula1=gcnew System::Drawing::SolidBrush(System::Drawing::Color::Black);

System::Drawing::SolidBrush^ Pensula2;

Pensula2=gcnew System::Drawing::SolidBrush(System::Drawing::Color::White);

Page 54: Laborator instructiuni repetitive - UMFST

for(int i=1;i<=8;i++){

for(int j=1;j<=8;j++){

int a=i*25;

int b=j*25;

if((i+j)%2==0)

Desen->FillRectangle(Pensula1, a, b, 25, 25);

if((i+j)%2==1)

Desen->FillRectangle(Pensula2, a, b, 25, 25);

}

}

Tabla de sah scalabila (redimensionabila)

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::SolidBrush^ Pensula1;

Pensula1=gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Black);

System::Drawing::SolidBrush^ Pensula2;

Pensula2=gcnew

System::Drawing::SolidBrush(System::Drawing::Color::White);

Desen->Clear(this->BackColor);

for(int i=1;i<=8;i++){

for(int j=1;j<=8;j++){

int w=this->Width/11;

int a=i*w;

int b=j*w;

if((i+j)%2==0)

Desen->FillRectangle(Pensula1, a, b,

w, w);

else

Desen->FillRectangle(Pensula2, a, b,

w, w);

}

}

Avand x0, y0,w,h si numarul de patratele realizati o aplicatie asemanatoare imaginii

de jos:

Page 55: Laborator instructiuni repetitive - UMFST

namespace liniute

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

int x0 = 10, y0 = 25, w = 550, h = 400, i = 0;

int nc=20; // numarul de coloane

int lp = w / nc;// latimea unui patratel

int nl = h / lp;// numarul de linii

System.Drawing.Graphics Desen;

Desen = this.CreateGraphics();

Page 56: Laborator instructiuni repetitive - UMFST

System.Drawing.Pen Creion_a, Creion_r, Creion_gr;

Creion_a = new System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_r = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_gr = new

System.Drawing.Pen(System.Drawing.Color.LightGray);

// Contur

Desen.DrawRectangle(Creion_a, x0-1, y0-1, w, h);

//Grid

for (i = 0; i <= nc; i++)

{

Desen.DrawLine(Creion_gr, x0 + i * lp, y0, x0 + i * lp,

y0+h);

}

for (i = 0; i <= nl; i++)

{

Desen.DrawLine(Creion_gr, x0, y0+i*lp, x0 + w, y0 + i*lp);

}

// Liniute /

y0 = y0 + lp;

for (i = 0; i < nc; i++)

{

Desen.DrawLine(Creion_r, x0+i*lp, y0+lp, x0 + lp+i*lp, y0);

}

// Liniute \

y0 = y0 + 2*lp;

for (i = 0; i < nc; i++)

{

Desen.DrawLine(Creion_r, x0 + i * lp, y0, x0 + lp + i * lp,

y0+lp);

}

// X

y0 = y0 + 2 * lp;

for (i = 0; i < nc; i+=2)

{

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp, x0 + lp + i *

lp, y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0, x0 + lp + i * lp,

y0 + lp);

}

// +

y0 = y0 + 2 * lp;

for (i = 0; i < nc; i += 2)

{

Desen.DrawLine(Creion_r, x0 + lp / 2 + i * lp, y0 + lp, x0 +

lp/2 + i * lp, y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp / 2, x0 + lp +

i * lp, y0 + lp / 2);

}

// |\

y0 = y0 + 2 * lp;

for (i = 0; i < nc; i ++)

{

Page 57: Laborator instructiuni repetitive - UMFST

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp, x0 + i * lp,

y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0, x0 + lp + i * lp,

y0+lp);

}

// /|

y0 = y0 + 2 * lp;

for (i = 0; i < nc; i++)

{

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp, x0 + i * lp,

y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp, x0 + lp + i *

lp, y0);

}

// *

y0 = y0 + 2 * lp;

for (i = 0; i < nc; i += 2)

{

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp, x0 + lp + i *

lp, y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0, x0 + lp + i * lp,

y0 + lp);

Desen.DrawLine(Creion_r, x0 + lp / 2 + i * lp, y0 + lp, x0 +

lp / 2 + i * lp, y0);

Desen.DrawLine(Creion_r, x0 + i * lp, y0 + lp / 2, x0 + lp +

i * lp, y0 + lp / 2);

}

}

}

}

WFA-Aplicatie grafica pentru a desena axa ox oy cu diviziuni -realizat cu for

Page 58: Laborator instructiuni repetitive - UMFST

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_blu ;

System::Drawing::Pen^ Creion_rosu ;

Creion_blu=gcnew System::Drawing::Pen(System::Drawing::Color::Blue);

Creion_rosu=gcnew System::Drawing::Pen(System::Drawing::Color::Red);

Desen= this->CreateGraphics();

Desen->Clear(System::Drawing::Color(this->BackColor));

int w=this->Size.Width;

int h=this->Size.Height;

Desen->DrawLine( Creion_rosu,10,20, 10, h);

Desen->DrawLine(Creion_rosu,0,h-45, w, h-45);

for (int j=10;j < w-30;j+=10)

Desen->DrawLine(Creion_blu,10+j,h-43, 10+j, h-47);

for (int j=10;j < h-65;j+=10)

Desen->DrawLine(Creion_blu,8,20+j,12,20+j);

}

WFA-Aplicatie grafica pentru a desena 10 reptunghiuri

Page 59: Laborator instructiuni repetitive - UMFST

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

int i=0;

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

Desen->Clear(System::Drawing::Color(this->BackColor));

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew System::Drawing::Pen(System::Drawing::Color::Blue);

i=0;

for ( int i=1; i<=10; i++){

Desen->DrawRectangle(Creion_albastru,100+10*i,50+10*i,200,100);

}

delete Creion_albastru;

delete Desen;

}

WFA-Aplicatie grafica pentru a desena dereptunghiuri de dimensiuni si culori

aleatoare

Page 60: Laborator instructiuni repetitive - UMFST

private: System::Void Form1_ResizeEnd(System::Object^ sender,

System::EventArgs^ e) {

int i=0;

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Random^ n = gcnew System::Random();

Desen->Clear(System::Drawing::Color(this->BackColor));

i=0;

for ( int i=1; i<=100; i++){

System::Drawing::Pen^ Creion;

Creion=gcnew System::Drawing::Pen(System::Drawing::Color::FromArgb(n-

>Next(256),n->Next(256),n->Next(256)));

Desen->DrawRectangle(Creion,n->Next(this->Width),n->Next(this->Height),n-

>Next(75),n->Next(50));

Desen->DrawEllipse(Creion,n->Next(this->Width),n->Next(this->Height),n-

>Next(50),n->Next(75));

delete Creion;

}

delete Desen;

}

WFA-Aplicatie grafica pentru a desena cercuri inscrise

namespace cerc_p_c

{

Page 61: Laborator instructiuni repetitive - UMFST

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

Graphics desen;

private void Form1_Paint(object sender, PaintEventArgs e)

{

desen = CreateGraphics();

Pen bluePen = new Pen(Color.Blue);

Pen redPen = new Pen(Color.Red);

float L, indent;

L = Math.Min(this.Width - 40, this.Height - 40);

indent = 0;

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

{

desen.DrawEllipse(bluePen, indent + 20, indent + 20, L, L);

desen.DrawRectangle(redPen, indent + 20, indent + 20, L, L);

indent += L / 2 - L / (float)Math.Sqrt(8);

L = L - (L / 2 - L / (float)Math.Sqrt(8)) * 2;

}

}

private void Form1_Resize(object sender, EventArgs e)

{

desen.Clear(this.BackColor);

this.Refresh();

}

}

}

Realizati o aplicatie grafica reprezentand un instrument virtual de forma:

Page 62: Laborator instructiuni repetitive - UMFST

namespace instrum_02

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_pic;

System.Drawing.Pen Creion_gri;

System.Drawing.Font font_nina;

System.Drawing.SolidBrush Pens_r;

Int16 j,val, val_v, val_max = 400, x0 = 20, y0 = 30, w = 250, h1=10;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Page 63: Laborator instructiuni repetitive - UMFST

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_pic = new System.Drawing.Pen(this.BackColor);

Creion_gri = new

System.Drawing.Pen(System.Drawing.Color.LightGray);

Pens_r = new

System.Drawing.SolidBrush(System.Drawing.Color.Red);

font_nina = new System.Drawing.Font("Nina", 8);

Desen.DrawRectangle(Creion_albastru, x0, y0-h1, w + x0, w + y0);

this.trackBar1.Maximum = val_max;

for (j = 0; j <= w; j += 5)// desenez gradatii

{

if (j % 25 == 0)

{

Desen.DrawLine(Creion_gri, x0 +10+ j, y0 - h1+4, x0 +

j+10, y0 - h1+17);

Desen.DrawString(System.Convert.ToString(j * val_max /

w), font_nina, Pens_r, x0 + j, y0-h1-15);

}

else

{

Desen.DrawLine(Creion_gri, x0 +10+ j, y0-h1+10, x0

+j+10, y0 -h1+17);

}

}

}

private void trackBar1_Scroll(object sender, EventArgs e)

{

val_v = val;

val = System.Convert.ToInt16(this.trackBar1.Value);

this.label1.Text =

System.Convert.ToString(this.trackBar1.Value);

val = System.Convert.ToInt16(System.Convert.ToDouble(val) *

(System.Convert.ToDouble(w) / System.Convert.ToDouble(val_max))); //scalare

Desen.DrawLine(Creion_pic, ((w+ x0) / 2 + 10), w+ y0, val_v + x0

+ 10, y0 + 10);

if (val < 0.9 * w)

Desen.DrawLine(Creion_albastru, ((w+ x0) / 2 + 10), w+ y0,

val + x0 + 10, y0 + 10);

else

Desen.DrawLine(Creion_rosu, ((w+ x0) / 2 + 10), w+ y0, val +

x0 + 10, y0 + 10);

}

}

}

WFA-Aplicatie grafica pentru a desena n raze in cerc

Page 64: Laborator instructiuni repetitive - UMFST

Desenare cerc

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew System::Drawing::Pen(System::Drawing::Color::Blue);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

C# Desenare cerc

namespace cerc

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

int x0, y0, d;

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru,x0,y0,d,d);

}

Page 65: Laborator instructiuni repetitive - UMFST

}

}

Trasarea unei raze intr-o pozitie oarecare intr-un cerc

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew System::Drawing::Pen(System::Drawing::Color::Blue);

System::Drawing::Pen^ Creion_rosu;

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

int xc=x0+d/2;

int yc=y0+d/2;

int alfa_g=40;// unghiul in grade

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in radiani

int x=xc+d/2*System::Math::Cos(alfa_r);

int y=yc-d/2*System::Math::Sin(alfa_r);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

C# Trasarea unei raze intr-o pozitie oarecare intr-un cerc

Page 66: Laborator instructiuni repetitive - UMFST

namespace raza_cerc

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

int x0, y0, d, xc, yc, x, y;

double alfa_g, alfa_r;

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

alfa_g = 40;// unghiul in grade

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in radiani

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

Page 67: Laborator instructiuni repetitive - UMFST

xc = x0 + d / 2;

yc = y0 + d / 2;

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

}

}

Trasarea 12 raze echidistante intr-un cerc

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

System::Drawing::Pen^ Creion_rosu;

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

Page 68: Laborator instructiuni repetitive - UMFST

int xc=x0+d/2;

int yc=y0+d/2;

int x,y;

int alfa_g;// unghiul in grade

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_gDrawLine(Creion_rosu,xc,yc,x,y);

}

N raze echidistante intr-un cerc "C#"

namespace Mot_cerc_v0

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

int x, y, x0, y0, d, xc, yc;

double alfa_g,alfa_r;

private void Form1_Load(object sender, EventArgs e)

{

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

Desen = this.CreateGraphics();

Creion_albastru =new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu =new

System.Drawing.Pen(System.Drawing.Color.Red);

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

for (alfa_g = 0; alfa_g < 360; alfa_g += 6)

{

Page 69: Laborator instructiuni repetitive - UMFST

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

}

}

}

Trasarea 12 raze echidistante intr-un cerc. Se vor evinentia razele din 5 in cinci cu o

culoare distincta astfel:

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

System::Drawing::Pen^ Creion_rosu;

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

int xc=x0+d/2;

int yc=y0+d/2;

Page 70: Laborator instructiuni repetitive - UMFST

int x,y;

int alfa_g;// unghiul in grade

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

else

Desen->DrawLine(Creion_albastru,xc,yc,x,y);

}

Realizati un cadran de forma:

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

System::Drawing::Pen^ Creion_rosu;

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

System::Drawing::SolidBrush^ Pensula;

Pensula=gcnew System::Drawing::SolidBrush(this->BackColor);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

Page 71: Laborator instructiuni repetitive - UMFST

int xc=x0+d/2;

int yc=y0+d/2;

int x,y;

int alfa_g;// unghiul in grade

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

else

Desen->DrawLine(Creion_albastru,xc,yc,x,y);

}

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Versiunea II-a

System::Drawing::Graphics^ Desen;

Desen = this->CreateGraphics();

System::Drawing::Pen^ Creion_albastru;

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

System::Drawing::Pen^ Creion_rosu;

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

int x0=this->Width/5;

int y0=this->Height/5;

int d=this->Width/2;

int di=d-10;

int xc=x0+d/2;

int yc=y0+d/2;

int x1,y1,x2,y2;

int alfa_g;// unghiul in grade

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

y1=yc-d/2*System::Math::Sin(alfa_r);

x1=xc+d/2*System::Math::Cos(alfa_r);

y2=yc-di/2*System::Math::Sin(alfa_r);

x2=xc+di/2*System::Math::Cos(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,x1,y1,x2,y2);

else

Page 72: Laborator instructiuni repetitive - UMFST

Desen->DrawLine(Creion_albastru,x1,y1,x2,y2);

}

Folosind un timer afisati cu temporizare de 1 secunda 60 de raze echidistante intru-un

cerc

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_albastru;

System::Drawing::Pen^ Creion_rosu;

int x,y,x0,y0,xc,yc,x1,y1,x2,y2,i,d,di,alfa_g;

double alfa_r;

#pragma endregion

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

x0=this->Width/5;

y0=this->Height/5;

d=this->Width/2;

di=d-10;

xc=x0+d/2;

yc=y0+d/2;

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

y1=yc-d/2*System::Math::Sin(alfa_r);

x1=xc+d/2*System::Math::Cos(alfa_r);

Page 73: Laborator instructiuni repetitive - UMFST

y2=yc-di/2*System::Math::Sin(alfa_r);

x2=xc+di/2*System::Math::Cos(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,x1,y1,x2,y2);

else

Desen->DrawLine(Creion_albastru,x1,y1,x2,y2);

}

}

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

i=i-360/60;

if (i<-360)

i=0;

}

Reluati aplicatia anterioara dar stergeti razele anterioare pentru a simula o raza care se

roteste

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_albastru;

System::Drawing::Pen^ Creion_rosu;

System::Drawing::SolidBrush^ Pensula;

int x,y,x0,y0,xc,yc,x1,y1,x2,y2,i,d,di,alfa_g;

double alfa_r;

#pragma endregion

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

Pensula=gcnew System::Drawing::SolidBrush(this->BackColor);

x0=this->Width/5;

y0=this->Height/5;

d=this->Width/2;

di=d-10;

xc=x0+d/2;

yc=y0+d/2;

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

Page 74: Laborator instructiuni repetitive - UMFST

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

y1=yc-d/2*System::Math::Sin(alfa_r);

x1=xc+d/2*System::Math::Cos(alfa_r);

y2=yc-di/2*System::Math::Sin(alfa_r);

x2=xc+di/2*System::Math::Cos(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,x1,y1,x2,y2);

else

Desen->DrawLine(Creion_albastru,x1,y1,x2,y2);

}

}

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

i=i-360/60;

if (i<-360)

i=0;

}

namespace radar_v0

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

int x0, y0, d, xc, yc, x, y;

double alfa_g, alfa_r;

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_pic;

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Page 75: Laborator instructiuni repetitive - UMFST

Creion_pic = new System.Drawing.Pen(this.BackColor);

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

alfa_g = 90;// unghiul in grade

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in radiani

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

Desen.DrawEllipse(Creion_albastru, x0 + 20, y0 + 20, d - 40, d -

40);

//Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

private void timer1_Tick(object sender, EventArgs e)

{

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + ((d / 2) - 25) *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - ((d / 2) - 25) *

System.Math.Sin(alfa_r));

Desen.DrawLine(Creion_pic, xc, yc, x, y);

alfa_g = alfa_g - 6;

if (alfa_g == 0)

alfa_g = 360;

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + ((d / 2) - 25) *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - ((d / 2) - 25) *

System.Math.Sin(alfa_r));

Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

}

}

Page 76: Laborator instructiuni repetitive - UMFST

Simulati radarul din imaginea de sus

namespace radar_v1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.SolidBrush Radiera;

int x0, y0, r, xc, yc, xm, ym, a, ai, d, rc, ds;

double ar;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(Color.Red);

Radiera = new System.Drawing.SolidBrush(this.BackColor);

d = 10;

x0 = d;

y0 = d;

r = 3 * this.Width / 7;

xc = x0 + r;

yc = y0 + r;

a = 0; // unghiul alfa in grade

ar = 0; // unghiul alfa in radiani

ai = 0; // unghiul alfa initial in grade

ds = 2 * r / 7; // rata cu care scade diametrul

Page 77: Laborator instructiuni repetitive - UMFST

rc = 0; // rata cu care creste pozitia pe x si pe y

Desen.DrawEllipse(Creion_albastru, x0, y0, 2 * r, 2 * r);

}

private void timer1_Tick(object sender, EventArgs e)

{

// cercuri concentrice

Desen.FillEllipse(Radiera, x0 + 1, y0 + 1, 2 * r - 2, 2 * r -

2);

rc = 0;

for (d = 2 * r; d >= 0; d -= ds)

{

Desen.DrawEllipse(Creion_albastru, x0 + rc, y0 + rc, d, d);

rc += ds / 2;

}

//raze in cerc

for (a = 0; a < 360; a += 360 / 12)

{

ar = a * Math.PI / 180;

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

Desen.DrawLine(Creion_rosu, xc, yc, xm, ym);

}

// fascicul de raze

for (a = ai; a < (ai + 30); a += 360 / 120)

{

ar = a * Math.PI / 180;

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

if (a % 30 == 0)

Desen.DrawLine(Creion_rosu, xc, yc, xm, ym);

else

Desen.DrawLine(Creion_albastru, xc, yc, xm, ym);

}

ai += 10;

}

}

}

Pentru a evita clipirea imaginii vom folosi obiectul imagine

namespace radar_v2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

Page 78: Laborator instructiuni repetitive - UMFST

}

System.Drawing.Graphics Desen,g;

System.Drawing.Bitmap img;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

int x0, y0, r, xc, yc, xm, ym, a, ai, d, rc, ds;

double ar;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(Color.Red);

d = 10;

x0 = d;

y0 = d;

r = 3 * this.Width / 7;

xc = x0 + r;

yc = y0 + r;

img = new Bitmap(2*(r+d), 2*(r+d));

g = Graphics.FromImage(img);

a = 0; // unghiul alfa in grade

ar = 0; // unghiul alfa in radiani

ai = 0; // unghiul alfa initial in grade

ds = 2 * r / 7; // rata cu care scade diametrul

rc = 0; // rata cu care creste pozitia pe x si pe y

}

private void timer1_Tick(object sender, EventArgs e)

{

g.Clear(BackColor);

g.DrawEllipse(Creion_albastru, x0, y0, 2 * r, 2 * r);

// cercuri concentrice

rc = 0;

for (d = 2 * r; d >= 0; d -= ds)

{

g.DrawEllipse(Creion_albastru, x0 + rc, y0 + rc, d, d);

rc += ds / 2;

}

//raze in cerc

for (a = 0; a < 360; a += 360 / 12)

{

ar = a * Math.PI / 180;

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

g.DrawLine(Creion_rosu, xc, yc, xm, ym);

}

// fascicul de raze

for (a = ai; a < (ai + 30); a += 360 / 120)

{

ar = a * Math.PI / 180;

Page 79: Laborator instructiuni repetitive - UMFST

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

if (a % 30 == 0)

g.DrawLine(Creion_rosu, xc, yc, xm, ym);

else

g.DrawLine(Creion_albastru, xc, yc, xm, ym);

}

Desen.DrawImage(img, 0, 0);

ai += 10;

}

}

}

Pentru a evita clipirea imaginii vom folosi pictureBox si obiectul imagine

namespace radar_v3

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics g;

System.Drawing.Bitmap img;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

int x0, y0, r, xc, yc, xm, ym, a, ai, d, rc, ds;

double ar;

private void Form1_Paint(object sender, PaintEventArgs e)

{

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(Color.Red);

d = 10;

x0 = d;

y0 = d;

r = 3 * this.pictureBox1.Width / 7;

xc = x0 + r;

yc = y0 + r;

img = new Bitmap(2 * (r + d), 2 * (r + d));

g = Graphics.FromImage(img);

a = 0; // unghiul alfa in grade

ar = 0; // unghiul alfa in radiani

ai = 0; // unghiul alfa initial in grade

ds = 2 * r / 7; // rata cu care scade diametrul

rc = 0; // rata cu care creste pozitia pe x si pe y

}

Page 80: Laborator instructiuni repetitive - UMFST

private void timer1_Tick(object sender, EventArgs e)

{

g.Clear(BackColor);

g.DrawEllipse(Creion_albastru, x0, y0, 2 * r, 2 * r);

// cercuri concentrice

rc = 0;

for (d = 2 * r; d >= 0; d -= ds)

{

g.DrawEllipse(Creion_albastru, x0 + rc, y0 + rc, d, d);

rc += ds / 2;

}

//raze in cerc

for (a = 0; a < 360; a += 360 / 12)

{

ar = a * Math.PI / 180;

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

g.DrawLine(Creion_rosu, xc, yc, xm, ym);

}

// fascicul de raze

for (a = ai; a < (ai + 30); a += 360 / 120)

{

ar = a * Math.PI / 180;

xm = System.Convert.ToInt16(xc + r * Math.Sin(ar));

ym = System.Convert.ToInt16(yc - r * Math.Cos(ar));

if (a % 30 == 0)

g.DrawLine(Creion_rosu, xc, yc, xm, ym);

else

g.DrawLine(Creion_albastru, xc, yc, xm, ym);

}

pictureBox1.Image = img;

ai += 10;

}

}

}

Utilizati ceasul sistem pentru a simula un secundar de ceas

Secundarul se roteste in sens trigonometric

Page 81: Laborator instructiuni repetitive - UMFST

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_albastru;

System::Drawing::Pen^ Creion_rosu;

System::Drawing::SolidBrush^ Pensula;

int x,y,x0,y0,xc,yc,x1,y1,x2,y2,i,d,di,alfa_g;

double alfa_r;

#pragma endregion

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

Creion_rosu =gcnew System::Drawing::Pen(System::Drawing::Color::Red);

Pensula=gcnew System::Drawing::SolidBrush(this->BackColor);

x0=this->Width/5;

y0=this->Height/5;

d=this->Width/2;

di=d-10;

xc=x0+d/2;

yc=y0+d/2;

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_albastru,x0,y0,d,d);

for (alfa_g=0;alfa_g < 360;alfa_g+=6){

double alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

y1=yc-d/2*System::Math::Sin(alfa_r);

x1=xc+d/2*System::Math::Cos(alfa_r);

y2=yc-di/2*System::Math::Sin(alfa_r);

x2=xc+di/2*System::Math::Cos(alfa_r);

if (alfa_g%30==0)

Desen->DrawLine(Creion_rosu,x1,y1,x2,y2);

else

Desen->DrawLine(Creion_albastru,x1,y1,x2,y2);

}

}

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

i=6*System::DateTime::Now.Second;

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

}

Secundarul se roteste in sens direct

Page 82: Laborator instructiuni repetitive - UMFST

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

i=90-6*System::DateTime::Now.Second;

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

}

Adaugati si un minutar

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

i=90-6*System::Convert::ToInt16(System::DateTime::Now.Second);

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

i=90-6*System::Convert::ToInt16(System::DateTime::Now.Minute);

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->DrawLine(Creion_albastru,xc,yc,x,y);

}

Realizati un ceas de forma: - "ceas_v1"

Page 83: Laborator instructiuni repetitive - UMFST

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

i=90-6*System::Convert::ToInt16(System::DateTime::Now.Second);

alfa_r=2*System::Math::PI*i/360;

x=xc+d/2*System::Math::Cos(alfa_r);

y=yc-d/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pensula,x0+5,y0+5,d-10,d-10);

Desen->DrawLine(Creion_albastru,xc,yc,x,y);

i=90-6*System::Convert::ToInt16(System::DateTime::Now.Minute);

alfa_r=2*System::Math::PI*i/360;

x=xc+(d-20)/2*System::Math::Cos(alfa_r);

y=yc-(d-20)/2*System::Math::Sin(alfa_r);

Desen->DrawLine(Creion_albastru,xc,yc,x,y);

i=90-

(30*System::Convert::ToInt16(System::DateTime::Now.Hour)+System::Convert::ToInt16(System::DateTime::Now.Minute)/2);

alfa_r=2*System::Math::PI*i/360;

x=xc+(d-30)/2*System::Math::Cos(alfa_r);

y=yc-(d-30)/2*System::Math::Sin(alfa_r);

Desen->DrawLine(Creion_rosu,xc,yc,x,y);

}

# "ceas_v3"

namespace ceas_v3

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics desen;

System.Drawing.Pen creion;

System.Drawing.Pen creion2;

System.Drawing.SolidBrush pensula;

System.Drawing.Pen creionminut;

System.Drawing.Pen creionora;

System.Drawing.SolidBrush pensulacerc;

int x, y, x1, y1, d, x2, y2, i, x3, y3; double g, r;

private void Form1_Paint(object sender, PaintEventArgs e)

Page 84: Laborator instructiuni repetitive - UMFST

{

x = this.Width / 5;

y = this.Height / 5;

d = this.Width / 2;

x1 = x + d / 2;

y1 = y + d / 2;

desen = this.CreateGraphics();

creion = new System.Drawing.Pen(System.Drawing.Color.Blue);

creionminut = new

System.Drawing.Pen(System.Drawing.Color.White);

creionora = new System.Drawing.Pen(System.Drawing.Color.Orange);

creion2 = new System.Drawing.Pen(System.Drawing.Color.Red);

pensula = new System.Drawing.SolidBrush(this.BackColor);

pensulacerc = new

System.Drawing.SolidBrush(System.Drawing.Color.DarkBlue);

desen.Clear(this.BackColor);

desen.DrawEllipse(creion, x, y, d, d);

for (g = 0; g < 360; g += 6)

{

double r = 2 * System.Math.PI * g / 360;

x2 = Convert.ToInt16(x1 + (d / 2) * System.Math.Cos(r));

y2 = Convert.ToInt16(y1 - (d / 2) * System.Math.Sin(r));

desen.DrawLine(creion2, x1, y1, x2, y2);

if (g % 30 == 0)

desen.DrawLine(creion2, x1, y1, x2, y2);

else

desen.DrawLine(creion, x1, y1, x2, y2);

}

desen.FillEllipse(pensula, x + 5, y + 5, d - 10, d - 10);

}

private void timer1_Tick(object sender, EventArgs e)

{

//secundar

i = 90 - 6 * System.DateTime.Now.Second;

r = (2 * System.Math.PI * i) / 360;

x3 = Convert.ToInt16(x1 + (d / 2 - 6) * System.Math.Cos(r));

y3 = Convert.ToInt16(y1 - (d / 2 - 6) * System.Math.Sin(r));

desen.FillEllipse(pensula, x + 5, y + 5, d - 10, d - 10);

desen.DrawLine(creion2, x1, y1, x3, y3);

//minutar

i = 90 - 6 * System.DateTime.Now.Minute;

r = (2 * System.Math.PI * i) / 360;

x3 = Convert.ToInt16(x1 + (d / 2 - 10) * System.Math.Cos(r));

y3 = Convert.ToInt16(y1 - (d / 2 - 10) * System.Math.Sin(r));

desen.DrawLine(creionminut, x1, y1, x3, y3);

//ora

i = 90 - (30 * System.Convert.ToInt16(System.DateTime.Now.Hour)

+ System.Convert.ToInt16(System.DateTime.Now.Minute) / 2);

r = (2 * System.Math.PI * i) / 360;

x3 = Convert.ToInt16(x1 + (d / 2 - 35) * System.Math.Cos(r));

y3 = Convert.ToInt16(y1 - (d / 2 - 35) * System.Math.Sin(r));

desen.DrawLine(creionora, x1, y1, x3, y3);

desen.FillEllipse(pensulacerc, x1 - 5, y1 - 5, 10, 10);

}

}

Page 85: Laborator instructiuni repetitive - UMFST

}

simulati o roata care se invarte: - "roata"

namespace roata

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

int x0, y0, d, xc, yc, x, y;

double alfa_g, alfa_r;

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_pic;

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Page 86: Laborator instructiuni repetitive - UMFST

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_pic = new System.Drawing.Pen(this.BackColor);

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

alfa_g = 90;// unghiul in grade

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in radiani

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

x0 = this.Width / 5;

y0 = this.Height / 5;

d = this.Width / 2;

xc = x0 + d / 2;

yc = y0 + d / 2;

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

Desen.DrawEllipse(Creion_albastru, x0+20, y0+20, d-40, d-40);

//Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

private void timer1_Tick(object sender, EventArgs e)

{

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

{

alfa_r = 2 * System.Math.PI * (alfa_g+30*i) / 360;// unghiul

in radiani

x = System.Convert.ToInt16(xc + ((d / 2) - 25) *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - ((d / 2) - 25) *

System.Math.Sin(alfa_r));

Desen.DrawLine(Creion_pic, xc, yc, x, y);

}

alfa_g = alfa_g-6;

if (alfa_g == 0)

alfa_g = 360;

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

{

alfa_r = 2 * System.Math.PI * (alfa_g + 30 * i) / 360;//

unghiul in radiani

x = System.Convert.ToInt16(xc + ((d / 2) - 25) *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - ((d / 2) - 25) *

System.Math.Sin(alfa_r));

Page 87: Laborator instructiuni repetitive - UMFST

Desen.DrawLine(Creion_rosu, xc, yc, x, y);

}

}

}

}

Afisati rotatia pamantului in jurul soarelui sub forma: - "sist_sol_v0"

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_gri;

System::Drawing::SolidBrush^ Pens_r;

System::Drawing::SolidBrush^ Pens_a;

System::Drawing::SolidBrush^ Pens_pic;

static int x0; // Stanga sus

static int y0;

static int d0; // axa majora

static int d1; // axa minora

Page 88: Laborator instructiuni repetitive - UMFST

static int xc; // centrul sistemului solar

static int yc;

static int x,y; // poxitia curenta a planetei

static int r=5; // raza planetei

static int alfa_g;// unghiul in grade

static double alfa_r;// unghiul in radiani

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_gri =gcnew

System::Drawing::Pen(System::Drawing::Color::LightGray);

Pens_r =gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Red);

Pens_a =gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Blue);

Pens_pic =gcnew

System::Drawing::SolidBrush(System::Drawing::Color(this->BackColor));

x0=this->Width/5;

y0=this->Height/5;

d0=this->Width/1.5;

d1=this->d0/2;

xc=x0+d0/2;

yc=y0+d1/2;

Desen->Clear(this->BackColor);

Desen->DrawEllipse(Creion_gri,x0,y0,d0,d1); //desenez elipsa

Desen->FillEllipse(Pens_r,x0+d1/3,y0+d1/2-10,20,20); //

desenez soarele

for(alfa_g=0;alfa_g <=360;alfa_g+=360/12){

alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in

radiani

x=xc+d0/2*System::Math::Cos(alfa_r);

y=yc-d1/2*System::Math::Sin(alfa_r);

Desen->FillEllipse(Pens_a,x-r,y-r,2*r,2*r);

}

}

C#

namespace sist_sol_v0

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_gri;

Page 89: Laborator instructiuni repetitive - UMFST

System.Drawing.SolidBrush Pens_r;

System.Drawing.SolidBrush Pens_a;

System.Drawing.SolidBrush Pens_pic;

static int x0; // Stanga sus

static int y0;

static int d0; // axa majora

static int d1; // axa minora

static int xc; // centrul sistemului solar

static int yc;

static int x,y; // poxitia curenta a planetei

static int r=5; // raza planetei

static int alfa_g;// unghiul in grade

static double alfa_r;// unghiul in radiani

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_gri =new

System.Drawing.Pen(System.Drawing.Color.LightGray);

Pens_r =new

System.Drawing.SolidBrush(System.Drawing.Color.Red);

Pens_a =new

System.Drawing.SolidBrush(System.Drawing.Color.Blue);

Pens_pic =new System.Drawing.SolidBrush(this.BackColor);

x0=this.Width/5;

y0=this.Height/5;

d0=System.Convert.ToInt16(this.Width/1.5);

d1=d0/2;

xc=x0+d0/2;

yc=y0+d1/2;

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_gri,x0,y0,d0,d1); //desenez

elipsa

Desen.FillEllipse(Pens_r,x0+d1/3,y0+d1/2-10,20,20); //

desenez soarele

for(alfa_g=0;alfa_g <=360;alfa_g+=360/12){

alfa_r=2*System.Math.PI*alfa_g/360;// unghiul in

radiani

x=System.Convert.ToInt16(xc+d0/2*System.Math.Cos(alfa_r));

y=System.Convert.ToInt16(yc-

d1/2*System.Math.Sin(alfa_r));

Desen.FillEllipse(Pens_a,x-r,y-r,2*r,2*r);

}

}

}

}

Folosind un timer cu temporizare de 0,1 simlati rotatia pamantului in jurul soarelui -

"sist_sol_v1"

Page 90: Laborator instructiuni repetitive - UMFST

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_gri;

System::Drawing::SolidBrush^ Pens_r;

System::Drawing::SolidBrush^ Pens_a;

System::Drawing::SolidBrush^ Pens_pic;

static int x0; // Stanga sus

static int y0;

static int d0; // axa majora

static int d1; // axa minora

static int xc; // centrul soarelui

static int yc;

static int x,y; // poxitia curenta a planetei

static int r=5; // raza planetei

static int alfa_g;// unghiul in grade

static double alfa_r;// unghiul in radiani

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_gri =gcnew

System::Drawing::Pen(System::Drawing::Color::LightGray);

Pens_r =gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Red);

Page 91: Laborator instructiuni repetitive - UMFST

Pens_a =gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Blue);

Pens_pic =gcnew

System::Drawing::SolidBrush(System::Drawing::Color(this->BackColor));

x0=this->Width/6;

y0=this->Height/6;

d0=this->Width/1.5;

d1=this->d0/2;

xc=x0+d0/2;

yc=y0+d1/2;

Desen->Clear(this->BackColor);

Desen->FillEllipse(Pens_r,x0+d1/3,y0+d1/2-10,20,20); //

desenez soarele

}

private: System::Void timer1_Tick(System::Object^ sender,

System::EventArgs^ e) {

Desen->FillEllipse(Pens_pic,x-r,y-r,2*r,2*r); // sterg

vechiul pamant

Desen->DrawEllipse(Creion_gri,x0,y0,d0,d1); //desenez elipsa

alfa_r=2*System::Math::PI*alfa_g/360;// unghiul in radiani

x=xc+d0/2*System::Math::Cos(alfa_r);

y=yc-d1/2*System::Math::Sin(alfa_r);

alfa_g+=10;

if (alfa_g>360)

alfa_g=0;

//Desen->DrawLine(Creion_rosu,xc,yc,x,y);

Desen->FillEllipse(Pens_a,x-r,y-r,2*r,2*r);

}

C#

namespace sist_sol_v1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_gri;

System.Drawing.SolidBrush Pens_r;

System.Drawing.SolidBrush Pens_a;

System.Drawing.SolidBrush Pens_pic;

static int x0; // Stanga sus

static int y0;

static int d0; // axa majora

static int d1; // axa minora

static int xc; // centrul sistemului solar

static int yc;

Page 92: Laborator instructiuni repetitive - UMFST

static int x, y; // poxitia curenta a planetei

static int r = 5; // raza planetei

static int alfa_g;// unghiul in grade

static double alfa_r;// unghiul in radiani

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Creion_gri = new

System.Drawing.Pen(System.Drawing.Color.LightGray);

Pens_r = new

System.Drawing.SolidBrush(System.Drawing.Color.Red);

Pens_a = new

System.Drawing.SolidBrush(System.Drawing.Color.Blue);

Pens_pic = new System.Drawing.SolidBrush(this.BackColor);

x0 = this.Width / 5;

y0 = this.Height / 5;

d0 = System.Convert.ToInt16(this.Width / 1.5);

d1 = d0 / 2;

xc = x0 + d0 / 2;

yc = y0 + d1 / 2;

Desen.FillEllipse(Pens_r, x0 + d1 / 3, y0 + d1 / 2 - 10, 20,

20); // desenez soarele

}

private void timer1_Tick(object sender, EventArgs e)

{

Desen.FillEllipse(Pens_pic,x-r,y-r,2*r,2*r); // sterg vechiul

pamant

Desen.DrawEllipse(Creion_gri,x0,y0,d0,d1); //desenez

elipsa

alfa_r=2*System.Math.PI*alfa_g/360;// unghiul in radiani

x=System.Convert.ToInt16(xc+d0/2*System.Math.Cos(alfa_r));

y=System.Convert.ToInt16(yc-

d1/2*System.Math.Sin(alfa_r));

alfa_g+=10;

if (alfa_g>360)

alfa_g=0;

//Desen.DrawLine(Creion_gri,xc,yc,x,y);

Desen.FillEllipse(Pens_a,x-r,y-r,2*r,2*r);

}

}

}

Realizati o aplicatie grafica in care veti simula un termometru asemanator cu imaginea

de jos: - "termo_02"

Page 93: Laborator instructiuni repetitive - UMFST

System::Drawing::Graphics^ Desen;

System::Drawing::Pen^ Creion_albastru;

System::Drawing::Pen^ Creion_rosu;

System::Drawing::Pen^ Creion_gri;

System::Drawing::SolidBrush^ Pens_r;

System::Drawing::SolidBrush^ Pens_v;

System::Drawing::SolidBrush^ Pens_pic;

System::Drawing::Font^ font_nina;

static double val;

static double val_max=400;

static int j;

static int x0=100;

static int y0=50;

static int w=10;

static double h=200;

private: System::Void Form1_Paint(System::Object^ sender,

System::Windows::Forms::PaintEventArgs^ e) {

Desen = this->CreateGraphics();

Creion_albastru =gcnew

System::Drawing::Pen(System::Drawing::Color::Blue);

Page 94: Laborator instructiuni repetitive - UMFST

Creion_rosu =gcnew

System::Drawing::Pen(System::Drawing::Color::Red);

Creion_gri =gcnew

System::Drawing::Pen(System::Drawing::Color::LightGray);

Pens_r=gcnew

System::Drawing::SolidBrush(System::Drawing::Color::Red);

Pens_v=gcnew

System::Drawing::SolidBrush(System::Drawing::Color::LightBlue);

Pens_pic=gcnew

System::Drawing::SolidBrush(System::Drawing::Color(this->BackColor));

font_nina=gcnew System::Drawing::Font("Nina",8);

Desen->Clear(System::Drawing::Color(this->BackColor));

this->trackBar1->Maximum=val_max;

Desen->DrawRectangle(Creion_albastru,x0,y0,w,h);//desenez

contur termometru

for (j=0;j <= h ;j+=5)// desenez gradatii

if(j%25==0){

Desen->DrawLine(Creion_gri,x0+w+2,y0+j,

x0+w+12,y0+j);

Desen-

>DrawString(System::Convert::ToString(val_max-

j*val_max/h),font_nina,Pens_r,x0+w+20,y0+j-7);

}

else

Desen->DrawLine(Creion_gri,x0+w+2,y0+j,

x0+w+7,y0+j);

}

private: System::Void trackBar1_Scroll(System::Object^ sender,

System::EventArgs^ e) {

val=(h/val_max)*System::Convert::ToDouble((this->trackBar1-

>Value));

Desen->FillRectangle(Pens_pic, x0+1, y0+1, w-2, h-2);

this->label1->Text=System::Convert::ToString(this->trackBar1-

>Value);

if(valFillRectangle(Pens_v, x0+1, h+y0-val, w-2, val);

Desen->FillEllipse(Pens_v,x0-20,y0,10,10);

}

else{

Desen->FillRectangle(Pens_r, x0+1, h+y0-val, w-2,

val);

Desen->FillEllipse(Pens_r,x0-20,y0,10,10);

}

}

C#

Page 95: Laborator instructiuni repetitive - UMFST

namespace termo_02

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

System.Drawing.Pen Creion_gri;

System.Drawing.SolidBrush Pens_r;

System.Drawing.SolidBrush Pens_v;

System.Drawing.SolidBrush Pens_pic;

System.Drawing.Font font_nina;

static float val;

static float val_max = 100;

static float val_min = 0;

static float val_p = 50;

static int j;

static int x0 = 100;

static int y0 = 50;

static int w = 10;

static int h = 200;

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

Creion_gri = new

System.Drawing.Pen(System.Drawing.Color.LightGray);

Pens_r = new

System.Drawing.SolidBrush(System.Drawing.Color.Red);

Pens_v = new

System.Drawing.SolidBrush(System.Drawing.Color.LightBlue);

Pens_pic = new System.Drawing.SolidBrush(this.BackColor);

font_nina = new System.Drawing.Font("Nina", 8);

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

//h = 5 * System.Convert.ToInt16(this.Height / 8);

h = System.Convert.ToInt16(val_max) * System.Convert.ToInt16(5 *

this.Height / 8 / val_max);

this.trackBar1.Maximum = System.Convert.ToInt16(val_max);

this.trackBar1.Minimum = System.Convert.ToInt16(val_min);

Desen.DrawRectangle(Creion_albastru, x0, y0, w, h);//desenez

contur termometru

for (j = 0; j <= h; j += 5)// desenez gradatii

{

if (j % 25 == 0)

Page 96: Laborator instructiuni repetitive - UMFST

{

Desen.DrawLine(Creion_gri, x0 + w + 2, y0 + j, x0 + w +

12, y0 + j);

Desen.DrawString(System.Convert.ToString(val_max-

j*val_max/h),font_nina,Pens_r,x0+w+20,y0+j-7);

}

else

{

Desen.DrawLine(Creion_gri, x0 + w + 2, y0 + j, x0 + w +

7, y0 + j);

}

}

}

private void trackBar1_Scroll(object sender, EventArgs e)

{

val = (h / val_max) * (this.trackBar1.Value);

this.label1.Text =

System.Convert.ToString(this.trackBar1.Value);

Desen.FillRectangle(Pens_pic, x0 + 1, y0 + 1, w - 1, h - 2);

if (this.trackBar1.Value > val_p)

{

Desen.FillRectangle(Pens_r, x0 + 1, h + y0 - val, w - 1,

val);

}

else

{

Desen.FillRectangle(Pens_v, x0 + 1, h + y0 - val, w - 1,

val);

}

}

}

}

Paradoxul lui Ahile

Page 97: Laborator instructiuni repetitive - UMFST

namespace ahile

{

public partial class Form1 : Form

{

static int a,b; // pozitia lui ahile respectiv a broastei

static int w, h, v ; // h, w dimensiunile unui dreptunghi

static System.Drawing.Graphics Desen;

static System.Drawing.SolidBrush Pensula, Pensula_pic;

static System.Drawing.Pen Creion_blu ;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Pensula = new

System.Drawing.SolidBrush(System.Drawing.Color.Blue);

Pensula_pic = new System.Drawing.SolidBrush(this.BackColor);

Creion_blu = new System.Drawing.Pen(System.Drawing.Color.Blue);

Desen.Clear(this.BackColor);

w = this.Size.Width / 67;

h = this.Size.Height / 20;

a = 63;

b = 37;

v = 3;

}

private void timer1_Tick(object sender, EventArgs e)

{

Page 98: Laborator instructiuni repetitive - UMFST

a = a + 1;

if (a == 64)

{

Desen.Clear(this.BackColor);

Desen.FillRectangle(Pensula_pic, (a - 1) * w,

this.Size.Height / 3, w, h);

Desen.FillRectangle(Pensula_pic, (b - 1) * w,

(this.Size.Height / 3) + h + 2, w, h);

for (a = 0; a < 65; a++)

{

if (this.radioButton2.Checked){

Desen.DrawRectangle(Creion_blu, (a - 1) * w,

this.Size.Height / 3, w, h);

Desen.DrawRectangle(Creion_blu, (a - 1) * w,

(this.Size.Height / 3)+h+2, w, h);

}

}

a = 0;

b = 37;

}

Desen.FillRectangle(Pensula, a*w, this.Size.Height / 3, w, h);

Desen.FillRectangle(Pensula_pic, (a-1)*w, this.Size.Height / 3,

w, h);

if (this.radioButton2.Checked)

Desen.DrawRectangle(Creion_blu, (a - 1) * w,

this.Size.Height / 3, w, h);

if(v==3){

Desen.FillRectangle(Pensula, b*w, (this.Size.Height / 3) +

h+2, w, h);

Desen.FillRectangle(Pensula_pic, (b-1)*w, (this.Size.Height

/ 3) + h+2, w, h);

if (this.radioButton2.Checked)

Desen.DrawRectangle(Creion_blu, (b - 1) * w,

(this.Size.Height / 3) + h+2, w, h);

b=b+1;

v = 0;

}

v = v + 1;

}

private void trackBar1_Scroll(object sender, EventArgs e)

{

this.timer1.Interval = this.trackBar1.Value;

}

}

}

Realizati o aplicatie grafica in care veti desena n cercuri de forma de jos.

Page 99: Laborator instructiuni repetitive - UMFST

namespace Mot_cerc_v1

{

public partial class Form1 : Form

{

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

int x, y, x0, y0, d, xc, yc;

double alfa_g, alfa_r;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

x0 = this.Width / 4;

y0 = this.Height / 4;

d = this.Width / 3;

xc = x0 + d / 2;

Page 100: Laborator instructiuni repetitive - UMFST

yc = y0 + d / 2;

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

for (alfa_g = 0; alfa_g < 360; alfa_g += 45)

{

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen.DrawEllipse(Creion_rosu,x-d/2,y-d/2,d,d);

}

}

}

}

Versiunea 2

namespace Mot_cerc_v2

{

public partial class Form1 : Form

{

System.Drawing.Graphics Desen;

System.Drawing.Pen Creion_albastru;

System.Drawing.Pen Creion_rosu;

int x, y, x0, y0, d, xc, yc;

double alfa_g, alfa_r;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Desen = this.CreateGraphics();

Creion_albastru = new

System.Drawing.Pen(System.Drawing.Color.Blue);

Creion_rosu = new System.Drawing.Pen(System.Drawing.Color.Red);

}

private void trackBar1_Scroll(object sender, EventArgs e)

{

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

for (alfa_g = 0; alfa_g < 360; alfa_g += this.trackBar1.Value)

{

Page 101: Laborator instructiuni repetitive - UMFST

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen.DrawEllipse(Creion_rosu, x - d / 2, y - d / 2, d, d);

}

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

x0 = this.Width / 4 + 20;

y0 = this.Height / 4 + 30;

d = this.Width / 3;

xc = x0 + d / 2;

yc = y0 + d / 2;

Desen = this.CreateGraphics();

Desen.Clear(this.BackColor);

Desen.DrawEllipse(Creion_albastru, x0, y0, d, d);

for (alfa_g = 0; alfa_g < 360; alfa_g += this.trackBar1.Value)

{

alfa_r = 2 * System.Math.PI * alfa_g / 360;// unghiul in

radiani

x = System.Convert.ToInt16(xc + d / 2 *

System.Math.Cos(alfa_r));

y = System.Convert.ToInt16(yc - d / 2 *

System.Math.Sin(alfa_r));

Desen.DrawEllipse(Creion_rosu, x - d / 2, y - d / 2, d, d);

}

}

}

}