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

📄 sales.cpp

📁 C++上机课的习题答案
💻 CPP
字号:
// Exercise 3 (sales.cpp)
//用二维数组记录每个售货员(1-4)售出每种产品(1-5)的总价值,
//二维数组的第一行和第一列并没有使用. 
//然后统计每个售货员所售出的所有产品的总价值
//和每种产品被所有售货员售出的总价值,并保存在一维数组中。
//最后格式化输出列表。 
#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; //实际是4个售货员 
   const int PRODUCTS = 6;//实际是5件产品 

   /* Write the declaration for array sales here and initialize all elements to 0.0 */

   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;

   system("PAUSE");
   return 0;

} // end main


⌨️ 快捷键说明

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