1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// Sales.h
#ifndef SALES_H_
#define SALES_H_
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minumum values
void setSales(Sales & s);
// display all information in structure s
void showSales(const Sales & s);
}
#endif
// Program 9.4file1.cpp
#include<iostream>
#include "Sales.h"
using namespace SALES;
void setSales(Sales & s, const double ar[], int n)
{
using std::cout;
using std::cin;
double total = 0;
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
total += s.sales[i];
}
s.average = total/QUARTERS;
s.max = s.sales[0];
s.min= s.sales[0];
for (int i = 0; i < n; i++) //calculate max
{
if (s.sales[i] > s.max)
s.sales[i] = s.max;
else if (s.sales[i] < s.min)
s.sales[i] = s.min;
}
}
void setSales(Sales & s)
{
using std::cout;
using std::cin;
double total = 0;
cout << "Please enter the sales for the approrpiate quarter.\n";
// input the sales
for (int i = 0; i < QUARTERS; i++)
{
cout << "Please enter quarter " << i + 1 << "sales: ";
cin >> s.sales[i];
total += s.sales[i];
}
s.average = total/QUARTERS;
s.max = s.sales[0];
s.min= s.sales[0];
//calculate max and min
for (int i = 0; i < QUARTERS; i++)
{
if (s.sales[i] > s.max)
s.sales[i] = s.max;
else if (s.sales[i] < s.min)
s.sales[i] = s.min;
}
}
void showSales(const Sales & s)
{
using std::cout;
for (int i = 0; i < QUARTERS; i++)
cout << s.sales[i] << "\n";
cout << "The average is: " << s.average << "\n";
cout << "The max is: " << s.max << "\n";
cout << "The min is: " << s.min << "\n";
}
// Program 9.4file2.cpp
#include<iostream>
#include "Sales.h"
int main()
{
using namespace SALES;
using std::cout;
using std::cin;
Sales inventory[2];
double ar[3] = {5.0, 9.0, 6.0};
cout << "Welcome to the inventory tracking database.\n";
setSales(inventory[1], ar, 3);
cout << "Please enter last year's quarterly sales.\n";
setSales(inventory[2]);
cout << "Here is this year's sales: \n";
showSales(inventory[1]);
cout << "Here is the previous year's sales: \n";
showSales(inventory[2]);
return 0;
}
|