Advertisement
Lisaveta777

Prata chapter 7 exs 8

Aug 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. //prata exc 7 chapter 7
  2. //working, but i'm not happy about names vars and consts
  3. //have to read a book about properly naming!
  4. //another matter - which is better style -introducing after_tax or not?
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #define O_RATE 1.5 //overtime rate
  8. #define TAX1 0.15
  9. #define TAX2 0.2
  10. #define TAX3 0.25
  11. #define BREAK1 300.0
  12. #define BREAK2 450.0
  13. #define WEEK 40.0
  14.  
  15. int main()
  16. {
  17.     float hours,before_tax, after_tax, tax,h_rate;
  18.     int t, flag = 1;
  19.     printf("How many hours have you worked this week?\n");
  20.     scanf("%f",&hours);
  21.     while(flag)
  22.     {
  23.  
  24.         printf("Choose your hourly tariff: 1. 8.50/h 2. 9.50/h "
  25.             "3. 10.00/h 4. 11.00/h 5. 22.00/h\n");
  26.         scanf("%d",&t);
  27.         flag = 0;
  28.         switch(t)
  29.         {
  30.             //flag=0;
  31.         case 1:
  32.             h_rate = 8.50;
  33.             break;
  34.         case 2:
  35.             h_rate = 9.50;
  36.             break;
  37.         case 3:
  38.             h_rate = 10.00;
  39.             break;
  40.         case 4:
  41.             h_rate = 11.00;
  42.             break;
  43.         case 5:
  44.             h_rate = 22.00;
  45.             break;
  46.         default:
  47.             flag = 1;
  48.         }
  49.     }
  50.     printf("you chosen hourly rate of %f\n",h_rate);
  51.  
  52.     //calculating salary before tax
  53.     if(hours<=WEEK)
  54.         before_tax = hours * h_rate;
  55.     else
  56.         before_tax = WEEK*h_rate +(hours-WEEK)*O_RATE*h_rate;
  57.  
  58.     //calculating taxes
  59.     if(before_tax<BREAK1)
  60.     {
  61.         tax = before_tax*TAX1;
  62.  
  63.     }
  64.     else if(before_tax<BREAK2)
  65.     {
  66.         tax = BREAK1*TAX1+ (before_tax-BREAK1)*TAX2;
  67.  
  68.     }
  69.     else
  70.         tax = BREAK1*TAX1 + (BREAK2-BREAK1)*TAX2+ (before_tax-BREAK2)*TAX3;
  71.  
  72.      after_tax = before_tax - tax;
  73.  
  74.      printf("before %f, after %f, tax %f",before_tax,after_tax,tax);
  75.  
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement