Advertisement
Guest User

Untitled

a guest
Jan 30th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <map>
  4. #include <fstream>
  5. #include <windows.h>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9. map<int, string> Data;
  10.  
  11. int ValueBigger(string t1, string t2){
  12.     if(t1.length() < t2.length())
  13.      return -1;
  14.     if(t1.length() > t2.length())
  15.      return 1;
  16.     return strcmp(t1.c_str(), t2.c_str());
  17. }
  18.  
  19. bool LoadFile(const char sciezka[], bool czysc){
  20.     fstream Plik;
  21.     Plik.open(sciezka, ios::in);
  22.     if(!Plik.good())
  23.      return false;
  24.  
  25.     if(czysc)
  26.      Data.clear();
  27.     int n=Data.size();
  28.     while(!Plik.eof())
  29.     {
  30.         string element;
  31.         Plik >> element;
  32.         Data[n] = element;
  33.         n++;
  34.     }
  35.  
  36.     Plik.close();
  37.     return true;
  38. }
  39. bool SaveFile(const char sciezka[]){
  40.     fstream Plik;
  41.     Plik.open(sciezka, ios::out);
  42.     if(!Plik.good())
  43.      return false;
  44.  
  45.     for(int n=0; n<Data.size(); n++)
  46.      Plik << Data[n] << " ";
  47.  
  48.     Plik.close();
  49.     return true;
  50. }
  51. int FindText(string fraza, bool przebieg){
  52.     map<int, string>::iterator it = Data.begin();
  53.     while(true)
  54.     {
  55.         if(it == Data.end())
  56.          return -1;
  57.         if(przebieg)
  58.          cout<<" > Sprawdzam "<<fraza<<" z "<<it->second<<endl;
  59.         if(it->second == fraza)
  60.          return it->first;
  61.         it++;
  62.     }
  63.  
  64.     return -1;
  65. }
  66. void BubbleSortText(){
  67.     if(Data.size() < 2)
  68.      return;
  69.     int pozostalo = Data.size()-1;
  70.     while(pozostalo != 0)
  71.     {
  72.         map<int, string>::iterator it = Data.begin();
  73.         map<int, string>::iterator next = ++Data.begin();
  74.         for(int n=0; n<pozostalo; ++n)
  75.         {
  76.             if(strcmp(it->second.c_str(), next->second.c_str()) > 0)
  77.              swap(it->second, next->second);
  78.             ++it;
  79.             ++next;
  80.         }
  81.         --pozostalo;
  82.     }
  83.  
  84. }
  85. void BubbleSortValue(){
  86.     if(Data.size() < 2)
  87.      return;
  88.     int pozostalo = Data.size()-1;
  89.     while(pozostalo != 0)
  90.     {
  91.         map<int, string>::iterator it = Data.begin();
  92.         map<int, string>::iterator next = ++Data.begin();
  93.         for(int n=0; n<pozostalo; ++n)
  94.         {
  95.             if(ValueBigger(it->second, next->second) > 0)
  96.              swap(it->second, next->second);
  97.             ++it;
  98.             ++next;
  99.         }
  100.         --pozostalo;
  101.     }
  102.  
  103. }
  104. void QuickSortText(int left, int right){
  105.     if(Data.size() < 2)
  106.      return;
  107.     int i = left;
  108.     int j = right;
  109.     string x = Data[( left + right ) / 2 ];
  110.     do
  111.     {
  112.         while( strcmp(Data[ i ].c_str(), x.c_str()) < 0)
  113.              i++;
  114.         while( strcmp(Data[ j ].c_str(), x.c_str()) > 0 )
  115.              j--;
  116.         if( i <= j )
  117.         {
  118.             swap( Data[ i ], Data[ j ] );
  119.             i++;
  120.             j--;
  121.         }
  122.     } while( i <= j );
  123.     if( left < j ) QuickSortText( left, j );
  124.     if( right > i ) QuickSortText( i, right );
  125. }
  126. void QuickSortValue(int left, int right){
  127.     if(Data.size() < 2)
  128.      return;
  129.     int i = left;
  130.     int j = right;
  131.     string x = Data[( left + right ) / 2 ];
  132.     do
  133.     {
  134.         while( ValueBigger(Data[ i ], x) < 0 )
  135.              i++;
  136.         while( ValueBigger(Data[ j ], x) > 0 )
  137.              j--;
  138.         if( i <= j )
  139.         {
  140.             swap( Data[ i ], Data[ j ] );
  141.             i++;
  142.             j--;
  143.         }
  144.     } while( i <= j );
  145.     if( left < j ) QuickSortValue( left, j );
  146.     if( right > i ) QuickSortValue( i, right );
  147. }
  148. void EditIndexData(){
  149.         string command;
  150.         if(Data.size() < 1)
  151.         {
  152.             cout <<" > Lista jest pusta !"<<endl;
  153.             return;
  154.         }
  155.  
  156.         system("cls");
  157.         cout<<"@LISTA: --------------"<<endl;
  158.         for(int i=0; i<Data.size(); ++i)
  159.          cout<<"@ "<<i<<". "<<Data[i]<<endl;
  160.  
  161.         int nr;
  162.         do
  163.         {
  164.             cout<<endl<<" > Ktory element edytowac? :";
  165.             cin >> nr;
  166.         }while(nr < 0 || nr  >= Data.size());
  167.  
  168.  
  169.         cout<<" > Podaj nowy wyraz: ";
  170.         cin >> command;
  171.         cout <<" > Wyraz "<<Data[nr]<<" zamieniony na "<<command<<endl;
  172.         Data[nr] = command;
  173.         return;
  174. }
  175. void EditData(string data){
  176.     string command;
  177.     if(Data.size() < 1)
  178.     {
  179.         cout <<" > Lista jest pusta !"<<endl;
  180.         return;
  181.     }
  182.  
  183.     int index = FindText(data, false);
  184.     if(index == -1)
  185.     {
  186.         cout <<" > Nie znaleziono wyrazu!"<<endl;
  187.         return;
  188.     }
  189.  
  190.     cout <<" > Zamiana wyrazu "<<data<<"(index: "<<index<<") na: ";
  191.     cin >> command;
  192.     Data[index] = command;
  193.     cout <<" > Wyraz "<<data<<"(index: "<<index<<") zamieniony na "<<command<<endl;
  194.     return;
  195. }
  196. void EditDataToData(string data1, string data2){
  197.     if(Data.size() < 1)
  198.     {
  199.         cout <<" > Lista jest pusta !"<<endl;
  200.         return;
  201.     }
  202.  
  203.     int index = FindText(data1, false);
  204.     if(index == -1)
  205.     {
  206.         cout <<" > Nie znaleziono wyrazu!"<<endl;
  207.         return;
  208.     }
  209.     Data[index] = data2;
  210.     cout <<" > Wyraz "<<data1<<" zamieniony na "<<data2<<endl;
  211.     return;
  212. }
  213. void AddData(int ilosc){
  214.     int rozmiar = Data.size();
  215.     srand(time(NULL));
  216.     for(int n=0; n<ilosc; ++n)
  217.     {
  218.         char value[5];
  219.         itoa(rand()%1000, value, 10);
  220.         Data[rozmiar+n] = value;
  221.         cout<<"Dodano index: "<<rozmiar+n<<" = "<<Data[rozmiar+n]<<endl;
  222.     }
  223. }
  224.  
  225. void DisplayStart(){
  226.     cout<<"> Dostepne komendy:"<<endl;
  227.     cout<<"     > load <nazwapliku.txt>"<<endl;
  228.     cout<<"     > save <nazwapliku.txt>"<<endl;
  229.     cout<<"     > find <kryterium> <wyraz>"<<endl;
  230.     cout<<"     > add <ilosc> (argumenty opcjonalne)"<<endl;
  231.     cout<<"     > sort <text/value> <bubble/quick>"<<endl;
  232.     cout<<"     > edit <podmieniany> <podmieniony> (argumenty opcjonalne)"<<endl;
  233.     cout<<"     > rand"<<endl;
  234.     cout<<"     > display"<<endl;
  235.     cout<<"     > exit"<<endl;
  236.     cout<<endl;
  237.     cout<<"# ";
  238. }
  239.  
  240. string ReadNextCommand(string & in){
  241.     int pointer = 0;
  242.     pointer = in.find(' ');
  243.     string out;
  244.     if(pointer != string::npos)
  245.      out = in.substr(0, pointer);
  246.     else
  247.      out = in;
  248.     if(pointer == string::npos)
  249.      in.clear();
  250.     else
  251.      in.erase(0, pointer+1);
  252.     return out;
  253. }
  254. int Dispatch(string in){
  255.     string command = ReadNextCommand(in);
  256.     if(command == "load")
  257.     {
  258.  
  259.         if(!LoadFile(ReadNextCommand(in).c_str(), true))
  260.          cout<<"> Wystapil blad otwarcia pliku."<<endl;
  261.         else
  262.          cout<<"> Dane z pliku wczytane poprawnie."<<endl;
  263.         getch();
  264.         return 0;
  265.     }
  266.     if(command == "save")
  267.     {
  268.  
  269.         if(!SaveFile(ReadNextCommand(in).c_str()))
  270.          cout<<"> Wystapil blad otwarcia pliku."<<endl;
  271.         else
  272.          cout<<"> Pliki zostaly poprawnie zapisane."<<endl;
  273.         getch();
  274.         return 0;
  275.     }
  276.     if(command == "find")
  277.     {
  278.         int result = FindText(ReadNextCommand(in).c_str(), true);
  279.         if(result != -1)
  280.          cout <<" > Wyraz posiada index: "<<result<<" (0 - "<< Data.size()-1 <<")"<<endl;
  281.         else
  282.          cout <<" > Podany wyraz nie istnieje !"<<endl;
  283.         getch();
  284.         return 0;
  285.     }
  286.     if(command == "add")
  287.     {
  288.         string data1 = ReadNextCommand(in);
  289.         if(!data1.length())
  290.          AddData(1);
  291.         else
  292.         {
  293.             if(atoi(data1.c_str()) < 1)
  294.              AddData(1);
  295.             else
  296.              AddData(atoi(data1.c_str()));
  297.         }
  298.         getch();
  299.         return 0;
  300.     }
  301.     if(command == "sort")
  302.     {
  303.         command = ReadNextCommand(in);
  304.         if(command == "text")
  305.         {
  306.             command = ReadNextCommand(in);
  307.             if(command == "bubble"){
  308.                 BubbleSortText();
  309.                 cout<<" > tabela posortowana."<<endl;
  310.             }
  311.             else
  312.             if(command == "quick"){
  313.                 QuickSortText(0, Data.size()-1);
  314.                 cout<<" > tabela posortowana."<<endl;
  315.             }
  316.             else
  317.             cout<<"Niepoprawny rodzaj sortowania!"<<endl;
  318.         }
  319.         else
  320.         if(command == "value")
  321.         {
  322.             command = ReadNextCommand(in);
  323.             if(command == "bubble"){
  324.                 BubbleSortValue();
  325.                 cout<<" > tabela posortowana."<<endl;
  326.             }
  327.             else
  328.             if(command == "quick"){
  329.                 QuickSortValue(0, Data.size()-1);
  330.                 cout<<" > tabela posortowana."<<endl;
  331.             }
  332.             else
  333.             cout<<"Niepoprawny rodzaj sortowania!"<<endl;
  334.         }
  335.         else
  336.          cout<<"Niepoprawny typ sortowania!"<<endl;
  337.         getch();
  338.         return 0;
  339.     }
  340.     if(command == "edit")
  341.     {
  342.         string data1 = ReadNextCommand(in);
  343.         if(!data1.length())
  344.          EditIndexData();
  345.         else
  346.         {
  347.             string data2 = ReadNextCommand(in);
  348.             if(!data2.length())
  349.              EditData(data1);
  350.             else
  351.              EditDataToData(data1, data2);
  352.         }
  353.         getch();
  354.         return 0;
  355.  
  356.     }
  357.     if(command == "rand")
  358.     {
  359.         srand(time(NULL));
  360.         for(int n=0; n<Data.size(); ++n)
  361.          swap(Data[n], Data[rand()%Data.size()]);
  362.         cout<<"Elementy zostaly ulozone losowo."<<endl;
  363.         getch();
  364.         return 0;
  365.     }
  366.     if(command == "display")
  367.     {
  368.         system("cls");
  369.         cout<<"@ LISTA: --------------"<<endl;
  370.         for(int i=0; i<Data.size(); ++i)
  371.          cout<<" "<<i<<". "<<Data[i]<<endl;
  372.         getch();
  373.         return 0;
  374.     }
  375.     if(command == "exit")
  376.     {
  377.         cout<<" > wyjscie"<<endl;
  378.         return 1;
  379.     }
  380.  
  381.     if(command != "")
  382.      cout<<" > Nieprawidlowa komenda!"<<endl;
  383.     getch();
  384.     return 0;
  385. }
  386.  
  387. int main()
  388. {
  389.     while(true)
  390.     {
  391.         char command[100];
  392.         system("cls");
  393.         DisplayStart();
  394.         cin.clear();
  395.         cin.getline(command, 100);
  396.         if(Dispatch(command))
  397.          break;
  398.     }
  399.     return 0;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement