CPP 9
Untitled Guest on 22nd September 2022 11:35:00 PM
  1. // Wyatt Vandenburg
  2. // Paintingco.cpp
  3. // the following code takes in the the size of a painting job and calculates the size of job, how many paint cans it
  4. // will take and the cost, next the program takes an inputed numeric date and calculates the day of the week, and
  5. // the calculates the correct month while considering if there is a leap year or not
  6. // Xcode
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <iomanip>
  11. using namespace std;
  12.  
  13. void displayTitle();
  14. void getData(int &length, int &width);
  15. char convert12Format(int &hour);
  16. int calcSqMeter(int length, int width);
  17. int calcTotalCans(int area);
  18. double calcCost(int cans);
  19. void displayResults(int area, int cans, double cost);
  20. bool isLeapYear(int year);
  21. int getYearValue(int year);
  22. int getMonthNumber(string month);
  23. int getMonthVa0lue(int m, int year);
  24. int dayOfWeek(int day, int month, int year);
  25. void displayMsg(int cans);
  26.  
  27. int main()
  28. {
  29.     int length, width, hour, minute, day, month, year;
  30.     string weekDay[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  31.     string monthsInYear[] = {"January", "Feburary", "March","April", "May", "June", "July", "August", "September", "November", "Decemeber"};
  32. //
  33.     displayTitle();
  34.     getData(length, width);
  35.        
  36.     while (length != 0) {
  37.         cout << "  Enter the transaction time in 24-hour notation: " <<endl;
  38.         cout<<"  Hour: ";
  39.         cin >> hour;
  40.         cout<<"  Minute: ";
  41.         cin >> minute;
  42.         cout << "  Enter the transaction date (dd mm yyyy): ";
  43.         cin >> day >>  month >> year;
  44.  
  45.         int area = calcSqMeter(length, width);
  46.         int cans = calcTotalCans(area);
  47.         double cost = calcCost(cans);
  48.         displayResults(area, cans, cost);
  49.        
  50.         int idx = dayOfWeek(day, month, year);
  51.    
  52.         char ampm = convert12Format(hour);
  53.         cout << "\n\tDate/Time: " << weekDay[idx] <<", " << monthsInYear[month -1] << " " << day << ", " << year
  54.              << " " << hour << ":" << minute;
  55.         if(ampm == 'P')
  56.             cout<<"PM" << endl;
  57.         else
  58.             cout <<"AM"<<endl;
  59.         displayMsg(cans);
  60.  
  61.         cout <<"\tThank you for your purchase.\n "<<endl;
  62.         cout <<"========================================================="<<endl;
  63.         getData(length, width);
  64.     }
  65. }
  66.  
  67.  
  68. void displayTitle()
  69. {
  70.     cout<<"\t\tRainbow Printing Company Paint Costing"<<endl;
  71.     cout<<"\t-----------------------------------------------"<<endl;
  72.  
  73. }
  74. // uses call by reference
  75. void getData(int &length, int &width)
  76. {
  77.     cout<<"\n  Enter the length in meter (0 to stop).....";
  78.     cin >> length;
  79.     if(length == 0)
  80.         return;
  81.     cout<<"  Enter the width in meter....................";
  82.     cin >> width;
  83. }
  84.  
  85. // this method converts  24 hour period into a 12 hour also is using call by reference
  86. char convert12Format(int &hour)
  87. {
  88.     if(hour > 12)
  89.     {
  90.         hour = hour - 12;
  91.         return 'P';
  92.     }
  93.     return 'A';
  94. }
  95.  
  96. //function receives the values for length and width, and
  97. int calcSqMeter(int length, int width)
  98. {
  99.     int height = 3;
  100.     return length * width + 2 * height * (length + width);
  101. }
  102.  
  103. //function calculates and returns the # of cans
  104. int calcTotalCans(int area)
  105. {
  106.     int coverArea = 20;
  107.     int cans =  ceil((double)area/coverArea); // ceil will round the value up
  108.     return cans;
  109. }
  110.  
  111. //function calculates and returns the total cost
  112. double calcCost(int cans)
  113. {
  114.     double price = 15.50;
  115.     return cans * price;
  116. }
  117. // method displays numeric values and makes sure that the cost is set to two decimal places
  118. void displayResults(int area, int cans, double cost)
  119. {
  120.     cout << "\n--------------------------------------------------------------------------"<<endl;
  121.     cout << "\tArea\t\t=\t\t" << area << " Square meters"<<endl;
  122.     cout << "\tCans\t\t=\t\t" << cans << endl;
  123.     cout<<fixed <<setprecision(2);
  124.     cout << "\tCost\t\t=\t\t" << cost << endl;
  125. }
  126.  
  127. // the year must be divisble by 400 OR be divisible by 4 AND is NOT divisible by 100
  128. bool isLeapYear(int year)
  129. {
  130.    
  131.     if(!(year%4 && year%100)||year%400)
  132.         return false; // if the above conditiion is met it is true otherwise the statement is false, and the year is not a leap year.
  133. else
  134.     return true;
  135. }
  136.  
  137. //function computes a value based on the years since the beginning of the century
  138. int getYearValue(int year)
  139. {
  140.     int remainder;
  141.     remainder = year%100;
  142.     int num;
  143.     num= remainder / 4;
  144.    
  145.     return remainder + num;
  146. }
  147.  
  148. //function returns a month number based on a 3 charater month name
  149.  int getMonthNumber(int m)
  150. {
  151.      switch(m)
  152.         {
  153.          case 1:
  154.                 cout << ("January\n");
  155.                 break;
  156.          case 2:
  157.                 cout << ("February\n");
  158.                 break;
  159.          case 3:
  160.                 cout << ("March\n");
  161.                 break;
  162.          case 4:
  163.                 cout << ("April\n");
  164.                 break;
  165.          case 5:
  166.                 cout << ("May\n");
  167.                 break;
  168.          case 6:
  169.                 cout << ("June\n");
  170.                 break;
  171.          case 7:
  172.                 cout << ("July\n");
  173.                 break;
  174.          case 8:
  175.                 cout << ("August\n");
  176.                 break;
  177.          case 9:
  178.                 cout << ("September\n");
  179.                 break;
  180.          case 10:
  181.                 cout << ("October\n");
  182.                 break;
  183.          case 11:
  184.                 cout << ("November\n");
  185.                 break;
  186.          case 12:
  187.                 cout << ("December\n");
  188.                 break;
  189.          default:
  190.                 cout << ("invalid Month number. \nPlease try again ....\n");
  191.                 break;
  192.         }return m;}
  193. //function uses the values in the textbook and envokes the isLeapYear function
  194. int getMonthVa0lue(int month, int year)
  195. {
  196.    
  197.     const int modifier[] = {
  198.      0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5
  199.     };
  200.     if (month > 12 || month < 1)
  201.      return -1;
  202.     else if (month > 2 || !isLeapYear(year))
  203.      return modifier[month - 1];
  204.     else
  205.      return (modifier[month - 1] + 6) % 7;
  206.     }
  207.  
  208.  
  209. //function returns the day of the week of the specified date as an int (Sunday = 0, Monday = 1, etc.)
  210. int dayOfWeek(int d, int m, int y)
  211. {
  212.     static int t[] = { 0, 3, 2, 5, 0, 3,
  213.                            5, 1, 4, 6, 2, 4 };
  214.         y -= m < 3;
  215.         return ( y + y / 4 - y / 100 +
  216.                  y / 400 + t[m - 1] + d) % 7;
  217. }
  218.  
  219. // this fucntion will give the customer a free gift depending on the amount of cans they purchcase
  220. void displayMsg(int cans)
  221. {
  222.     if(cans>1 && cans<4)
  223.         cout<<"Gift: free paint brush\n "<<endl;
  224.     else if(cans < 8)
  225.         cout<<"Gift: Free paint tray\n"<<endl;
  226.     else
  227.         cout<<"Gift: $10 gift card\n"<<endl;
  228. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.