Jump to content

Redox's C++ Tutorial : Tipul struct + functia switch.Program complex.


Recommended Posts

Posted

OWWeiiZ.png

 

Rulat Codeblocks.

 

Pe scurt programul va pune sa introduceti un numar de masini si specificatiile acestora.

Va apare pe ecran un meniu si selectati optiunea dorita.

 

#include <iostream>
#include <string.h>
using namespace std;
typedef struct{
char marca[50],carburant[50],culoare[50];
int an_fabricatie,pret;
}vehicul;
int main()
{
    int n,i,j,optiune;
    vehicul a[100],aux;
    cout<<"Cate masini introduceti: ";
    cin>>n;
    cout<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<"Marca: ";cin>>a.marca;
        cout<<"Carburant: ";cin>>a.carburant;
        cout<<"Culoare: ";cin>>a.culoare;
        cout<<"Anul fabricatiei: ";cin>>a.an_fabricatie;
        cout<<"Pretul: ";cin>>a.pret;
        cout<<endl;
    }
    int ok=1;
    cout<<"----MENIU----"<<endl;
    cout<<"1.Afisati masinile citite insotite de specificatii."<<endl;
    cout<<"2.Afisati masinile cu carburant benzina."<<endl;
    cout<<"3.Afisati masinile in ordinea descrescatoare a preturilor."<<endl;
    cout<<"4.Afisati masinile de culoare neagra."<<endl;
    cout<<"5.Iesire."<<endl<<endl;
 
    while(ok){
    cout<<"Introduceti optiunea dvs. ";cin>>optiune;
    cout<<endl;
        switch(optiune){
        case 1: for(i=1;i<=n;i++)
                 cout<<a.marca<<" "<<a.culoare<<" "<<a.an_fabricatie<<" "<<a.carburant<<" "<<a.pret<<endl;
                 cout<<endl;
                 break;
 
        case 2:{ for(i=1;i<=n;i++)
                 if(strcmp(a.carburant,"benzina")==0)
                    cout<<a.marca<<" ";
                cout<<endl;}
                 break;
 
        case 3:{for(i=1;i<n;i++)
                for(j=i+1;j<=n;j++)
                    if(a.pret<a[j].pret)
                {
                    aux=a;
                    a=a[j];
                    a[j]=aux;
                }
                for(i=1;i<=n;i++)
                    cout<<a.marca<<" "<<a.culoare<<" "<<a.an_fabricatie<<" "<<a.carburant<<" "<<a.pret<<endl;}
                cout<<endl;
                break;
 
      case 4:{ for(i=1;i<=n;i++)
               if(strcmp(a.culoare,"negru")==0)
                cout<<a.marca<<" ";
            cout<<endl;}
               break;
 
      case 5: ok=0;
      break;
}
    }
}
 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. 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.

moduri samp