Advertisement
Lisaveta777

Prata chapter 7 exs 8 plus function

Aug 9th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 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 koefficient
  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. float your_rate();
  16.  
  17. int main()
  18. {
  19.     float hours,before_tax, after_tax, tax,h_rate;
  20.     int t, flag = 1;
  21.     printf("How many hours have you worked this week?\n");
  22.     scanf("%f",&hours);
  23.     h_rate = your_rate();
  24.  
  25.     printf("you chosen hourly rate of %f\n",h_rate);
  26.  
  27.     //calculating salary before tax
  28.     if(hours<=WEEK)
  29.         before_tax = hours * h_rate;
  30.     else
  31.         before_tax = WEEK*h_rate +(hours-WEEK)*O_RATE*h_rate;
  32.  
  33.     //calculating taxes
  34.     if(before_tax<BREAK1)
  35.     {
  36.         tax = before_tax*TAX1;
  37.  
  38.     }
  39.     else if(before_tax<BREAK2)
  40.     {
  41.         tax = BREAK1*TAX1+ (before_tax-BREAK1)*TAX2;
  42.  
  43.     }
  44.     else
  45.         tax = BREAK1*TAX1 + (BREAK2-BREAK1)*TAX2+ (before_tax-BREAK2)*TAX3;
  46.  
  47.      after_tax = before_tax - tax;
  48.  
  49.      printf("before %f, after %f, tax %f",before_tax,after_tax,tax);
  50.  
  51.  
  52.     return 0;
  53. }
  54. float your_rate()
  55. {
  56.     int t,flag = 1;
  57.     float h_rate;
  58.     while(flag)
  59.     {
  60.         printf("Choose your hourly tariff: 1. 8.50/h 2. 9.50/h "
  61.             "3. 10.00/h 4. 11.00/h 5. 22.00/h\n");
  62.         scanf("%d",&t);
  63.         flag = 0;
  64.         switch(t)
  65.         {
  66.         case 1:
  67.             h_rate = 8.50; break;
  68.         case 2:
  69.             h_rate = 9.50; break;
  70.         case 3:
  71.             h_rate = 10.00; break;
  72.         case 4:
  73.             h_rate = 11.00; break;
  74.         case 5:
  75.             h_rate = 22.00; break;
  76.         default:
  77.             flag = 1;
  78.         }
  79.     }
  80.     return h_rate;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement