Advertisement
pdaogu

Ex6.3.1

Oct 14th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define PRICE_NORMAL 7
  5. #define PRICE_WEEKEND 10
  6. #define DISCOUNT_CHILD 0.5
  7. #define DISCOUNT_OLD 0.3
  8.  
  9. typedef enum { SUN,
  10.            MON,
  11.            TUE,
  12.            WED,
  13.            THU,
  14.            FRI,
  15.            SAT } WeekDay;
  16. typedef enum { CHILD,
  17.             ADULT,
  18.             SENIOR,
  19.             INVALID = -1
  20. } Class;
  21.  
  22. void strupr1 (char* str) {
  23.     for (int i=0; str[i] != '\0'; ++i) {
  24.         if (str[i] >= 'a' && str[i] <= 'z') {
  25.             str[i] -= 32;
  26.         }
  27.     }
  28.     return;
  29. }
  30.  
  31. int main () {
  32.   WeekDay day;
  33.   int age1, age2, age3, n;
  34.   char movie1[30], movie2[30], movie3[30];
  35.   printf("What day is it today?\n(%d=SUN, %d=MON, %d=TUE, %d=WED, %d=THU, %d=FRI, %d=SAT): ",
  36.      SUN, MON, TUE, WED, THU, FRI, SAT);
  37.   scanf("%d", &day);
  38.   printf("How many people of you? ");
  39.   scanf("%d", &n);
  40.   if (n >= 1) {
  41.     printf("\n\nPlease enter the age of the 1st people: ");
  42.     scanf("%d", &age1);
  43.     age1 = age1 >= 0 ? (age1 >= 18 ? (age1 >= 65 ? SENIOR : ADULT) : CHILD) : -1;
  44.     if (age1 == INVALID)
  45.         printf("Your value you enterd is invalid.\n");
  46.     printf("What movie would you want: ");
  47.     while (getchar() != '\n');
  48.     scanf("%[^\n]", movie1);
  49.     strupr1(movie1);
  50.   }
  51.   if (n >= 2) {
  52.     printf("\nPlease enter the age of the 2nd people: ");
  53.     scanf("%d", &age2);
  54.     age2 = age2 >= 0 ? (age2 >= 18 ? (age2 >= 65 ? SENIOR : ADULT) : CHILD) : -1;
  55.     if (age2 == INVALID)
  56.         printf("Your value you enterd is invalid.\n");
  57.     printf("What movie would you want: ");
  58.     while (getchar() != '\n');
  59.     scanf("%[^\n]", movie2);
  60.     strupr1(movie2);
  61.   }
  62.   if (n>=3) {
  63.     printf("\nPlease enter the age of the 3rd people: ");
  64.     scanf("%d", &age3);
  65.     age3 = age3 >= 0 ? (age3 >= 18 ? (age3 >= 65 ? SENIOR : ADULT) : CHILD) : -1;
  66.     if (age3 == INVALID)
  67.         printf("Your value you enterd is invalid.\n");
  68.     printf("What movie would you want: ");
  69.     while (getchar() != '\n');
  70.     scanf("%[^\n]", movie3);
  71.     strupr1(movie3);
  72.   }
  73.  
  74.   printf("\n%d ticket(s) for you:\n\n", n);
  75.   if (n >= 1) {
  76.     if (age1 != INVALID) {
  77.         float price;
  78.         if (day==SUN || day==SAT)
  79.             price = PRICE_WEEKEND;
  80.         else
  81.             price = PRICE_NORMAL;
  82.         if (age1 == CHILD)
  83.             price *= (1 - DISCOUNT_CHILD);
  84.         else if (age1 == SENIOR)
  85.             price *= 1 - DISCOUNT_OLD;
  86.         printf("----------------------------------------\n");
  87.         printf("|%*s%s%*s|\n", 12, "", "TICKET & BILL", 13, "");
  88.         printf("|%*s|\n", 38, "");
  89.         printf("|--------------------------------------|\n");
  90.         printf("|%-*s%-*s|\n", 10, "MOVIE:", 28, movie1);
  91.         printf("|%*s|\n", 38, "");
  92.         printf("|%-*s%-*s|\n", 10, "Class: ", 28, age1 == CHILD ? "Child" : (age1 == ADULT ? "Adult" : "Senior Citizen"));
  93.         printf("|%-*s%-*s|\n", 10, "Date: ", 28, (day == SUN || day == SAT) ? "Weekend" : "Normal");
  94.         printf("|%-*s$%-*.2f|\n", 10, "Price: ", 27, price);
  95.         printf("|%*s|\n", 38, "");
  96.         printf("----------------------------------------\n");
  97.     } else
  98.         printf("Information of the 1st people is invalid.\n");
  99. }
  100.  
  101.  if (n >= 2) {
  102.     if (age2 != INVALID) {
  103.         float price;
  104.         if (day==SUN || day==SAT)
  105.             price = PRICE_WEEKEND;
  106.         else
  107.             price = PRICE_NORMAL;
  108.         if (age2 == CHILD)
  109.             price *= (1 - DISCOUNT_CHILD);
  110.         else if (age2 == SENIOR)
  111.             price *= 1 - DISCOUNT_OLD;
  112.         printf("\n----------------------------------------\n");
  113.         printf("|%*s%s%*s|\n", 12, "", "TICKET & BILL", 13, "");
  114.         printf("|%*s|\n", 38, "");
  115.         printf("|--------------------------------------|\n");
  116.         printf("|%-*s%-*s|\n", 10, "MOVIE:", 28, movie2);
  117.         printf("|%*s|\n", 38, "");
  118.         printf("|%-*s%-*s|\n", 10, "Class: ", 28, age2 == CHILD ? "Child" : (age2 == ADULT ? "Adult" : "Senior Citizen"));
  119.         printf("|%-*s%-*s|\n", 10, "Date: ", 28, (day == SUN || day == SAT) ? "Weekend" : "Normal");
  120.         printf("|%-*s$%-*.2f|\n", 10, "Price: ", 27, price);
  121.         printf("|%*s|\n", 38, "");
  122.         printf("----------------------------------------\n");
  123.     } else
  124.         printf("Information of the 2nd people is invalid.\n");
  125. }
  126.  
  127.  if (n >= 3) {
  128.     if (age3 != INVALID) {
  129.         float price;
  130.         if (day==SUN || day==SAT)
  131.             price = PRICE_WEEKEND;
  132.         else
  133.             price = PRICE_NORMAL;
  134.         if (age3 == CHILD)
  135.             price *= (1 - DISCOUNT_CHILD);
  136.         else if (age3 == SENIOR)
  137.             price *= 1 - DISCOUNT_OLD;
  138.         printf("\n----------------------------------------\n");
  139.         printf("|%*s%s%*s|\n", 12, "", "TICKET & BILL", 13, "");
  140.         printf("|%*s|\n", 38, "");
  141.         printf("|--------------------------------------|\n");
  142.         printf("|%-*s%-*s|\n", 10, "MOVIE:", 28, movie3);
  143.         printf("|%*s|\n", 38, "");
  144.         printf("|%-*s%-*s|\n", 10, "Class: ", 28, age3 == CHILD ? "Child" : (age3 == ADULT ? "Adult" : "Senior Citizen"));
  145.         printf("|%-*s%-*s|\n", 10, "Date: ", 28, (day == SUN || day == SAT) ? "Weekend" : "Normal");
  146.         printf("|%-*s$%-*.2f|\n", 10, "Price: ", 27, price);
  147.         printf("|%*s|\n", 38, "");
  148.         printf("----------------------------------------\n");
  149.     } else
  150.         printf("Information of the 3rd people is invalid.\n");
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement