Advertisement
Anton0093

3_4

Oct 11th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. class sort
  6. {
  7. public:
  8.     bool vyvod(string file);
  9.     void print();
  10.     void sort_1();
  11.     void sort_2();
  12. private:
  13.     int n;
  14.     char *str;
  15. };
  16.  
  17. bool sort::vyvod(string file){
  18.  ifstream myfile("/Users/antontamarov/Desktop/"+file);
  19.     if (!myfile.is_open())
  20.     {
  21.         cout<<"Файл не открыт"<<endl;
  22.         return 0;
  23.     }
  24.     else
  25.     {
  26.         cout<<"Файл успешно открыт"<<endl;
  27.         myfile>>n;
  28.         cout<<"количетсво элементов в файле: "<<n<<endl;
  29.         str = new char[n];
  30.         int i=0;
  31.         while (true)
  32.         {
  33.             myfile>>str[i];
  34.             i++;
  35.             if (i==n)
  36.                 break;
  37.         }
  38.     myfile.close();
  39.     return 1;
  40.     }
  41. };
  42.  
  43. void sort::print(){
  44.     for(int i=0; i < n; ++i){
  45.         cout<<"str["<<i<<"]="<<str[i]<<endl;
  46.     }
  47. };
  48.  
  49. void sort::sort_1(){
  50.     char a;
  51.     int i;
  52.     int j;
  53.     for(i=1; i < n; ++i){
  54.         a = str[i];
  55.         for(j=i-1; (j >= 0) && (a <= str[j]); j--)
  56.             str[j+1] = str[j];
  57.         str[j+1] = a;
  58.     }
  59. };
  60.  
  61. void sort::sort_2(){
  62.     char a;
  63.     for(int i = 1; i < n; ++i)
  64.         for(int j = n-1; j >= i; --j){
  65.             if(str[j-1] >= str[j]){
  66.                 a = str[j-1];
  67.                 str[j-1] = str[j];
  68.                 str[j] = a;
  69.             }
  70.         }
  71. };
  72.  
  73. int main(){
  74. setlocale(LC_ALL, "Russian");
  75. class sort A;
  76. string FileName;
  77. cout<<"Введите название файла: ";
  78. cin>>FileName;
  79.     while (!A.vyvod(FileName)) {
  80.         cout<<"Введите название файла: ";  //"/Users/antontamarov/Desktop/file_1.txt"
  81.         cin>>FileName;
  82.     }
  83.     A.print();
  84.     A.sort_1();
  85.     cout<<"Новая матрица, полученная сортировкой вcтавками: "<<endl;
  86.     A.print();
  87.     A.vyvod(FileName);
  88.     A.print();
  89.     A.sort_2();
  90.     cout<<"Новая матрица, полученная сортировкой обменом: "<<endl;
  91.     A.print();
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement