Sam zobacz: (kompilator minGW)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <fstream>
#include <time.h>
#include <vector>
class Test
{
public:
int number_attempts;
int diff_level;
int high_low;
int good;
int liczba;
int answer;
int correct;
int points;
int which_attempt;
int Vec_size;
std::vector <std::string> Vector;
std::string name;
std::string data;
Test(int n_r=5, int d_l=0, int h_l=0, int l=0, int g=0, int a=0, int c=0, int p=0, int w_a=0, int V_s=0, std::string n="default", std::string d="00.00.0000")
{
number_attempts=n_r;
diff_level=d_l;
high_low=h_l;
good=g;
answer=a;
correct=c;
points=p;
which_attempt=w_a;
liczba=l;
Vec_size=V_s;
name=n;
data=d;
}
virtual ~Test() {};
virtual void genere_freq()=0;
virtual void genere_freq2()=0;
virtual void genere_base()=0;
virtual void select_octave()=0;
virtual void select_terce()=0;
virtual void select_freq2()=0;
virtual void higher_lower()=0;
void user_data()
{
std::cout<<"Podaj nazwe: ";
std::cin>>name;
time_t data;
time(&data);
ctime(&data);
}
void load_number_att()
{
std::cout<<"Podaj ilosc prob: ";
std::cin>>number_attempts;
}
void take_answer()
{
std::cout<<"Podaj odpowiedz: ";
std::cin>>answer;
}
void check()
{
if(answer==correct)
{
std::cout<<"Dobrze!"<<std::endl;
points++;
}
else if (answer != correct)
{
std::cout<<"Zle!"<<std::endl;
}
}
void show_points()
{
std::cout<<"Zdobyte punkty: "<<points<<std::endl;
}
void menu()
{
std::cout<<"1. Test z wybieraniem jednej czestotliwosci."<<std::endl;
std::cout<<"2. Test z porownywaniem dwoch czestotliwosci."<<std::endl;
std::cout<<"3. Historia."<<std::endl;
std::cout<<"4. Koniec."<<std::endl;
std::cout<<std::endl<<"Twoj wybor: ";
std::cin>>liczba;
}
void how_can_freq()
{
std::cout<<"Podaj poziom trudnosci (tercje = 1, oktawy = 0): ";
std::cin>>diff_level;
if(diff_level==0)
std::cout<<"Czestotliwosci do wyboru: 125, 250, 500, 1000, 2000, 4000, 8000, 16000";
else if(diff_level==1)
{
std::cout<<"Czestotliwosci do wyboru: 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000"<<std::endl;
std::cout<<"1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000"<<std::endl;
}
}
void create_file()
{
std::ofstream plik( "history.txt", std::ios::app);
}
void save_file()
{
std::fstream plik;
plik.open("history.txt", std::ios::out | std::ios::app);
if( plik.good() )
{
plik<<__DATE__<<" ";
plik<<liczba<<" ";
plik<<diff_level<<" ";
plik<<points<<" ";
plik<<name<<std::endl;
}
plik.close();
}
void load_file()
{
std::fstream plik;
plik.open("history.txt", std::ios::in);
if(plik.good()==false)
{
std::cout<<"Nie udalo sie otworzyc pliku!";
exit(0);
}
std::string linia;
while(getline(plik,linia))
{
Vector.push_back(linia);
}
plik.close();
}
void wyswietl()
{
std::cout<<" Data "<<" Numer Testu "<<" Poziom Trudnosci "<<" Punkty "<<" Nazwa "<<std::endl;
std::vector < std::string >::iterator it_Vec = Vector.begin();
for( ; it_Vec != Vector.end(); it_Vec++ )
std::cout<<* it_Vec<<std::endl;
}
};
class Freq :public Test
{
public:
int* liczba_octave;
int* liczba_terce;
int* liczba_another;
int record_number;
Freq(int r_n=0, int n_r=5, int d_l=0) :Test(n_r=5, d_l=0)
{
number_attempts=n_r;
diff_level=d_l;
liczba_octave = new int[n_r];
liczba_terce = new int[n_r];
liczba_another = new int[n_r];
record_number=r_n;
}
~Freq()
{
delete [] liczba_octave;
delete [] liczba_terce;
delete [] liczba_another;
}
virtual void select_octave()
{
int octave[8] = {125, 250, 500, 1000, 2000, 4000, 8000, 16000};
for(int i = 0 ; i < number_attempts; i++)
{
int rand_octave = ( rand () % 8);
for( int j = 0 ; j <= rand_octave ; j++)
{
if(j==rand_octave)
liczba_octave[i]=octave[j];
}
}
}
virtual void select_terce()
{
int terce[22] = {125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000};
for(int i = 0 ; i < number_attempts; i++)
{
int rand_terce = ( rand () % 22);
for( int j = 0 ; j <= rand_terce ; j++)
{
if(j==rand_terce)
liczba_terce[i]=terce[j];
}
}
}
virtual void select_freq2()
{
int another[4] = { 436, 438, 442, 444};
{
for(int i = 0 ; i < number_attempts; i++)
{
int rand_another = ( rand () % 4);
for ( int t = 0 ; t <= rand_another ; t++)
{
if(liczba_another[t]==another[rand_another])
do
{
rand_another = ( rand () % 4);
}
while(liczba_another[t] != another[rand_another]);
}
for( int j = 0 ; j <= rand_another ; j++)
{
if(j==rand_another)
liczba_another[i]=another[j];
}
}
}
}
virtual void genere_base()
{
Beep(440,3000);
}
virtual void genere_freq()
{
if(diff_level==0)
{
correct=liczba_octave[record_number];
Beep(liczba_octave[record_number],3000);
}
else if(diff_level==1)
{
correct=liczba_terce[record_number];
Beep(liczba_terce[record_number],3000);
}
}
virtual void genere_freq2()
{
correct=liczba_another[record_number];
Beep(liczba_another[record_number],3000);
}
virtual void higher_lower()
{
std::cout<<std::endl<<"Wygenerowana czestotliwosc byla wyzsza[1] czy nizsza[0]?: ";
std::cin>>high_low;
if(correct<=440)
{
good = 0;
}
else if(440<=correct)
{
good = 1;
}
if(high_low==good)
{
std::cout<<"Dobrze!"<<std::endl;
points++;
}
else if(high_low != good)
{
std::cout<<"Zle!"<<std::endl;
}
}
};
int main()
{
srand( time( NULL ) );
Freq f1;
int again;
f1.create_file();
f1.user_data();
system("cls");
poczatek:
f1.menu();
switch(f1.liczba)
{
case 1:
{
f1.load_number_att();
f1.how_can_freq();
if(f1.diff_level==0)
f1.select_octave();
else if(f1.diff_level==1)
f1.select_terce();
for(int i = 0 ; i < f1.number_attempts ; i++)
{
f1.record_number = i;
if(f1.diff_level==0)
{
std::cout<<std::endl<<"Czestotliwosc numer: "<<i+1<<std::endl;
f1.genere_freq();
f1.take_answer();
f1.check();
std::cout<<"Wygenerowana czestotliwosc to: ";
std::cout<<f1.liczba_octave[i]<<std::endl;
}
else if(f1.diff_level==1)
{
std::cout<<std::endl<<"Czestotliwosc numer: "<<i+1<<std::endl;
f1.genere_freq();
f1.take_answer();
f1.check();
std::cout<<"Wygenerowana czestotliwosc to: ";
std::cout<<f1.liczba_terce[i]<<std::endl;
}
Sleep(500);
}
f1.show_points();
f1.save_file();
f1.points=0;
break;
}
case 2:
{
f1.load_number_att();
f1.select_freq2();
for(int i = 0 ; i < f1.number_attempts ; i++)
{
f1.record_number = i;
std::cout<<std::endl<<"Zestaw czestotliwosci numer: "<<i+1<<std::endl;
Sleep(1500);
std::cout<<"Czestotliowsc podstawowa 440Hz";
for( int y = 0 ; y <= 3 ; y++)
{
std::cout<<".";
Sleep(500);
}
f1.genere_base();
std::cout<<std::endl<<"Czestotliowsc odstrojona";
for( int y = 0 ; y <= 3 ; y++)
{
std::cout<<".";
Sleep(500);
}
Sleep(1500);
f1.genere_freq2();
f1.higher_lower();
Sleep(1000);
}
f1.show_points();
f1.save_file();
f1.points=0;
break;
}
case 3:
{
f1.load_file();
f1.wyswietl();
f1.Vector.clear();
break;
}
case 4:
{
system("pause");
return 0;
}
}
std::cout<<std::endl<<"Wrocic do menu? (1.Tak 2.Nie): ";
std::cin>>again;
system("cls");
if(again==1)
goto poczatek;
else
system("pause");
return 0;
}