본문 바로가기

Software/C/C++

[C++]Workshop

.
1.Design and code three global functions named

    enter()
    simplify()
    display()

Your enter() function accepts a fraction from the user.  Your simplify() function simplifies a fraction, if possible.  Your display() function displays a fraction. 

Your global functions use a Fraction type.  A Fraction type holds the numerator and denominator of a fraction as separate data members. 

The main program that uses your three functions contains the following code:

 // Workshop 2 - Group X

 // w2.cpp

 // OOP244

 // Jan 24 2010


 #include 

 using namespace std;

 #include "Fraction.h"


 int main() {

     struct Fraction fraction;


     cout << "Fraction Simplifier" << endl;

     cout << "===================" << endl;


     enter(&fraction);

     simplify(&fraction);

     display(fraction);


     return 0;

 }

The output from the complete application looks something like

 Fraction Simplifier

 ===================

 Numerator:   4

 Denominator: 16


 1 / 4

Your enter() function receives the address of an instance of a Fraction type in a pointer, accepts from the user the numerator and denominator of a fraction, and stores those values in the fraction at the address held in the pointer parameter. 

Your simplify() function receives the address of an instance of a Fraction type and reduces the numerator and denominator of that instance, if possible. 

Your display() function receives a copy of an instance of a Fraction type and displays the numerator and denominator of that instance on standard output. 

Place your function prototypes and your declaration of the Fraction type in a header file named Fraction.h.  Place your function definitions in an implementation file named Fraction.cpp. 

Include negative as well as zero values in your test cases.

2.

Design and code two global function named:

    enter()
    total()

Your enter() function accepts an array of sales records from the user and your total() function displays the pretax, gst, pst, and after-tax totals for the records in that array. 

Your two functions use an array of instances of a Sale type.  A Sale type holds the quantity sold, the unit price and the taxable status. 

The main function that uses your two functions contains the following code:

 // Workshop 2 - Group Y

 // w2.cpp

 // OOP244

 // Jan 24 2010


 #include 

 using namespace std;

 #include "Sale.h"

 const int MAX_SALES = 10;


 int main() {

 	int noSales = MAX_SALES;

     struct Sale sale[MAX_SALES];


     cout << "Sale Records " << endl;

     cout << "=============" << endl;


     enter(sale, &noSales);

     total(sale, noSales);


     return 0;

 }

The output from the complete application looks something like

 Sale Records

 =============

 Quantity :  2

 Unit Price: 1.99

 Taxable:    y

 Quantity :  1

 Unit Price: 2.99

 Taxable:    n

 Quantity :  5

 Unit Price: 1.00

 Taxable:    n

 Quantity:   0


 Subtotal    11.97

 GST (7%)     0.28

 PST (8%)     0.32

 Total       12.57

Your enter() function receives the address of an instance of a Sale type in a pointer and the address of an int that holds the number of sales records.  Initially the variable pointed to holds the maximum number of records that the user may enter.  Upon exit, the variable pointed to holds the number of records actually entered.  Your function accepts the data values for each sales record and stores that data in an element of an array of Sale type, one record at a time.  Your function stops accepting sales record input once the user has entered a 0 value for the quantity sold. 

Your total() function receives the address of an instance of a Sale type in a pointer and the number of records actually accepted in an int.  Your function calculates the pretax, gst, pst and after-tax totals from the sales records and displays these results on standard output. 

Place your function prototypes and your declaration of the Sale type in a header file named Sale.h.  Place your function definitions in an implementation file named Sale.cpp.