Изменил(а) на 'cv2/program.c'
This commit is contained in:
parent
158a70911c
commit
bf5d407edc
124
cv2/program.c
124
cv2/program.c
@ -3,103 +3,38 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 100
|
||||
|
||||
struct dish{
|
||||
char name[100];
|
||||
float price;
|
||||
int price;
|
||||
}; // блюда -- название и цена
|
||||
|
||||
int comparator (const void * p1, const void * p2){
|
||||
return (*(int*)p1 - *(int*)p2);
|
||||
} // сравнитель для сортировки
|
||||
|
||||
int* insertSort(int* array, int size){
|
||||
int temp;
|
||||
for(int i = 1; i < size; i++){
|
||||
if(array[i].price < array[i-1].price){
|
||||
for(int j = i-1; j >= 0; j--){
|
||||
if(array[i].price >= array[j].price){
|
||||
break;
|
||||
}
|
||||
else{
|
||||
temp = array[j].price;
|
||||
array[j].price = array[i].price;
|
||||
array[i].price = temp;
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
void getOrder(struct dish dishesList[100], int dishesNumber, int* result){ //сортиовка по ценам
|
||||
float allPrises[dishesNumber]; //все цены
|
||||
float ascendingPrises[dishesNumber]; // для сортировки в будущем
|
||||
int* order = calloc(dishesNumber, sizeof(int)); //порядок возрастающих элементов в масиве
|
||||
int counter = 0; //счетчик
|
||||
|
||||
for(int i = 0; i < dishesNumber; i++){
|
||||
allPrises[i] = dishesList[i].price;
|
||||
ascendingPrises[i] = allPrises[i];
|
||||
}
|
||||
|
||||
ascendingPrises = insertSort(ascendingPrices, dishesNumber); //процеес сортировки
|
||||
|
||||
for(int i = 0; i < dishesNumber; i++){ //запись порядка элемента в основном массиве
|
||||
for(int j = 0; j < dishesNumber; j++){
|
||||
STEP:
|
||||
if(ascendingPrises[i] == allPrises[j]){
|
||||
for(int y = 0; y < counter; y++){
|
||||
if(order[y] == j){
|
||||
j++;
|
||||
goto STEP;
|
||||
}
|
||||
}
|
||||
order[counter] = j;
|
||||
counter++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(result, order, dishesNumber * sizeof(int)); //копирование одного в другой
|
||||
}
|
||||
|
||||
void getAlphabetOrder(int* order, struct dish dishesList[100], int dishesNumber){ //сорторвка по алфавиту
|
||||
int* result = calloc(dishesNumber, sizeof(int));
|
||||
int counter = 0;
|
||||
int temp;
|
||||
|
||||
for(int i = 0; i < dishesNumber; i++){
|
||||
for(int j = i; j < dishesNumber; j++){ //чтение масивов их сравнение
|
||||
if(dishesList[order[i]].price == dishesList[order[j]].price){ //сравнение если цены совпадают
|
||||
if(dishesList[order[i]].name[0] < dishesList[order[j]].name[0]){ // если цены сопали но уже в порядке алфавита
|
||||
break;
|
||||
}
|
||||
else if(dishesList[order[i]].name[0] == dishesList[order[j]].name[0]){ //если цены совпали но первые буквы одиннаковые
|
||||
if(dishesList[order[i]].name[1] < dishesList[order[j]].name[1]){
|
||||
break;
|
||||
}
|
||||
else{
|
||||
temp = order[i];
|
||||
order[i] = order[j];
|
||||
order[j] = temp;
|
||||
}
|
||||
}
|
||||
else{ //цены совпали но не в порядке алфавита
|
||||
temp = order[i];
|
||||
order[i] = order[j];
|
||||
order[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
result[counter] = order[i]; //запись сортирвки
|
||||
counter++;
|
||||
}
|
||||
|
||||
memcpy(order, result, dishesNumber * sizeof(int)); //запись результата в main
|
||||
void insertSort(struct pizza array[100], int finalCounter){
|
||||
struct pizza temp;
|
||||
for(int i = 1; i < finalCounter; i++){
|
||||
if(array[i].price < array[i-1].price){
|
||||
for(int j = i-1; j >= 0; j--){
|
||||
if(array[i].price > array[j].price){
|
||||
break;
|
||||
}
|
||||
else if(array[i].price == array[j].price){
|
||||
if(strcmp(array[i].name, array[j].name) < 0){
|
||||
temp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = temp;
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
else{
|
||||
temp = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = temp;
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
@ -119,8 +54,7 @@ int main(){
|
||||
}
|
||||
|
||||
int* printOrder = calloc(finalCounter, sizeof(int)); //полная запись всего результата
|
||||
getOrder(dishesList, finalCounter, printOrder);
|
||||
getAlphabetOrder(printOrder, dishesList, finalCounter);
|
||||
insertSort(dishesList, finalCounter);
|
||||
|
||||
for(int j = 0; j < finalCounter; j++){ //вывод
|
||||
printf("%s", dishesList[printOrder[j]].name);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user