usaa21/cv2/program.c

131 lines
4.8 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct dish{
char name[100];
float 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
}
int main(){
struct dish dishesList[100]; //список блюдд
int i = 0;
int finalCounter = 0; //конечный счётчик колличества блюд
for(i = 0; fgets(dishesList[i].name, 100, stdin); i++){ // запись блюд с ввода
finalCounter++;
if(dishesList[i].name[0] == '\n' || dishesList[i].name[0] == '\0') finalCounter--; //если название пустое
scanf("%f", &dishesList[i].price);
if(dishesList[i].price == 0.0){ //если цены нету
finalCounter--;
break;
}
getchar(); //убирает перенос строки с ввода
}
int* printOrder = calloc(finalCounter, sizeof(int)); //полная запись всего результата
getOrder(dishesList, finalCounter, printOrder);
getAlphabetOrder(printOrder, dishesList, finalCounter);
for(int j = 0; j < finalCounter; j++){ //вывод
printf("%s", dishesList[printOrder[j]].name);
printf("%f\n", dishesList[printOrder[j]].price);
}
return 0;
}