Advertisement
Miha_Ch

Ice Cream

May 11th, 2025
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | Cryptocurrency | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // ex 1
  6. struct IceCream{
  7.   char id[3];
  8.   char name[21];
  9.   int weight;
  10.   float price;
  11.  };
  12.  
  13. int main() {
  14.     printf("Enter n:\n ");
  15.     int n;
  16.     scanf("%d", &n);
  17.  
  18.     if (n < 5 || n > 15) {
  19.       printf("Wrong input\n");
  20.       return 1;
  21.     }
  22.     struct IceCream* iceCream = (struct IceCream*)malloc(n*sizeof(struct IceCream));
  23.     if (iceCream == NULL) {
  24.       printf("Memory allocation error\n");
  25.       return 1;
  26.     }
  27.  
  28.     for (int i = 0; i < n; i++) {
  29.       printf("Enter id:\n ");
  30.       scanf("%s", iceCream[i].id);
  31.       printf("Enter name:\n ");
  32.       scanf("%s", iceCream[i].name);
  33.       printf("Enter weight:\n ");
  34.       scanf("%d", &iceCream[i].weight);
  35.       printf("Enter price:\n ");
  36.       scanf("%f", &iceCream[i].price);
  37.     }
  38.     return 0;
  39. }
  40.  
  41. // ex 2
  42. float sum(struct IceCream* iceCream, int n, char letter) {
  43.   float sum = 0;
  44.   for (int i = 0; i < n; i++) {
  45.     if (iceCream[i].name[0] == letter) {
  46.       sum += iceCream[i].price;
  47.     }
  48.   }
  49.   if(sum > 0){
  50.     return sum;
  51.    }
  52.    return 0;
  53. }
  54.  
  55. // ex 3
  56. int write(struct IceCream* iceCream, int n, float price, int weight) {
  57.   FILE *file = fopen("info.txt", "w");
  58.   if (file == NULL) {
  59.     printf("File could not be opened\n");
  60.     return 1;
  61.   }
  62.   int counter = 0;
  63.   for (int i = 0; i < n; i++) {
  64.     if (iceCream[i].price < price && iceCream[i].weight > weight) {
  65.       counter++;
  66.       fprintf(file, "%s; %s; %d; %.2fлв\n", iceCream[i].id, iceCream[i].name, iceCream[i].weight, iceCream[i].price);
  67.     }
  68.   }
  69.   fclose(file);
  70.   free(iceCream);
  71.   if (counter > 0) {
  72.     return counter;
  73.   }
  74.   return 0;
  75. }
  76.  
  77. struct binary{
  78.   char id[3];
  79.   int nameLength;
  80.   char name[100];
  81.   int weight;
  82.   float price;
  83.   };
  84. //ex 4
  85. int info(char id[3]) {
  86.   FILE *file = fopen("icecream.bin", "rb");
  87.   if (file == NULL) {
  88.     printf("File could not be opened\n");
  89.     return 1;
  90.   }
  91.  
  92.   if (fseek(file, 0, SEEK_END) != 0) {
  93.     printf("File could not be seeked\n");
  94.     fclose(file);
  95.     return 1;
  96.   }
  97.   long size = ftell(file);
  98.   if (size < 0) {
  99.     printf("Error determining file size\n");
  100.     fclose(file);
  101.     return 1;
  102.   }
  103.   rewind(file);
  104.   int n = size/sizeof(struct binary);
  105.  
  106.   struct binary* info = (struct binary*)malloc(n*sizeof(struct binary));
  107.   if (info == NULL) {
  108.     printf("Memory could not be allocated\n");
  109.     fclose(file);
  110.     return 1;
  111.   }
  112.   if (fread(info, sizeof(struct binary), n, file) != n) {
  113.     if (feof(file)) {
  114.       printf("File could not be read\n");
  115.     } else if (feof(file)) {
  116.       printf("Error determining file size\n");
  117.     }
  118.     fclose(file);
  119.     free(info);
  120.     return 1;
  121.   }
  122.  
  123.   int find = -1;
  124.   for (int i = 0; i < n; i++) {
  125.     if (strcmp(info[i].id, id) == 0) {
  126.       find = i;
  127.     }
  128.   }
  129.  
  130.   if (find != -1) {
  131.     printf("%s - %s - %d - %.2f", info[find].name, info[find].id, info[find].weight, info[find].price);
  132.   }
  133.   else{
  134.     printf("IceCream does not exist\n");
  135.   }
  136.  
  137.   fclose(file);
  138.   free(info);
  139.  
  140.   return 0;
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement