Добавил:
serega_ovc
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:lab1 / main
.cpp#include <QCoreApplication>
#include <iostream>
#include <cstdio>
using namespace std;
struct vect{
int* arr;
int size;
};
void print_vector(vect &vector,int start,int len){
if(vector.size < len) len = vector.size;
cout<<"Обработанный вектор = (";
for (int i= 0;i<len;i++){
cout<<vector.arr[start+i];
if(i != (len-1)) cout<<",";
}
cout<<")"<<endl;
}
void change(vect &vector,int start,int len){
if(len > 2){
if(len % 2 == 1){
change(vector,start,len/2+1);
change(vector,start + len/2 ,len/2);
}else{
change(vector,start,len/2);
change(vector,start + len/2,len/2);
}
}
if(len == 2 && (vector.arr[start +1] < vector.arr[start])){
int x = vector.arr[start];
vector.arr[start] = vector.arr[start+1];
vector.arr[start+1] = x;
}
if(len == 1) return;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char exit_str[20];
vect vector;
do{
char str[5];
printf("%s","Введите длину вектора: ");
fgets(str, sizeof(str), stdin);
while (sscanf(str, "%d", &vector.size) != 1) {
printf("Ошибка ввода. Попробуйте ещё:");
fgets(str, sizeof(str), stdin);
}
int arr[vector.size];
vector.arr = arr;
printf("Введите вектор \n");
for (int i=0;i<vector.size;i++){
printf("%d число:",i+1);
fgets(str, sizeof(str), stdin);
while (sscanf(str, "%d", &arr[i]) != 1) {
printf("Ошибка ввода. Попробуйте ещё:");
fgets(str, sizeof(str), stdin);
}
}
change(vector,0,vector.size);
print_vector(vector,0,vector.size);
printf("Введите new для продолжения\n");
scanf("%s",exit_str);
printf("\n");
fgets(str, sizeof(str), stdin);
}while(strcmp(exit_str,"new") == 0);
exit(1);
return a.exec();
}