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

📄 areacalculator.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Exercise 17.12: AreaCalculator.cpp
// Calculates the area for various shapes
#include <iostream> // required to perform C++-style stream I/O
#include <iomanip>  // required for parameterized stream manipulators

#include "TwoDimensionalShape.h" // TwoDimensional Shape base class
#include "Triangle.h"    // Triangle class definition
#include "Rectangle.h"   // Rectangle class definition

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

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

   // format output with two digits of precision
   cout << fixed << setprecision( 2 );

   // create a pointer to a TwoDimensionalShape
   TwoDimensionalShape *shape;

   while ( selection != 3 )
   {
      cout << "\nSelect the shape you would like to enter:" << endl;
      cout << "1 - Triangle" << endl;
      cout << "2 - Rectangle" << endl;
      cout << "3 - Exit the application" << endl;
      cout << "Enter selection: ";
      cin >> selection;

      switch ( selection )
      {
         // enter triangle information
         case 1:
            // define variables for a triangle
            double side1; // stores the length of side 1
            double side2; // stores the length of side 2
            double side3; // stores the length of side 3

            // prompt user for and store lengths of triangle sides
            cout << "\nEnter the length of side 1: ";
            cin >> side1; // get side1

            cout << "Enter the length of side 2: ";
            cin >> side2; // get side2

            cout << "Enter the length of side 3: ";
            cin >> side3; // get side3

            shape = new Triangle( side1, side2, side3 );
            break;

         // enter triangle information
         case 2:
            // define variables for a rectangle
            double length; // stores the length
            double width; // stores the width

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

            cout << "Enter the length of width: ";
            cin >> width; // get width

            shape = new Rectangle( width, length );

            break;

         // exit
         case 3:
            cout << "\nGood-bye!" << endl;
            return 0; // indicate that program ended successfully

         default:
            cout << "Error: Enter a valid menu selection\n";
			shape = 0;

      } // end switch

   } // end while

   return 0; // indicate that program ended successfully

} // end function main

/**************************************************************************
 * (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 + -