Tweet

Ana Sayfa   Forum   C , C++
Yeni Başlık Cevap Ekle
white_bullet Exception Handling Örneği (29/05/2010 05:15)
profil basak-bilgi
 offline OFFLINE
 New Coder

CODE:

// Author: BAŞAK BİLGİ
// Bowling Oyunu

#include <iostream>
#include "time.h"
#include "IllegalStrikeValueException.h"
#include "IllegalTotalValueException.h"
#include "IllegalStrikeBonusException.h"
#include "IllegalBonusException.h"

using namespace std;


int calculateScore(int scores [22]){

int totalScore=0;
int index;
int temp=0;

for (index=0;index<22;index++)

totalScore=totalScore+scores[index];

cout <<totalScore<<endl;


for (index=0;index<22;index=index+2){

if (scores[index]==10)

totalScore=totalScore+scores[index+2]+scores[index+3];


else if (((scores[index]+scores[index+1])==10)&&(scores[index]!=10))

totalScore=totalScore+scores[index+2];

}


return totalScore;


}

int main(){

int scores [22] = {0};
int index;
int frame=0;
int choice;
int totalPoint=0;

cout <<"Enter your choice (1/2)? :"<<endl;
cin >>choice;

for (index=0;index<22;index++)

scores[index] = rand() % 10+1 ;


if (choice==1){

for (frame=0;frame<11;frame++)

cout <<frame+1<<".Frame Scores:"<<scores[2*frame]<<" "<<scores[2*frame+1]<<endl;
}

for (frame=0;frame<11;frame++) {

try {

if ((scores[2*frame]==10) && (scores[2*frame+1]!=0))

throw IllegalStrikeValueException();


if ((scores[2*frame]!=10)&&(scores[2*frame]+scores[2*frame+1]> 10))

throw IllegalTotalValueException();

}

catch ( IllegalStrikeValueException &illegalStrikeValueException ){

if (choice==1)

cout << illegalStrikeValueException.what()<<" occurred at frame "<<frame+1<<"=>the value "<<scores[2*frame+1]<<" violates the rule"<< endl;

else

scores[2*frame+1]=0;


}

catch ( IllegalTotalValueException & illegalTotalValueException ){

if (choice==1)

cout << illegalTotalValueException.what()<<" occurred at frame "<<frame+1<<"=>the value "<<scores[2*frame+1]<<" violates the rule"<< endl;

else


scores[2*frame+1]= rand() % (10-scores[2*frame])+1 ;
}

}

try {

if ((scores[19]!=10) && (scores[21]!=0))

throw IllegalStrikeBonusException();
}

catch ( IllegalStrikeBonusException & illegalStrikeBonusException ){

if (choice==1)

cout << illegalStrikeBonusException.what()<<" occurred 11.frame's second value should be 0"<<endl;

else

scores[21]=0;


}

try {

if (((scores[18]+scores[19])!=10) && ((scores[20]!=0)|| (scores[21]!=0)))

throw IllegalBonusException();
}

catch ( IllegalBonusException & illegalBonusException ){

if(choice==1)

cout << illegalBonusException.what()<<" occurred 11.frame's values should be both 0"<<endl;

else {

scores[21]=0;
scores[20]=0;

}

}


if (choice==2){

for (frame=0;frame<11;frame++)

cout <<frame+1<<".Frame Scores:"<<scores[2*frame]<<" "<<scores[2*frame+1]<<endl;



cout <<"Congratulations you scored in "<< calculateScore(scores)<<" this stunning game !!!!" <<endl;

}

return 0;

}
------------
IllegalTotalValueException.h
------------
#include <stdexcept>
using std::runtime_error;


class IllegalTotalValueException : public runtime_error
{
public:

IllegalTotalValueException::IllegalTotalValueException() : runtime_error( "IllegalTotalValueException" ) {}
};
---------------
IllegalStrikeValueException.h
---------------
#include <stdexcept>
using std::runtime_error;


class IllegalStrikeValueException : public runtime_error
{
public:

IllegalStrikeValueException::IllegalStrikeValueException() : runtime_error( "IllegalStrikeValueException" ) {}
};

------------------
IllegalStrikeBonusException.h
------------------
#include <stdexcept>
using std::runtime_error;


class IllegalStrikeBonusException : public runtime_error
{
public:

IllegalStrikeBonusException::IllegalStrikeBonusException() : runtime_error( "IllegalStrikeBonusException" ) {}
};
--------------
IllegalBonusException.h
--------------
#include <stdexcept>
using std::runtime_error;


class IllegalBonusException : public runtime_error
{
public:

IllegalBonusException::IllegalBonusException() : runtime_error( "IllegalBonusException" ) {}
};

Üye Profili

white_bullet Re:Exception Handling Örneği (29/05/2010 09:43)
profil Oguzz
 offline OFFLINE
 Senior Coder

Örnek için teşekkürler.Fakat bu şekilde pek doyurucu olmamış.Keşke bölüm bölüm anlatsaydın.

just code it!

profil

white_bullet Re:Exception Handling Örneği (29/05/2010 09:53)
profil basak-bilgi
 offline OFFLINE
 New Coder

çok yoğunum sonra detaylıca açıklarım...

profil
 
1 /
 
Ana Sayfa   Forum   C , C++
Yeni Başlık Cevap Ekle
 

MENÜ » FORUM
Menü » Takip et
RSS Facebook Twitter Friendfeed
Sık Kullanılanlar Google Yahoo Live
Menü » Paylaş
E-Posta ile gönder Twitter Facebook Friendfeed
Buzz Stumbleupon Delicious Digg