⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 payroll.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Tutorial 17: Payroll.cpp
// Stores employee information and calculates payroll for
// employees of various types.
#include <iostream> // required to perform C++-style stream I/O
#include <iomanip>  // required for parameterized stream manipulators

using namespace std; // for accessing C++ Standard Library members

#include "Employee.h"    // Employee base class definition
#include "Salaried.h"    // SalariedEmployee class definition

const int ARRAY_SIZE = 10;

// function prototypes
int createEmployee( SalariedEmployee * [], int );
void displayEmployees(  SalariedEmployee * [], int );

// function main begins program execution
int main()
{
   int count = 0;     // stores the number of employees entered
   int selection = 0; // stores the user's menu selection

   // format output as currency
   cout << fixed << setprecision( 2 );

   // create an array of Employees
   SalariedEmployee *employees[ ARRAY_SIZE ];

   while ( selection != 3 )
   {
      cout << "\nSelect one of the following options" << endl;
      cout << "1 - Enter new employee information" << endl;
      cout << "2 - View payroll" << endl;
      cout << "3 - Exit the application" << endl;
      cout << "Enter selection: ";
      cin >> selection;

      switch ( selection )
      {
         // enter employee information
         case 1:
            // ensure that there is space in the array
            if ( count < ARRAY_SIZE )
            {
               count += createEmployee( employees, count );
            } // end if
            else
            {
               cout << "Error: You cannot enter any more employees."
                    << endl;
            } // end else

            break;

          // view payroll
         case 2:
            displayEmployees( employees, count );

            break;

         // exit
         case 3:

            break;

         default:
            cout << "Error: Enter a valid menu selection";

      } // end switch

   } // end while

   // delete each employee
   for ( int i = 0; i < count; i++ )
   {
      delete employees[ i ];
   } // end for

   return 0; // indicate that program ended successfully

} // end function main

int createEmployee( SalariedEmployee *employees[], int index )
{
   // define variables
   string firstName; // stores the employee's first name
   string lastName;  // stores the employee's last name
   string SSN;       // stores the employee's social security number
   int selection;    // stores the employee type selection

   cin.ignore(); // remove newline

   // prompt user for and store generic Employee information
   cout << "\nEnter employee's first name: ";
   getline( cin, firstName ); // get first name

   cout << "Enter employee's last name: ";
   getline( cin, lastName ); // get last name

   cout << "Enter employee's social security number: ";
   getline ( cin, SSN ); // get SSN

   // display a menu of employee types
   cout << "\nSelect the employee type" << endl;
   cout << "1 - Salaried employee" << endl;
   cout << "2 - Commission employee" << endl;
   cout << "3 - Hourly employee" << endl;
   cout << "Enter selection: ";
   cin >> selection;

   switch ( selection )
   {
      case 1:
         double salary; // stores the salary

         // prompt user for and store the salary
         cout << "\nEnter salary: ";
         cin >> salary;

         // store pointer to a SalariedEmployee object
         employees[ index ] = new SalariedEmployee( firstName,
            lastName, SSN, salary );

         break;

      case 2:
         double sales;  // stores the total sales
         double rate; // stores the commission percentage

         // prompt user for and store the total sales and rate
         cout << "\nEnter sales: ";
         cin >> sales; // get total sales

         cout << "Enter commission rate (%): ";
         cin >> rate; // get rate

         break;

      case 3:
         double hourlyWage;  // stores the hourly wage
         double hours;       // stores the hours worked

         // prompt user for and store the hourly wage and total hours
         cout << "\nEnter hourly wage: ";
         cin >> hourlyWage; // get hourly wage

         cout << "Enter hours worked: ";
         cin >> hours; // get hours

         break;

      default:
         cout << "Error: You must select a valid employee type."
              << endl;

         return 0;

   } // end switch

   return 1;

} // end function createEmployee

void displayEmployees( SalariedEmployee *employees[], int count )
{
   double total = 0.0; // stores the total payroll

   // ensure that employees have been entered before printing
   if ( count > 0 )
   {
      // display each employee
      for ( int i = 0; i < count; i++ )
      {
         // display employee information
         employees[ i ]->print();

         // display salary
         cout << "Earned: $" << employees[ i ]->getWeeklySalary()
              << endl;

         // add salary to taol
         total += employees[ i ]->getWeeklySalary();
      } // end for

      cout << "\nTotal payroll: $" << total << endl;

   } // end if
   else
   {
      cout << "Error: You must enter at least one employee first."
           << endl;
   } // end else

} // end function displayEmployees

/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -