Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class sort
- {
- public:
- bool vyvod(string file);
- void print();
- void sort_1();
- void sort_2();
- private:
- int n;
- char *str;
- };
- bool sort::vyvod(string file){
- ifstream myfile("/Users/antontamarov/Desktop/"+file);
- if (!myfile.is_open())
- {
- cout<<"Файл не открыт"<<endl;
- return 0;
- }
- else
- {
- cout<<"Файл успешно открыт"<<endl;
- myfile>>n;
- cout<<"количетсво элементов в файле: "<<n<<endl;
- str = new char[n];
- int i=0;
- while (true)
- {
- myfile>>str[i];
- i++;
- if (i==n)
- break;
- }
- myfile.close();
- return 1;
- }
- };
- void sort::print(){
- for(int i=0; i < n; ++i){
- cout<<"str["<<i<<"]="<<str[i]<<endl;
- }
- };
- void sort::sort_1(){
- char a;
- int i;
- int j;
- for(i=1; i < n; ++i){
- a = str[i];
- for(j=i-1; (j >= 0) && (a <= str[j]); j--)
- str[j+1] = str[j];
- str[j+1] = a;
- }
- };
- void sort::sort_2(){
- char a;
- for(int i = 1; i < n; ++i)
- for(int j = n-1; j >= i; --j){
- if(str[j-1] >= str[j]){
- a = str[j-1];
- str[j-1] = str[j];
- str[j] = a;
- }
- }
- };
- int main(){
- setlocale(LC_ALL, "Russian");
- class sort A;
- string FileName;
- cout<<"Введите название файла: ";
- cin>>FileName;
- while (!A.vyvod(FileName)) {
- cout<<"Введите название файла: "; //"/Users/antontamarov/Desktop/file_1.txt"
- cin>>FileName;
- }
- A.print();
- A.sort_1();
- cout<<"Новая матрица, полученная сортировкой вcтавками: "<<endl;
- A.print();
- A.vyvod(FileName);
- A.print();
- A.sort_2();
- cout<<"Новая матрица, полученная сортировкой обменом: "<<endl;
- A.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement