Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int MAX_ARRAYS = 10;
- const int MAX_LENGTH = 128;
- char** allocateMemory(int rows, int cols);
- void deallocateMemory(char** p, int rows);
- void assignData(char **destination, char **source, int rows);
- int loadStrings(char** strings);
- void printMenu();
- void print(char** strings, int loaded);
- void asciiOrder(char** strings, int loaded);
- void lenOrder(char** strings, int loaded);
- void baseOption(char** strings, int loaded);
- void asciiOption(char** strings, int loaded);
- void lenOption(char** strings, int loaded);
- int main(int argc, char const *argv[])
- {
- char** strings = allocateMemory(MAX_ARRAYS, MAX_LENGTH);
- cout << "Wczytaj maks 10 lancuchow znakow, jesli chcesz zakonczyc wcisnij Ctrl-D" << '\n';
- int loaded = loadStrings(strings);
- baseOption(strings, loaded);
- asciiOption(strings, loaded);
- lenOption(strings, loaded);
- baseOption(strings, loaded);
- deallocateMemory(strings, MAX_ARRAYS);
- return 0;
- }
- int loadStrings(char** strings)
- {
- int i = 0;
- while(cin.getline(strings[i], MAX_LENGTH) && i < MAX_ARRAYS)
- {
- i++;
- }
- return i;
- }
- void printMenu()
- {
- cout << " ----------------------------- \n"
- << "1. Wyswietlenie pierwotnej listy lancuchow \n"
- << "2. Wyswietlenie lancuchow w porzadku ASCI \n"
- << "3. Wyswietlenie lancuchow wg dlugosci \n"
- << "4. Koniec \n "
- << "----------------------------- \n";
- }
- void print(char** strings, int loaded)
- {
- for(int i = 0; i < loaded; i++)
- {
- cout << i + 1 << " | " << strings[i] << '\n';
- }
- }
- void asciiOrder(char** strings, int loaded)
- {
- char temp[MAX_LENGTH];
- for(int i = 0; i < loaded - 1; i++)
- {
- for(int j = 0; j < loaded - 1; j++)
- {
- if(strcmp(strings[j], strings[j +1]) > 0)
- {
- strcpy(temp, strings[j]);
- strcpy(strings[j], strings[j + 1]);
- strcpy(strings[j + 1], temp);
- }
- }
- }
- }
- void lenOrder(char** strings, int loaded)
- {
- char temp[MAX_LENGTH];
- for(int i = 0; i < loaded - 1; i++)
- {
- for(int j = 0; j < loaded - 1; j++)
- {
- if(strlen(strings[j]) < strlen(strings[j + 1]))
- {
- strcpy(temp, strings[j]);
- strcpy(strings[j], strings[j + 1]);
- strcpy(strings[j + 1], temp);
- }
- }
- }
- }
- char** allocateMemory(int rows, int cols)
- {
- char **p = new char*[rows];
- for(int i = 0; i < cols; i++)
- {
- p[i] = new char[cols];
- }
- return p;
- }
- void deallocateMemory(char** p, int rows)
- {
- for(int i = 0; i < rows; i++)
- {
- delete [] p[i];
- }
- delete [] p;
- }
- void assignData(char** destination, char **source, int rows)
- {
- for(int i = 0; i < rows; i++)
- {
- strcpy(destination[i], source[i]);
- }
- }
- void baseOption(char** strings, int loaded)
- {
- cout << "### Pierwotna lista ### " << '\n';
- print(strings, loaded);
- }
- void asciiOption(char** strings, int loaded)
- {
- char **p = allocateMemory(loaded, MAX_LENGTH);
- assignData(p, strings, loaded);
- cout << "### Posortowana wg ASCI ###" << '\n';
- asciiOrder(p, loaded);
- print(p, loaded);
- deallocateMemory(p, loaded);
- }
- void lenOption(char** strings, int loaded)
- {
- char **p = allocateMemory(loaded, MAX_LENGTH);
- assignData(p, strings, loaded);
- cout << "### Posortowana wg długości ###" << '\n';
- lenOrder(p, loaded);
- print(p, loaded);
- deallocateMemory(p, loaded);
- }
Add Comment
Please, Sign In to add comment