Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rebecca Wong, pikachu1997@gmail.com
- //Lab 4
- //November 2, 2015
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cstdlib>
- #include <cctype>
- #include <cmath>
- #define PROFIT 0.80
- using namespace std;
- //Function prototypes (declaration)
- int calcCopies(int, int);
- double calcProfit(int, double, int, char, char);
- void output(string, int, double);
- void signature()
- {
- string email = "pikachu1997@gmail.com",
- lab = "Lab 4",
- student = "Rebecca Wong";
- cout << endl << student << endl;
- cout << lab << endl;
- cout << email << endl;
- }
- int main()
- {
- //Declare variables
- string ISBN;
- double unitPrice, profit;
- int expectedClass, copies, finalCopies;
- char text, text2;
- char requiredText = 'R', suggestedText = 'S';
- char newText = 'N', previousText = 'O';
- //Prompt user for input
- cout << "Enter the book's ten digit ISBN number: ";
- cin >> ISBN;
- cout << "Enter the price per book copy: ";
- cin >> unitPrice;
- cout << "Enter the expected class enrollment: ";
- cin >> expectedClass;
- cout << endl << "Is the text required or suggested? "
- << endl << "Enter ""R"" for required text, or ""S"" for suggested text: ";
- cin >> text;
- text = toupper(text);
- cout << endl << "Is the text new or has it been used in previous quarters? "
- << endl << "Enter ""N"" for new text, or ""O"" for a previously used text: ";
- cin >> text2;
- text2 = toupper(text2);
- //Calls function that calculates the number of copies needed
- finalCopies = calcCopies(copies, expectedClass);
- //Calls function that calculates the profit
- profit = calcProfit(copies, unitPrice, expectedClass, text, text2);
- //Calls function for output
- output(ISBN, finalCopies, profit);
- signature();
- system("pause;");
- return 0;
- }
- int calcCopies(int copies, int enrollment)
- {
- /*
- Pre: copies - number of books to order
- enrollment - number of students that are going to buy a book
- Purpose : compute the number of copies needed
- Post : return finalCopies value
- */
- int finalCopies = copies * enrollment;
- return finalCopies;
- }
- double calcProfit(int copies, double unitPrice, int classEnrollment, char text, char text2)
- {
- /*
- Pre : copies - number of copies
- unitPrice - price of a book for the student
- classEnrollment - expected number of students for that class
- text - required or suggested book
- text 2 - new or old book
- Purpose : calculate the profit the bookstore earns
- Post : return porfit value
- */
- double pricePerBook = .20 * unitPrice;
- double profit;
- if (text == 'R' && text2 == 'N')
- {
- profit = copies * pricePerBook * classEnrollment * 0.90;
- }
- else if (text == 'R' && text2 == 'N')
- {
- profit = copies * pricePerBook * classEnrollment * 0.65;
- }
- else if (text == 'S' && text2 == 'N')
- {
- profit = copies * pricePerBook * classEnrollment * 0.40;
- }
- else if (text == 'S' && text2 == 'O')
- {
- profit = copies * pricePerBook * classEnrollment * 0.20;
- }
- return profit;
- }
- void output(string ISBN, int finalCopies, double profit)
- {
- /*
- Pre : ISBN - ISBN number of the book
- finalCopies - number of books to be bought
- profit - profit the bookstore gains
- Purpose : print results our for bookstore's use
- Post : output
- */
- cout << setprecision(2) << fixed;
- cout << "ISBN" << ISBN << endl;
- cout << "Copies needed: " << finalCopies << endl;
- cout << "Profit: " << "$" << profit << endl;
- return;
- }
- /*
- CALCULATE
- Calculate the number of copies needed and the profit if the store pays 80% of list (use
- global define for this). For the number of books to order, round up to the nearest whole
- book.
- OUTPUT A sample run might look like this:
- Enter book number: 0755798652
- Enter price per copy: 34.98
- Enter expected class enrollment: 35
- Enter 'R' if required or 'S' if suggested: r
- Enter 'N' if new or 'O' if not a new text: O
- ISBN: 0755798652
- Copies Needed: 23
- Profit: $ 160.91
- TEST DATA
- 0755798652,34.98,35,R,O
- 3453456784,23.95,100,R,N
- 5677655673,54.50,40,R,O
- 2462462464,5.95,40,S,O
- 8953647888, 65.99, 35, N, Y
- THEME ISSUES
- Functions (pass by value), IF Control Structures, character type, rounding
- Checkpoints
- 2) Minimum of three (3) comments in each function (Purpose: Pre: Post:)
- 4) To do this use if() and assign uppercase value or you may use
- toupper() function (see 10.2 on page 545 of text). Only an error message should
- be output and nothing more if data is erroneous. Use exit() function as explained
- in 6.15 (p. 360).
- 5) A function must be used to calculate the number of books to be ordered. Round to the nearest whole number.
- Hint: static_cast<int> (x + .5)
- 8) Output must be formatted using dollar sign and 2 decimal place as shown on page 1.
- Test Data: Execute the program five times and each time copy and paste the
- output to the bottom of the program.
- Set 1 –0755798652, 34.98, 31, R, O
- Set 2 – 3453456784, 23.95, 100, r, N
- Set 3 – 5677655673, 54.50, 40, R, O
- Set 4 – 2462462464, 5.95, 40,s,O
- Set 5 – 8953647888, 65.99, 35, N, Y
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement