Hej śpicie ?? :)
Mam pytanie do was odnośnie mojego kodu pisanego w C++:)
Robię projekt na temat wieży Hanoi dla n krążków i k słupków (mniejsza o szczegóły) :)
Pytanie to czemu w funkcji fstream nie zapisują się wszystkie moje dane.
Mam nadzieje że ktoś jeszcze nie śpi (albo już nie śpi) i pomoże mi z tym problemem.
Wrzucam kod programu :)
Z góry dziękuje pięknie :)
#include <iostream>
#include <math.h>
#include <conio.h>
#include <fstream>
using namespace std;
fstream z3("raport3.txt", ios::out);
fstream z4("raport4.txt", ios::out);
fstream z5("raport5.txt", ios::out);
fstream z6("raport6.txt", ios::out);
fstream z7("raport7.txt", ios::out);
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,c1,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,c1,t,ile5);
Hanoi_tower_7(k,c1,c2,c3,c4,f,c5,t,ile5);
}
}
// program g³ówny
int main()
{
int number=0;
int ile = 0;
int ile2 = 0;
int ile3 = 0;
int ile4 = 0;
int ile5 = 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;```cpp
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;
z7 << "Dla 7 pali" << endl;
z7 <<"ile krazkow :"<<number<<endl;
z7 <<"ile: "<<ile5<<endl;
z7 << endl;
z7.close();
getch();
return 0;}