Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Лабораторная работа №21
- */
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <vector>
- #include <time.h>
- #include <string>
- #include <sstream>
- #include <fstream>
- #include <list>
- #include <algorithm>
- #include <iterator>
- #include<windows.h>
- int is_sorted(int *, int);
- void shuffle(int *, int);
- void bogosort(int *, int);
- using namespace std;
- int GetWords(string str) // функция подсчета количества слов.
- {
- const char* s = str.c_str();
- unsigned int a,i;
- a=0;
- for (i=0;i<strlen(s)-1;i++)
- if ((s[i]==' ') && (s[i+1]!=' '))
- a=a+1;
- if (s[0]!=' ')
- a=a+1;
- return a;
- }
- void WriteFile(string str,int *a,int size) // запись в выходной файл
- {
- ofstream output_file(str, std::ios_base::app);
- for(int i = 0; i < size; i++)
- {
- output_file << a[i]<<" ";
- }
- output_file<<endl;
- }
- int main(int argc, char *argv[])
- {
- string file = "test.txt";
- string file_o = "test2.txt";
- bool test = false;
- if (argc > 1)
- {
- file = argv[1]; // аргумент входного файла
- }
- if (argc > 2)
- {
- file_o = argv[2]; // аргумент выходного файла
- }
- if (argc > 3)
- {
- if(strcmp(argv[3],"-t")==0) // флаг теста
- {
- test = true;
- }
- }
- fstream myfile;
- string line;
- int lineCount = 0;
- int allSize = 0;
- int start=GetTickCount();
- myfile.open(file);
- if(myfile.is_open())
- {
- while ( getline (myfile,line) )
- {
- lineCount++;
- int size = GetWords(line); // подсчитываем количество информации
- allSize = allSize + size;
- int *numbers = new int[size]; // выделяем массив под нее int
- string *Words = new string[size]; // выделяем массив под строки
- int i = 0;
- stringstream ssin(line);
- while (ssin.good() && i < size){
- ssin >> Words[i]; // считываем всю информацию как строки
- ++i;
- }
- for(int i = 0; i < size; i++)
- numbers[i] = atoi(Words[i].c_str()); // переводим всю информацию в int
- if(test == false)
- {
- bogosort(numbers, size); // сортируем
- }
- WriteFile(file_o,numbers,size); // пишем в выходной файл
- }
- myfile.close(); // закрываем файл
- allSize = allSize / lineCount; // считаем среднюю длинну строки
- int end=GetTickCount(); // засекаем время окончания работы программы
- cout<<end-start<<" "<<allSize<<" "<<lineCount<<endl; // выводи статистику
- }
- _getch();
- }
- int is_sorted(int *a, int n)
- {
- while (--n >= 1)
- {
- if (a[n] < a[n - 1])
- {
- return 0;
- }
- }
- return 1;
- }
- void shuffle(int *a, int n)
- {
- int i, t, temp;
- for (i = 0;i < n;i++)
- {
- t = a[i];
- temp = rand() % n;
- a[i] = a[temp];
- a[temp] = t;
- }
- }
- void bogosort(int *a, int n)
- {
- while (!is_sorted(a, n))
- {
- shuffle(a, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement