void A()
{
B();
C();
B();
}
inizia A
inizia B
esegue contenuto di B
finisce B e ritorna ad A
inizia C
esegue contenuto di C
finisce C e ritorna ad A
inizia D
esegue contenuto di D
finisce D e ritorna ad A
finisce A e ritorna a chi l'ha chiamata
#include <iostream>
#include <string>
using namespace std;
void hanoi(int n, char from, char to, char other, const string& spaces)
{
cout << spaces << "Inizia hanoi(n=" << n << ", to=" << to << ", from=" << from << ")" << endl;
string next_spaces = spaces + " ";
if (n == 1)
{
cout << next_spaces << "Stampa AZIONE hanoi(n=1, to=" << to << ", from=" << from << ")" << endl;
}
else
{
hanoi(n - 1, from, other, to, next_spaces);
cout << "hanoi(n=" << n << ", to=" << to << ", from=" << from << ")" << endl; // per il ritorna a
cout << next_spaces << "Stampa AZIONE hanoi(n=" << n << ", to=" << to << ", from=" << from << ")" << endl;
hanoi(n - 1, other, to, from, next_spaces);
cout << "hanoi(n=" << n << ", to=" << to << ", from=" << from << ")" << endl; // per il ritorna a
}
cout << spaces << "Finisce hanoi(n=" << n << ", to=" << to << ", from=" << from << ") e ritorna a ";
}
int main(void)
{
string s = "";
hanoi(3, 'a', 'c', 'b', s);
cout << "main" << endl;
}
Visitano il forum: Nessuno e 1 ospite