salesp.cpp
来自「经典vc教程的例子程序」· C++ 代码 · 共 56 行
CPP
56 行
// Fig. 6.7: salesp.cpp
// Member functions for class SalesPerson
#include <iostream.h>
#include <iomanip.h>
#include "salesp.h"
// Constructor function initializes array
SalesPerson::SalesPerson()
{
for ( int i = 0; i < 12; i++ )
sales[ i ] = 0.0;
}
// Function to get 12 sales figures from the user
// at the keyboard
void SalesPerson::getSalesFromUser()
{
double salesFigure;
for ( int i = 0; i < 12; i++ ) {
cout << "Enter sales amount for month "
<< i + 1 << ": ";
cin >> salesFigure;
setSales( i, salesFigure );
}
}
// Function to set one of the 12 monthly sales figures.
// Note that the month value must be from 0 to 11.
void SalesPerson::setSales( int month, double amount )
{
if ( month >= 0 && month < 12 && amount > 0 )
sales[ month ] = amount;
else
cout << "Invalid month or sales figure" << endl;
}
// Print the total annual sales
void SalesPerson::printAnnualSales()
{
cout << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< "\nThe total annual sales are: $"
<< totalAnnualSales() << endl;
}
// Private utility function to total annual sales
double SalesPerson::totalAnnualSales()
{
double total = 0.0;
for ( int i = 0; i < 12; i++ )
total += sales[ i ];
return total;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?