Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct food_t{
  4.         double calories;
  5.         int meat;
  6. };
  7.  
  8. int can_eat(struct food_t, int);
  9. int want_to_eat(struct food_t[10],int, int);
  10.  
  11. int main(int argc, const char *argv[]){
  12.  
  13.     struct food_t eat;
  14.     eat.calories = 300;
  15.     eat.meat = 1;
  16.     int vegetarian = 1;
  17.  
  18.     printf("May I eat this food?\n");
  19.  
  20.     if(can_eat(eat, vegetarian)){
  21.             printf("Yes, you could.\n");
  22.     }else{
  23.             printf("No, you couldn't. You are vegetarian.\n");
  24.     }
  25.     ///////////////////////////////////////////////////////////////////
  26.     struct food_t dish[10];
  27.     dish[0].calories = 300;
  28.     dish[0].meat = 1;
  29.  
  30.     dish[1].calories = 300;
  31.     dish[1].meat = 0;
  32.  
  33.     dish[2].calories = 150;
  34.     dish[2].meat = 0;
  35.  
  36.     int numOfDishes = want_to_eat(dish,140,1);
  37.     printf("\nHow much dishes for me: %d\n", numOfDishes);
  38.     ///////////////////////////////////////////////////////////////////
  39.     return 0;
  40. }
  41.  
  42. int want_to_eat(struct food_t food[10], int caloriesIwant, int vegetarian){
  43.     int delicious = 0;
  44.      for(int i = 0; i < 10; i++){
  45.              if((food[i].calories <= caloriesIwant)&&(food[i].meat != vegetarian)){
  46.                      delicious ++;
  47.              }
  48.      }
  49.  
  50.    return delicious;
  51. }
  52.  
  53. int can_eat(struct food_t food, int vegetarian){
  54.         if((vegetarian == 1)&&(food.meat == 0)){
  55.                 return 1;
  56.         }else if(vegetarian == 0){
  57.                 return 1;
  58.         }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement