Advertisement
Guest User

Untitled

a guest
May 24th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. /*
  2. Лабораторная работа №21
  3. */
  4.  
  5.  
  6. #include "stdafx.h"
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <conio.h>
  11. #include <vector>
  12. #include <time.h>
  13. #include <string>
  14. #include <sstream>
  15. #include <fstream>
  16. #include <list>
  17. #include <algorithm>
  18. #include <iterator>
  19. #include<windows.h>
  20.  
  21. int is_sorted(int *, int);
  22. void shuffle(int *, int);
  23. void bogosort(int *, int);
  24. using namespace std;
  25.  
  26. int GetWords(string str) // функция подсчета количества слов.
  27. {
  28.     const char* s = str.c_str();
  29.     unsigned int a,i;
  30.     a=0;
  31.     for (i=0;i<strlen(s)-1;i++)
  32.      if ((s[i]==' ') && (s[i+1]!=' '))
  33.       a=a+1;
  34.     if (s[0]!=' ')
  35.         a=a+1;
  36.     return a;
  37. }
  38.  
  39.  
  40. void WriteFile(string str,int *a,int size) // запись в выходной файл
  41. {
  42.          ofstream output_file(str, std::ios_base::app);
  43.          for(int i = 0; i < size; i++)
  44.          {
  45.             output_file << a[i]<<" ";
  46.          }
  47.          output_file<<endl;
  48. }
  49.  
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53.     string file = "test.txt";
  54.     string file_o = "test2.txt";
  55.     bool test = false;
  56.      if (argc > 1)
  57.        {
  58.              file = argv[1]; // аргумент входного файла
  59.        }
  60.      if (argc > 2)
  61.        {
  62.              file_o = argv[2]; // аргумент выходного файла
  63.        }
  64.      if (argc > 3)
  65.        {
  66.            if(strcmp(argv[3],"-t")==0) // флаг теста
  67.            {
  68.              test = true;
  69.            }
  70.  
  71.          
  72.        }
  73.     fstream myfile;
  74.     string line;
  75.    
  76.     int lineCount = 0;
  77.     int allSize = 0;
  78.     int start=GetTickCount();
  79.     myfile.open(file);
  80.     if(myfile.is_open())
  81.      {
  82.       while ( getline (myfile,line) )
  83.       {
  84.             lineCount++;
  85.  
  86.             int size = GetWords(line);    // подсчитываем количество информации
  87.             allSize = allSize + size;
  88.  
  89.             int *numbers = new int[size];  // выделяем массив под нее int
  90.  
  91.             string *Words = new string[size]; // выделяем массив под строки
  92.             int i = 0;
  93.             stringstream ssin(line);
  94.             while (ssin.good() && i < size){
  95.                 ssin >> Words[i];  // считываем всю информацию как строки
  96.                 ++i;
  97.             }
  98.  
  99.             for(int i = 0; i < size; i++)  
  100.                  numbers[i] = atoi(Words[i].c_str()); // переводим всю информацию в int
  101.  
  102.             if(test == false)
  103.             {
  104.                 bogosort(numbers, size);   // сортируем
  105.             }
  106.  
  107.             WriteFile(file_o,numbers,size); // пишем в выходной файл
  108.        }
  109.              myfile.close(); // закрываем файл
  110.             allSize = allSize / lineCount; // считаем среднюю длинну строки
  111.             int end=GetTickCount(); // засекаем время окончания работы программы
  112.             cout<<end-start<<" "<<allSize<<" "<<lineCount<<endl; // выводи статистику
  113.      }
  114.  
  115.  
  116.     _getch();
  117. }
  118.  
  119. int is_sorted(int *a, int n)
  120. {
  121.     while (--n >= 1)
  122.     {
  123.         if (a[n] < a[n - 1])
  124.         {
  125.             return 0;
  126.           }
  127.     }
  128.       return 1;
  129. }
  130.  
  131.  
  132. void shuffle(int *a, int n)
  133. {
  134.     int i, t, temp;
  135.     for (i = 0;i < n;i++)
  136.     {
  137.         t = a[i];
  138.         temp = rand() % n;    
  139.         a[i] = a[temp];
  140.         a[temp] = t;
  141.     }
  142. }
  143.  
  144. void bogosort(int *a, int n)
  145. {
  146.     while (!is_sorted(a, n))
  147.     {
  148.         shuffle(a, n);
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement