1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <ctype.h>
using namespace std;
//----- Global variables -----//
map<string*, int> elements; // conteneur pour les éléments avec leur nom (key) et leur valeur (value)
unsigned int elementsAlreadyAtZero = 0;
//----- Prototypes -----//
void askUserForValue(void);
void displayResults(void);
void computeWeirdStuff(void);
//---------------------------------------------------------------------- main
int main()
{
askUserForValue();
while(elementsAlreadyAtZero != elements.size())
{
computeWeirdStuff();
displayResults();
}
return 0;
}
//---------------------------------------------------------------------- askUserForValue
void askUserForValue(void)
{
unsigned int i = 0; // compteur
char answer = 'y'; // store the user's answer
while(answer == 'y')
{
int value;
ostringstream oss;
cout << "Please enter value for p" << i << " : "; // ask value
cin >> value; // store value
oss << "p(" << i << ")"; // Build the string to store as a key
if(value <= 0)
{
value = 0;
oss << " dismissed";
elementsAlreadyAtZero++;
}
elements[new string(oss.str())] = value; // insert new element in map
cout << "Do you want to continue ? (Y/N) : "; // continue or not ?
cin >> answer; // store answer
answer = tolower(answer);
i++;
}
}
//---------------------------------------------------------------------- displayResults
void displayResults(void)
{
map<string*, int>::iterator it;
cout << endl;
for(it = elements.begin() ; it != elements.end(); it++ )
{
string* key = (*it).first;
cout << key->c_str() << " => " << (*it).second << endl;
}
}
//---------------------------------------------------------------------- computeWeirdStuff
void computeWeirdStuff(void)
{
map<string*, int>::iterator it;
int value;
for(it = elements.begin() ; it != elements.end(); it++ )
{
value = (*it).second - 2;
if(value == -2)
{
// element already processed
continue;
}
if(value <= 0)
{
value = 0;
elementsAlreadyAtZero++;
string* key = (*it).first;
key->append(" dismissed"); // kludge : should not modify the string just like that
}
(*it).second = value; // set the value to 0
}
} |