Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <map>
- #include <fstream>
- #include <windows.h>
- #include <time.h>
- using namespace std;
- map<int, string> Data;
- int ValueBigger(string t1, string t2){
- if(t1.length() < t2.length())
- return -1;
- if(t1.length() > t2.length())
- return 1;
- return strcmp(t1.c_str(), t2.c_str());
- }
- bool LoadFile(const char sciezka[], bool czysc){
- fstream Plik;
- Plik.open(sciezka, ios::in);
- if(!Plik.good())
- return false;
- if(czysc)
- Data.clear();
- int n=Data.size();
- while(!Plik.eof())
- {
- string element;
- Plik >> element;
- Data[n] = element;
- n++;
- }
- Plik.close();
- return true;
- }
- bool SaveFile(const char sciezka[]){
- fstream Plik;
- Plik.open(sciezka, ios::out);
- if(!Plik.good())
- return false;
- for(int n=0; n<Data.size(); n++)
- Plik << Data[n] << " ";
- Plik.close();
- return true;
- }
- int FindText(string fraza, bool przebieg){
- map<int, string>::iterator it = Data.begin();
- while(true)
- {
- if(it == Data.end())
- return -1;
- if(przebieg)
- cout<<" > Sprawdzam "<<fraza<<" z "<<it->second<<endl;
- if(it->second == fraza)
- return it->first;
- it++;
- }
- return -1;
- }
- void BubbleSortText(){
- if(Data.size() < 2)
- return;
- int pozostalo = Data.size()-1;
- while(pozostalo != 0)
- {
- map<int, string>::iterator it = Data.begin();
- map<int, string>::iterator next = ++Data.begin();
- for(int n=0; n<pozostalo; ++n)
- {
- if(strcmp(it->second.c_str(), next->second.c_str()) > 0)
- swap(it->second, next->second);
- ++it;
- ++next;
- }
- --pozostalo;
- }
- }
- void BubbleSortValue(){
- if(Data.size() < 2)
- return;
- int pozostalo = Data.size()-1;
- while(pozostalo != 0)
- {
- map<int, string>::iterator it = Data.begin();
- map<int, string>::iterator next = ++Data.begin();
- for(int n=0; n<pozostalo; ++n)
- {
- if(ValueBigger(it->second, next->second) > 0)
- swap(it->second, next->second);
- ++it;
- ++next;
- }
- --pozostalo;
- }
- }
- void QuickSortText(int left, int right){
- if(Data.size() < 2)
- return;
- int i = left;
- int j = right;
- string x = Data[( left + right ) / 2 ];
- do
- {
- while( strcmp(Data[ i ].c_str(), x.c_str()) < 0)
- i++;
- while( strcmp(Data[ j ].c_str(), x.c_str()) > 0 )
- j--;
- if( i <= j )
- {
- swap( Data[ i ], Data[ j ] );
- i++;
- j--;
- }
- } while( i <= j );
- if( left < j ) QuickSortText( left, j );
- if( right > i ) QuickSortText( i, right );
- }
- void QuickSortValue(int left, int right){
- if(Data.size() < 2)
- return;
- int i = left;
- int j = right;
- string x = Data[( left + right ) / 2 ];
- do
- {
- while( ValueBigger(Data[ i ], x) < 0 )
- i++;
- while( ValueBigger(Data[ j ], x) > 0 )
- j--;
- if( i <= j )
- {
- swap( Data[ i ], Data[ j ] );
- i++;
- j--;
- }
- } while( i <= j );
- if( left < j ) QuickSortValue( left, j );
- if( right > i ) QuickSortValue( i, right );
- }
- void EditIndexData(){
- string command;
- if(Data.size() < 1)
- {
- cout <<" > Lista jest pusta !"<<endl;
- return;
- }
- system("cls");
- cout<<"@LISTA: --------------"<<endl;
- for(int i=0; i<Data.size(); ++i)
- cout<<"@ "<<i<<". "<<Data[i]<<endl;
- int nr;
- do
- {
- cout<<endl<<" > Ktory element edytowac? :";
- cin >> nr;
- }while(nr < 0 || nr >= Data.size());
- cout<<" > Podaj nowy wyraz: ";
- cin >> command;
- cout <<" > Wyraz "<<Data[nr]<<" zamieniony na "<<command<<endl;
- Data[nr] = command;
- return;
- }
- void EditData(string data){
- string command;
- if(Data.size() < 1)
- {
- cout <<" > Lista jest pusta !"<<endl;
- return;
- }
- int index = FindText(data, false);
- if(index == -1)
- {
- cout <<" > Nie znaleziono wyrazu!"<<endl;
- return;
- }
- cout <<" > Zamiana wyrazu "<<data<<"(index: "<<index<<") na: ";
- cin >> command;
- Data[index] = command;
- cout <<" > Wyraz "<<data<<"(index: "<<index<<") zamieniony na "<<command<<endl;
- return;
- }
- void EditDataToData(string data1, string data2){
- if(Data.size() < 1)
- {
- cout <<" > Lista jest pusta !"<<endl;
- return;
- }
- int index = FindText(data1, false);
- if(index == -1)
- {
- cout <<" > Nie znaleziono wyrazu!"<<endl;
- return;
- }
- Data[index] = data2;
- cout <<" > Wyraz "<<data1<<" zamieniony na "<<data2<<endl;
- return;
- }
- void AddData(int ilosc){
- int rozmiar = Data.size();
- srand(time(NULL));
- for(int n=0; n<ilosc; ++n)
- {
- char value[5];
- itoa(rand()%1000, value, 10);
- Data[rozmiar+n] = value;
- cout<<"Dodano index: "<<rozmiar+n<<" = "<<Data[rozmiar+n]<<endl;
- }
- }
- void DisplayStart(){
- cout<<"> Dostepne komendy:"<<endl;
- cout<<" > load <nazwapliku.txt>"<<endl;
- cout<<" > save <nazwapliku.txt>"<<endl;
- cout<<" > find <kryterium> <wyraz>"<<endl;
- cout<<" > add <ilosc> (argumenty opcjonalne)"<<endl;
- cout<<" > sort <text/value> <bubble/quick>"<<endl;
- cout<<" > edit <podmieniany> <podmieniony> (argumenty opcjonalne)"<<endl;
- cout<<" > rand"<<endl;
- cout<<" > display"<<endl;
- cout<<" > exit"<<endl;
- cout<<endl;
- cout<<"# ";
- }
- string ReadNextCommand(string & in){
- int pointer = 0;
- pointer = in.find(' ');
- string out;
- if(pointer != string::npos)
- out = in.substr(0, pointer);
- else
- out = in;
- if(pointer == string::npos)
- in.clear();
- else
- in.erase(0, pointer+1);
- return out;
- }
- int Dispatch(string in){
- string command = ReadNextCommand(in);
- if(command == "load")
- {
- if(!LoadFile(ReadNextCommand(in).c_str(), true))
- cout<<"> Wystapil blad otwarcia pliku."<<endl;
- else
- cout<<"> Dane z pliku wczytane poprawnie."<<endl;
- getch();
- return 0;
- }
- if(command == "save")
- {
- if(!SaveFile(ReadNextCommand(in).c_str()))
- cout<<"> Wystapil blad otwarcia pliku."<<endl;
- else
- cout<<"> Pliki zostaly poprawnie zapisane."<<endl;
- getch();
- return 0;
- }
- if(command == "find")
- {
- int result = FindText(ReadNextCommand(in).c_str(), true);
- if(result != -1)
- cout <<" > Wyraz posiada index: "<<result<<" (0 - "<< Data.size()-1 <<")"<<endl;
- else
- cout <<" > Podany wyraz nie istnieje !"<<endl;
- getch();
- return 0;
- }
- if(command == "add")
- {
- string data1 = ReadNextCommand(in);
- if(!data1.length())
- AddData(1);
- else
- {
- if(atoi(data1.c_str()) < 1)
- AddData(1);
- else
- AddData(atoi(data1.c_str()));
- }
- getch();
- return 0;
- }
- if(command == "sort")
- {
- command = ReadNextCommand(in);
- if(command == "text")
- {
- command = ReadNextCommand(in);
- if(command == "bubble"){
- BubbleSortText();
- cout<<" > tabela posortowana."<<endl;
- }
- else
- if(command == "quick"){
- QuickSortText(0, Data.size()-1);
- cout<<" > tabela posortowana."<<endl;
- }
- else
- cout<<"Niepoprawny rodzaj sortowania!"<<endl;
- }
- else
- if(command == "value")
- {
- command = ReadNextCommand(in);
- if(command == "bubble"){
- BubbleSortValue();
- cout<<" > tabela posortowana."<<endl;
- }
- else
- if(command == "quick"){
- QuickSortValue(0, Data.size()-1);
- cout<<" > tabela posortowana."<<endl;
- }
- else
- cout<<"Niepoprawny rodzaj sortowania!"<<endl;
- }
- else
- cout<<"Niepoprawny typ sortowania!"<<endl;
- getch();
- return 0;
- }
- if(command == "edit")
- {
- string data1 = ReadNextCommand(in);
- if(!data1.length())
- EditIndexData();
- else
- {
- string data2 = ReadNextCommand(in);
- if(!data2.length())
- EditData(data1);
- else
- EditDataToData(data1, data2);
- }
- getch();
- return 0;
- }
- if(command == "rand")
- {
- srand(time(NULL));
- for(int n=0; n<Data.size(); ++n)
- swap(Data[n], Data[rand()%Data.size()]);
- cout<<"Elementy zostaly ulozone losowo."<<endl;
- getch();
- return 0;
- }
- if(command == "display")
- {
- system("cls");
- cout<<"@ LISTA: --------------"<<endl;
- for(int i=0; i<Data.size(); ++i)
- cout<<" "<<i<<". "<<Data[i]<<endl;
- getch();
- return 0;
- }
- if(command == "exit")
- {
- cout<<" > wyjscie"<<endl;
- return 1;
- }
- if(command != "")
- cout<<" > Nieprawidlowa komenda!"<<endl;
- getch();
- return 0;
- }
- int main()
- {
- while(true)
- {
- char command[100];
- system("cls");
- DisplayStart();
- cin.clear();
- cin.getline(command, 100);
- if(Dispatch(command))
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement