Witam :)
Proszę was o małą pomoc, napisałem program na projekt na studia (o wieży Hanoi dla n słupków oraz k krążków). Dodałem bibliotekę fstream aby zapisać dane do pliku i w tym właśnie tkwi problem mianowicie. Dla 3 słupków program wszystko zapisuje to co chcę, problem pojawia się w następnych funkcjach gdzie zapisuje tylko po 3-4 kroki.
Czy jest ktoś w stanie mi pomóc, z góry dziękuje za pomoc :)
A i PS program jest na razie "goły i ma tylko działać" więc później będę to poprawiał jak poprawne ponazywane zminne czy odpowiednie komentarze. Pisze to bo na forum 4progremmers.net zamiast mi pomóc to mi wypisywali po 15 razy to samo.
Wrzucam kod programu :)
#include <iostream>
#include <math.h>
#include <conio.h>
#include <fstream>
using namespace std;
fstream z3("raport3.txt");
fstream z4("raport4.txt");
fstream z5("raport5.txt");
fstream z6("raport6.txt");
fstream z7("raport7.txt");
fstream z8("raport8.txt");
void Hanoi_tower_3(int n, int f, int t, int b, int &ile)
{
if(n != 0)
{
Hanoi_tower_3(n-1, f, b, t,ile);
cout << f << " -> " << t << endl;
z3 << f << " -> " << t << endl;
Hanoi_tower_3(n-1, b, t, f,ile);
ile++;
}
}
void Hanoi_tower_4(int n, int f, int b1,int b2, int t,int &ile2)
{
int r=0;
if (n == 1)
{
cout << f << " -> " << t <<endl;
z4 << f << " -> " << t << endl;
ile2++;
}
else
{
r=(int)floor((sqrt((double) 8*n+1)-1)/2);
Hanoi_tower_4(n-r,f,b2,t,b1,ile2);
Hanoi_tower_3(r,f,t,b2,ile2);
Hanoi_tower_4(n-r,b1,f,b2,t,ile2);
}
}
// Funkcja dla 5 wie¿
void Hanoi_tower_5(int n, int f, int c1,int c2, int c3, int t,int &ile3)
{
int p=0;
if (n == 1)
{
cout << f << " -> " << t << endl;
z5 << f << " -> " << t << endl;
ile3+=1;
}
else
{
p=(int)floor((sqrt((double) 10*n+1)-1)/2);
int k=n-p;
if (k<=0) k=n-p+1;
Hanoi_tower_5(k,f,c2,c3,t,c1,ile3);
Hanoi_tower_4(p,f,c2,c3,t,ile3);
Hanoi_tower_5(k,c1,c2,f,c3,t,ile3);
}
}
void Hanoi_tower_6(int n, int f, int c1,int c2, int c3,int c4, int t,int &ile4)
{
int p=0;
if (n == 1)
{
cout << f << " -> " << t << endl;
z6 << f << " -> " << t << endl;
ile4+=1;
}
else
{
p=(int)floor((sqrt((double) 12*n+1)-1)/2);
int k=n-p;
if (k<=0) {k=1;p=1;}
Hanoi_tower_6(k,f,c2,c3,c4,t,c1,ile4);
Hanoi_tower_5(p,f,c2,c3,c4,t,ile4);
Hanoi_tower_6(k,c1,c2,c3,f,c4,t,ile4);
}
}
void Hanoi_tower_7(int n, int f, int c1,int c2, int c3,int c4,int c5, int t,int &ile5)
{
int p=0;
if (n == 1)
{
cout << f << " -> " << t << endl;
z7 << f << " -> " << t << endl;
ile5+=1;
}
else
{
p=(int)floor((sqrt((double) 14*n+1)-1)/2);
int k=n-p;
if (k<=0) {k=1;p=1;}
Hanoi_tower_7(k,f,c2,c3,c4,c5,t,c1,ile5);
Hanoi_tower_6(p,f,c2,c3,c4,c5,t,ile5);
Hanoi_tower_7(k,c1,c2,c3,c4,f,c5,t,ile5);
}
}
void Hanoi_tower_8(int n, int f, int c1,int c2, int c3,int c4,int c5,int c6, int t,int &ile6)
{
int p=0;
if (n == 1)
{
cout << f << " -> " << t << endl;
z8 << f << " -> " << t << endl;
ile6+=1;
}
else
{
p=(int)floor((sqrt((double) 16*n+1)-1)/2);
int k=n-p;
if (k<=0) {k=1;p=1;}
Hanoi_tower_8(k,f,c2,c3,c4,c5,c6,t,c1,ile6);
Hanoi_tower_7(p,f,c2,c3,c4,c5,c6,t,ile6);
Hanoi_tower_8(k,c1,c2,c3,c4,c5,f,c6,t,ile6);
}
}
// program g³ówny
int main()
{
int number=0;
int ile = 0;
int ile2 = 0;
int ile3 = 0;
int ile4 = 0;
int ile5 = 0;
int ile6 = 0;
int ile7 = 0;
int ile8 = 0;
cout << "Podaj ilosc krazkow: ";
cin >> number;
cout << endl;
cout << "Dla 3 pali" << endl;
Hanoi_tower_3(number,1,2,3,ile);
cout << ile;
cout << endl;
z3 << "Dla 3 pali" << endl;
z3 <<"ile krazkow :"<<number<<endl;
z3 <<"ile ruchow :"<<ile<<endl;
z3 << endl;
z3.close();
cout << "Dla 4 pali" << endl;
Hanoi_tower_4(number,1,2,3,4,ile2);
cout << ile2;
cout << endl;
z4 << "Dla 4 pali" << endl;
z4 <<"ile krazkow :"<<number<<endl;
z4 <<"ile: "<<ile2<<endl;
z4 << endl;
z4.close();
cout << "Dla 5 pali" << endl;
Hanoi_tower_5(number,1,2,3,4,5,ile3);
cout << ile3;
cout << endl;
z5 << "Dla 5 pali" << endl;
z5 <<"ile krazkow :"<<number<<endl;
z5 <<"ile: "<<ile3<<endl;
z5 << endl;
z5.close();
cout << "Dla 6 pali" << endl;
Hanoi_tower_6(number,1,2,3,4,5,6,ile4);
cout << ile4<<endl;
z6 << "Dla 6 pali" << endl;
z6 <<"ile krazkow :"<<number<<endl;
z6 <<"ile: "<<ile4<<endl;
z6 << endl;
z6.close();
cout << "Dla 7 pali" << endl;
Hanoi_tower_7(number,1,2,3,4,5,6,7,ile5);
cout << ile5<<endl;
z7 << "Dla 7 pali" << endl;
z7 <<"ile krazkow :"<<number<<endl;
z7 <<"ile: "<<ile5<<endl;
z7 << endl;
z7.close();
cout << "Dla 8 pali" << endl;
Hanoi_tower_8(number,1,2,3,4,5,6,7,8,ile6);
cout << ile6<<endl;
z8 << "Dla 8 pali" << endl;
z8 <<"ile krazkow :"<<number<<endl;
z8 <<"ile: "<<ile6<<endl;
z8 << endl;
z8.close();
getch();
return 0;
}