7.3.. Napisati program za ispis uspjeha, prema slijedeæoj tabeli:

Opis programa: Da bi se mogla koristi naredba CASE vrijednosti moraju biti cjelobrojne.
ibb = (int) (bb + 0.5);
Zato se prosjeku dodaje 0.5 i zatim odbacuju decimalna mjesta.

Opseg Ispis Dodano 0.5 (bb + 0.5) Cio broj sa 0.5 (int) (bb + 0.5);
0 - 1.4 nedovoljan 0.5 - 1.9 0 -1
1.5 - 2.4 dovoljan 2.0 - 2.9 2 - 2
2.5 - 3.4 dobar 3.0 - 3.9 3 - 3
3.5 - 4.4 vrlo dobar 4.0 - 4.9 4 - 4
4.5 - 5.0 odlican 5.0 - 5.0 5 - 5

Listing programa:

//07311007
#include <iostream>
using namespace std;

int main()
{
    cout << "Za ucitani broj bodova ispisati uspjeh" << endl;
    float bb;
    int ibb;
    cout << "Osvojeno bodova (0 - 5.0): ";
    cin >> bb;                              // broj bodova

    ibb = (int) (bb + 0.5);  // dodaj 0.5 i odbaci decimalna mjesta

    switch(ibb) {
    case 0 ... 1:
        cout << "Nedovoljan" << endl;
        break;
    case 2:
        cout << "Dovoljan" << endl;
        break;
    case 3:
        cout << "Dobar" << endl;
        break;
    case 4:
        cout << "Vrlodobar" << endl;
        break;
    case 5:
        cout << "Odlican" << endl;
        break;
    default:
        cout << "Nevalidan unos" << endl;
    }
    return 0;
}

II Varijanta
//07311007
#include <iostream>
using namespace std;

int main()
{
	double a;
	bool bPRvi, bDrugi, bTreci;
	cout<<"Ocjena - Prosjek"<<endl;
	cout<<"Prosjek"<<endl;
	cin>>a;

	if((a>=0)&&(a<=1.4)){bPRvi=true;}
	else if((a>=1.5)&&(a<=2.4)){bPRvi=false;}
	else goto DRUGI_DIO;
	switch(bPRvi)
	{
		case true: cout<<"nedovoljan"<<endl; cout<<endl; break;
		case false: cout<<"dovoljan"<<endl; cout<<endl; break;

	}

	DRUGI_DIO:
	if((a>=2.5)&&(a<=3.4)){bDrugi=true;}
	else if((a>=3.5)&&(a<=4.4)){bDrugi=false;}
	else goto TRECI_DIO;
	switch(bDrugi)
	{
		case true: cout<<"dobar"<<endl; cout<<endl; break;
		case false: cout<<"vrlodobar"<<endl; cout<<endl; break;
	}

	TRECI_DIO:
	if((a>=4.5)&&(a<=5)){bTreci=true;}
	switch(bTreci)
	{
		case true: cout<<"odlican"<<endl; cout<<endl; break;
	}

    return 0;
}

III Varijanta
//07311007
#include <iostream>
using namespace std;

int main()
{
	double a;
	cout<<"Ocjena - Prosjek"<<endl;
	cout<<"Prosjek"<<endl;
	cin>>a;
	
	if ((a>=0)&&(a<=1.4))
	{cout<<"nedovoljan"<<endl; cout<<endl;}

	else if ((a>=1.5)&&(a<=2.4))
	{cout<<"dovoljan"<<endl; cout<<endl;}

	else if ((a>=2.5)&&(a<=3.4))
	{cout<<"dobar"<<endl; cout<<endl;}

	else if((a>=3.5)&&(a<=4.4))
	{cout<<"vrlo dobar"<<endl; cout<<endl;}
	
	else if((a>=4.5)&&(a<=5))
	{cout<<"odlican"<<endl; cout<<endl;}


    return 0;
}

Ispis na ekranu:

Index