Ciao a tutti,
mi sono arenato in un groviglio logico e non riesco a trovare una soluzione
Come risultato devo avere un numero predeterminato di lettere che verranno estratte con rand dovranno uscire:
5 volte la A
4 volte la B
2 volte la C
E il programma funziona perfettamente. Nel caso commento la 2° scelta il programma si blocca perché?
Per quel che vedo io al massimo dovrebbe ignorare la lettera B
codice:
string WorkDays::createTurn() {
bool check = true;
string randomTurn;
do{
// 1° scelta
randomTurn = getRotationRound();
if ( myTurn.getMorning() != 0 and randomTurn == "A"){
myTurn.updateMorning();
check = false;
}
// QUI c'è l'errore
// 2° scelta
// if ( myTurn.getAfternoon() != 0 and randomTurn == "B"){
// myTurn.updateAfternoon();
// check = false;
// }
// 3° scelta
if ( myTurn.getRestDays() != 0 and randomTurn == "C"){
myTurn.updateRestDays();
check = false;
}
} while (check == true);
return randomTurn;
}
Grazie
Lele
Codice originale.
main
codice:
#include "workdays.h"
#include<iostream>
using std::cout;
int main(int argc, char *argv[])
{
WorkDays myWorkday;
for (int var = 0; var < 11; ++var) {
cout << myWorkday.createTurn();
}
}
userturn.h
codice:
#ifndef USERTURN_H
#define USERTURN_H
class userTurn
{
public:
userTurn();
// set value of the turn
void setMorning( int val = 5);
void setAfternoon( int val = 4);
void setRestDays( int val = 2);
// update value of the turn
void updateMorning();
void updateAfternoon();
void updateRestDays();
// get value of the turn
int getMorning();
int getAfternoon();
int getRestDays();
private:
int m_Morning;
int m_Afternoon;
int m_RestDays;
};
#endif // USERTURN_H
userturn.cpp
codice:
#include "userturn.h"
userTurn::userTurn()
{
setMorning();
setAfternoon();
setRestDays();
}
void userTurn::updateMorning(){
m_Morning--;
}
void userTurn::updateAfternoon(){
m_Afternoon--;
}
void userTurn::updateRestDays(){
m_RestDays--;
}
void userTurn::setMorning( int val ){
m_Morning = val;
}
void userTurn::setAfternoon( int val ){
m_Afternoon = val;
}
void userTurn::setRestDays( int val ){
m_RestDays = val;
}
int userTurn::getMorning(){
return m_Morning;
}
int userTurn::getAfternoon(){
return m_Afternoon;
}
int userTurn::getRestDays(){
return m_RestDays;
}
workdays.h
codice:
#ifndef WORKDAYS_H
#define WORKDAYS_H
#include "userturn.h"
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
class WorkDays
{
public:
userTurn myTurn;
WorkDays();
// assemble a turn of employ
string createTurn();
// create a turn casual for work fo moorning, afternoon and rest
string getRotationRound();
};
#endif // WORKDAYS_H
workdays.cpp
codice:
#include "workdays.h"
WorkDays::WorkDays()
{
//srand(time(0));
}
string WorkDays::createTurn() {
bool check = true;
string randomTurn;
do{
randomTurn = getRotationRound();
if ( myTurn.getMorning() != 0 and randomTurn == "A"){
myTurn.updateMorning();
check = false;
}
if ( myTurn.getAfternoon() != 0 and randomTurn == "B"){
myTurn.updateAfternoon();
check = false;
}
if ( myTurn.getRestDays() != 0 and randomTurn == "C"){
myTurn.updateRestDays();
check = false;
}
} while (check == true);
return randomTurn;
}
// choose a turn casual for work of moorning, afternoon or restDays
string WorkDays::getRotationRound(){
// int morning, int afternoon, int restDay
const int value = 3;
string randomTurn[value] = {"A", "B", "C"};
return randomTurn[0 + rand() % value];
}