usaa21/cv2/program.c
2021-11-25 23:19:25 +00:00

64 lines
2.1 KiB
C
Raw Permalink 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>
#define SIZE 100 //колличество пицц
struct dish{
char name[100];
int price;
}; // блюда -- название и цена
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(){
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(); //убирает перенос строки с ввода
}
insertSort(dishesList, finalCounter);
for(int j = 0; j < finalCounter; j++){ //вывод
printf("%s", dishesList[j].name);
printf("%f\n", dishesList[j].price);
}
return 0;
}