📄 sales.cpp
字号:
// Chapter 4 of C++ How to Program
// Exercise 3 (sales.cpp)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
using std::setw;
int main()
{
const int PEOPLE = 5;
const int PRODUCTS = 6;
/* Write the declaration for array sales here */
double value;
double totalSales;
double productSales[ PRODUCTS ] = { 0.0 };
int salesperson;
int product;
cout << "Enter the salesperson (1 - 4), "
<< "product number (1 - 5)\nand total sales."
<< "Enter -1 for the salesperson to end input.\n";
cin >> salesPerson;
// process sales
while ( salesPerson != -1 ) {
cin >> product >> value;
/* Write a statement that adds values to the
sales array */
cin >> salesPerson;
} // end while
// table header: describes output and prints
// column header(product numbers 1-5)
cout << "\nThe total sales for each sales person "
<< "are displayed\nat the end of each row,"
<< "and the total sales for each\nproduct "
<< "are displayed at the bottom of each column.\n"
<< setw( 10 ) << 1 << setw( 10 ) << 2
<< setw( 10 ) << 3 << setw( 10 ) << 4
<< setw( 10 ) << 5 << setw( 12 ) << "Total\n" << fixed
<< setprecision( 2 );
// nested loop structure: prints salesperson number
// followed by the amounts sold for each product
for ( int i = 1; /* Write condition here */ ; ++i ) {
totalSales = 0.0;
// print salesperson number
cout << i;
// inner loop: prints amounts sold for each product
for ( int j = 1; /* Write condition here */; ++j ) {
/* Write a statement that adds the current sales
element to totalSales */
// print sales for each salesperson for each product
cout << setw( 10 )
<< sales[ i ][ j ];
/* Write a statement that adds the current sales
element to productSales */
} // end for
// print the last column item (total sales of each
// product). The totalSales value is 9.99 under
// "Total" in the output box. After this value is
// printed, the next table line can be created
cout << setw( 10 ) << totalSales << '\n';
} // end for
// header for last row
cout << "\nTotal" << setw( 6 ) << productSales[ 1 ];
// prints last row which displays total sales
// for each product
for ( int j = 2; j < PRODUCTS; ++j )
cout << setw( 10 ) << productSales[ j ];
cout << endl;
return 0;
} // end main
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. 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 + -