veselinsavov

pik2/upr

Mar 16th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. //Author: vsavovpc
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  struct student {
  6.      int id;
  7.      char name[50];
  8.      char spec[50];
  9.      double price;
  10.  };
  11.  
  12. int main()
  13. {
  14.     int input = 0;
  15.     while (scanf("%d",&input))
  16.     {
  17.         if (input == 3)
  18.         {
  19.             break;
  20.         }
  21.    
  22.  
  23.         switch (input)
  24.         {
  25.         case 1: {
  26.                 FILE *fp;
  27.                 int i;
  28.                 struct student n;
  29.  
  30.                 if ((fp = fopen("gosho", "wb")) == NULL) {
  31.                     exit(1);
  32.                 }
  33.                
  34.                 for (i = 0; i < 1; i++)
  35.                 {
  36.                     printf("id: ");
  37.                     fflush(stdin);
  38.                     scanf("%d", &n.id);
  39.  
  40.                     printf("Name: ");
  41.                     fflush(stdin);
  42.                     scanf("%s", &n.name);
  43.  
  44.                     printf("Spec: ");
  45.                     fflush(stdin);
  46.                     scanf("%s", &n.spec);
  47.  
  48.                     printf("price: ");
  49.                     fflush(stdin);
  50.                     scanf("%lf", &n.price);
  51.  
  52.                     fwrite(&n, sizeof(n), 1,fp);
  53.                 }
  54.  
  55.                 fclose(fp);
  56.         }; break;
  57.         case 2:{
  58.                    FILE *fp;
  59.                    fp = fopen("gosho", "rb");
  60.                    struct student n;
  61.                    int id;
  62.                    char name[50];
  63.                    char spec[50];
  64.                    double price = 100000;
  65.  
  66.                    while (fread(&n, 1, sizeof(n), fp) != NULL){
  67.                        printf("print from reading: id: %d  name: %s  spec: %s   price: %lf\n", n.id ,n.name,  n.spec, n.price);
  68.                        if (price > n.price)
  69.                        {
  70.                             id = n.id;
  71.                             strncpy(name, n.name, 50);
  72.                             strncpy(spec, n.spec, 50);
  73.                             price = n.price;
  74.                        }
  75.                    }
  76.                
  77.                    printf("Product with min price is: id: %d  name: %s  spec: %s   price: %lf\n", id, name, spec, price);
  78.                    fclose(fp);
  79.  
  80.         }; break;
  81.         default:
  82.             break;
  83.         }
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment