Advertisement
YauhenMardan

Lab6ZAD3

Oct 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5. //////////////////
  6. const int SIZE = 50;
  7. char text[SIZE];
  8. int i = 0;
  9. /////////////////
  10. void toPrint(long double *arr, long double len)
  11. {
  12.     for (int i = 0; i < len; i++)
  13.         cout << arr[i] << " ";
  14. }
  15. /////////////////
  16. void toSort(long double *arr, long double len)
  17. {
  18.     for (int i = 0; i < len; i++)
  19.     {
  20.         int tempo = arr[0];
  21.         for (int j = i + 1; j < len; j++)
  22.         {
  23.             if (arr[i] > arr[j])
  24.             {
  25.                 tempo = arr[i];
  26.                 arr[i] = arr[j];
  27.                 arr[j] = tempo;
  28.             }
  29.         }
  30.     }
  31. }
  32. /////////////////
  33. int size(char txt[])
  34. {
  35.     long double n = 0;
  36.     for (i = 0; i < strlen(txt); i++)
  37.     {
  38.         if ((isdigit(txt[i])))
  39.         {
  40.             while (i < strlen(txt))
  41.             {
  42.                 if (txt[i] == '.')
  43.                     i++;
  44.                 if (!isdigit(txt[i]))
  45.                     break;
  46.                 i++;
  47.             }
  48.             n++;
  49.         }
  50.     }
  51.     return n;
  52. }
  53. /////////////////
  54. long double* toFind(char txt[], int n)
  55. {
  56.     long double *array = new long double[n];
  57.     int k = 0, c = 0;
  58.     i = 0;
  59.    
  60.     for (i = 0; i < strlen(txt); i++)
  61.     {
  62.         char *temp = new char[SIZE];
  63.        
  64.         if (txt[i]!='.' && !isdigit(txt[i]) && isdigit(txt[i + 1]))
  65.         {
  66.             if(txt[i]!='.' && txt[i]=='-' && isdigit(txt[i+1]))
  67.             {
  68.                 for (int r = i; r < strlen(txt); r++)
  69.                 {
  70.                     temp[k] = txt[r];
  71.                     k++;
  72.                 }
  73.                 array[c] = atof(temp);
  74.                 c++;
  75.             }
  76.             for (int r = i + 1; r < strlen(txt); r++)
  77.             {
  78.                 temp[k] = txt[r];
  79.                 k++;
  80.             }
  81.             array[c] = atof(temp);
  82.             c++;
  83.         }
  84.         delete [] temp;
  85.            
  86.        
  87.         k = 0;
  88.        
  89.     }
  90.    
  91.     toSort(array, n);
  92.     return array;
  93.    
  94. }
  95. //////////////
  96. int main()
  97. {
  98.     cout << "Write your text: ";  cin.getline(text, SIZE, '&');
  99.     cout << "Your text: " << text << endl;
  100.     //ARRAY//
  101.     int sz = size(text);
  102.     long double *array = new long double[sz];
  103.     //FILL IN ARRAY//
  104.     array=toFind(text, sz);
  105.     //PRINT ARRAY//
  106.     toPrint(array, sz);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement