Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- bool isDigit(char a) {
- if (a >= 48 && a <= 57) {
- return true;
- }
- else {
- return false;
- }
- }
- int JapCompare(std::string str1, std::string str2) {
- //Tworzymy tablicy na ktorych bedzimy pracowac
- const char *s1 = str1.c_str();
- const char *s2 = str2.c_str();
- int index1 = 0;
- int index2 = 0;
- int log = 0;
- bool prev = false;
- int n1;
- int n2;
- while (true) {
- char e1 = NULL;
- char e2 = NULL;
- e1 = s1[index1];
- e2 = s2[index2];
- if (e1 == NULL) {
- if (e2 == NULL) {
- if (log == 1 ) {
- return -1;
- }
- else if(log == -1 ){
- return 1;
- }
- if (index1 >= index2) {
- return 1;
- }
- else {
- return -1;
- }
- }
- else {
- return -1;
- }
- }
- else if (e2 == NULL) {
- return 1;
- }
- //Porownujemy liczby
- if (isDigit(e1) && isDigit(e2)) {
- //Poczatek liczb
- int start1 = index1;
- int start2 = index2;
- //Dlugosc liczb
- n1 = 1;
- n2 = 1;
- while (s1[++index1] != NULL && isDigit(s1[index1])) {
- n1++;
- }
- while (s2[++index2] != NULL && isDigit(s2[index2])) {
- n2++;
- }
- //MAX
- int n;
- if (n1 > n2) {
- n = n1;
- }
- else {
- n = n2;
- }
- int d1 = n1 - n;
- int d2 = n2 - n;
- e1 = '0';
- e2 = '0';
- for (int i = 0; i < n; i++) {
- //Porownujemy
- if ((i + d1) >= 0) {
- e1 = s1[start1++];
- }
- if ((i + d2) >= 0) {
- e2 = s2[start2++];
- }
- if (e1 > e2) {
- return 1;
- }
- if (e1 < e2) {
- return -1;
- }
- }
- if (n1 < n2) {
- if (s1[index1] == NULL && s2[index2] == NULL) {
- if (prev == true) {
- if (log == 1) {
- return -1;
- }
- else if (log == -1 ){
- return 1;
- }
- }
- return 1;
- }
- if (s2[index2] == NULL) {
- return 1;
- }
- if (s1[index1] == NULL) {
- return -1;
- }
- if (prev == false) {
- log = -1;
- prev = true;
- }
- }
- if (n1 > n2) {
- if (s1[index1] == NULL && s2[index2] == NULL) {
- if (prev == true) {
- if (log == 1) {
- return -1;
- }
- else if(log == -1){
- return 1;
- }
- }
- return -1;
- }
- if (s2[index2] == NULL) {
- return 1;
- }
- if (s1[index1] == NULL) {
- return -1;
- }
- if (prev == false) {
- log = 1;
- prev = true;
- }
- }
- }
- else {
- if (e1 > e2) {
- return 1;
- }
- if (e1 < e2) {
- return -1;
- }
- index1++;
- index2++;
- }
- }//while
- }
- bool japLess(std::string str1, std::string str2) {
- if (JapCompare(str1, str2) == -1) {
- return true;
- }
- return false;
- }
- int main()
- {
- int count = 0;
- std::string k;
- std::vector<std::string> V;
- std::ifstream R("Test.txt");
- if (R.is_open()) {
- while (std::getline(R , k)) {
- if (k.empty()) {
- break;
- }
- V.push_back(k);
- count++;
- }
- }
- else {
- std::cout << "Unable open" << std::endl;
- }
- while (std::getline(std::cin, k)) {
- if (k.empty()) {
- break;
- }
- V.push_back(k);
- count++;
- }
- std::sort(V.begin(), V.end(), japLess);
- //Printting
- for (int i = 0; i < count; i++) {
- std::cout << V[i] << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement