Jump to content

[C++] Functia switch (meniu)


iAlexandru93
 Share

Recommended Posts

De functia switch va puteti folosi in multe cazuri, in aces topic va voi arata cum sa formati un meniu cu ajutorul ei.

 

int choice; // declaram choice de tipul int
bool gameOn = true; // declaram gameOn de tipul bool si il initializam cu true
while (gameOn != false) // cat timp este true meniu va aparea
{
cout<<"Apasa 1 pentru ...... .\n"; // textul meniului, \n este o alta varianta pentru endl
cout<<"Apasa 2 pentru ........ .\n";
cout<<"Apasa 3 pentru iesire.\n";
cin>>choice; // retin ce valoarea a ales
switch (choice) //
{
case 1:
cout<<"Ai apasat butonul 1"; // in cazul in care se apasa butonul 1 se va afisa textul Ai apasat butonul 1
break;
case 2:
cout<<"Ai apasat butonul 2"; // in cazul in care se apasa butonul 2 se va afisa textul Ai apasat butonul 2
break;
case 3:
cout<<"Iesire pogram";
gameOn = false; // in cazul in care se apasa butonul 3 parasim meniul, mai exact initializam gameOn cu false deoarece in while am specificat ca va merge cat timp este true
break;
}
}

 

 

Exemplu de program in care am folosit functia switch:

 

 

 

 

#include <iostream>

#include <conio.h>

using namespace std;

int bubble(int n,int v[20])

{

int k=0,aux,i;

while(k==0)

{

k=1;

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

if(v>v[i+1]) { aux=v;

v=v[i+1];

v[i+1]=aux;

k=0;}

}

return 0;

}

int selection(int n,int v[20])

{

int i,p,min,aux,k;

p=1;

while(p!=n)

{

min=v[p];

k=p;

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

if(min>v) { min=v;

k=i;}

if(p!=k) {aux=v[p];

v[p]=v[k];

v[k]=aux;}

p++;

}

return 0;

}

int insertion(int n,int v[20])

{

int i,aux,j;

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

{

aux=v[j];

i=j-1;

while(v>aux && i>=0)

{

v[i+1]=v;

i--;

}

v[i+1]=aux;

}

return 0;

}

int secventiala(int n, int v[20],int k)

{

int i;

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

if(v==k) return 1;

return 0;

}

int binara(int v[20],int p,int u,int k)

{

int m;

if(p>u) return 0;

else

{

m=(p+u)/2;

if (k<v[m]) return binara(v,p,m-1,k);

else if (k>v[m]) return binara(v,m+1,u,k);

else return 1;

}

 

return 0;

}

int afisare(int n,int v[20])

{

int i;

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

cout<<v<<" ";

return 0;

}

int main()

{

int n,i,v[20],choice,k;

cout<<"Introduceti numarul de elemente: ";

cin>>n;

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

{

cout<<"Introduceti numarul "<<i<<" = ";

cin>>v;

}

cout<<endl;

bool gameOn = true; // meniu

while (gameOn != false)

{

cout<<endl;

cout << "*******************************\n";

cout << " 1 - Bubble sort.\n";

cout << " 2 - Selection sort.\n";

cout << " 3 - Insertion sort.\n";

cout << " 4 - Cautare secventiala.\n";

cout << " 5 - Cautare binara.\n";

cout << " 6 - Afisare vector.\n";

cout << " 7 - Exit.\n";

cout << " Introduceti numarul si apasati ENTER: ";

cin >> choice;

cout<<endl;

switch (choice)

{

case 1:

cout<<"1. Bubble Sort"<<endl;

cout<<"Numerele introduse sunt: ";

afisare(n,v);

cout<<endl;

bubble(n,v);

cout<<"Numerele sortate: ";

afisare(n,v);

break;

case 2:

cout<<"2. Selection sort"<<endl;

cout<<"Numerele introduse sunt: ";

afisare(n,v);

cout<<endl;

selection(n,v);

cout<<"Numerele sortate: ";

afisare(n,v);

break;

case 3:

cout<<"3. Insertion sort"<<endl;

cout<<"Numerele introduse sunt: ";

afisare(n,v);

cout<<endl;

insertion(n,v);

cout<<"Numerele sortate: ";

afisare(n,v);

break;

case 4:

cout<<"Introduceti valoarea pe care doriti sa o cautati: ";

cin>>k;

if(secventiala(n,v,k)) cout<<"Numarul "<<k<<" apartine sirului!";

else cout<<"Numarul "<<k<<" nu apartine sirului!";

break;

case 5:

int p,u;

cout<<"Introduceti valoarea pe care doriti sa o cautati: ";

cin>>k;

bubble(n,v);

p=0;

u=n;

if(binara(v,p,u,k)) cout<<"Numarul "<<k<<" apartine sirului!";

else cout<<"Numarul "<<k<<" nu apartine sirului!";

break;

case 6:

cout<<"Numerele introduse sunt: ";

afisare(n,v);

break;

case 7:

cout<<"Parasire program";

gameOn = false;

break;

}

}

return 0;

}

 

 

 

Tot in acest progam puteti vedea cum am inserat bubble sort, insertion sort, selection sort, cautare binara si secventiala.

Edited by iAlexandru93
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.