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

📄 universalsorter.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Exercise 21.15: UniversalSorter.cpp
// This application sorts generic arrays that the user inputs.
#include <iostream> // required to perform C++ stream I/O
#include <string> // required to perform string manipulations
#include <algorithm> // required to use the sort function

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

// function main begins program execution
int main()
{
   // define variables
   int type = 0; // determines which data type the user will input
   int size = 0; // The number of values the user will input

   cout << endl; // insert newline for readability

   // prompt for and input type
   cout << "Select a data type:" << endl;
   cout << "1-char" << endl;
   cout << "2-int" << endl;
   cout << "3-double" << endl;
   cout << "Enter selection: ";
   cin >> type;

   // repeat until user enters valid input
   while ( type < 1 || type > 3 )
   {
      // display error message
      cout << "\nError: Enter a number from 1 to 4" << endl;

      // prompt for and input type
      cout << "Select a data type:" << endl;
      cout << "1-char" << endl;
      cout << "2-int" << endl;
      cout << "3-double" << endl;
      cout << "Enter selection: ";
      cin >> type;
   } // end while

   cout << endl; // insert newline for readability

   // prompt for and input number of values
   cout << "Enter number of items to sort: ";
   cin >> size;

   // repeat until the user enters a valid size
   while ( size <= 0 )
   {
      // display error message
      cout << "\nError: Enter a positive number" << endl;

      // prompt for and input number of values
      cout << "Enter number of items to sort: ";
      cin >> size;
   } // end while

   cout << endl; // insert newline for readability

   if ( type == 1 ) // sort an array of chars
   {   
      // dynamically allocate memory for the arrayToSort array
      char *charArray = new char[ size ];

      for ( int count = 0; count < size; count++ )
      {
         // prompt user for and input char values
         cout << "Enter char #" << count + 1 << ": ";
         cin >> charArray[ count ];
      } // end for

      // display header
      cout << endl << "Sorted array of chars:" << endl;

      // sort and display charArray

   } // end if
   else if ( type == 2 ) // sort an array of ints
   {      
      // dynamically allocate memory for the arrayToSort array
      int *intArray = new int[ size ];

      for ( int count = 0; count < size; count++ )
      {
         // prompt user for and input int values
         cout << "Enter int #" << count + 1 << ": ";
         cin >> intArray[ count ];
      } // end for

      // display header
      cout << endl << "Sorted array of ints:" << endl;

      // sort and display intArray

   } // end else if
   else if ( type == 3 ) // sort an array of doubles
   {
      // dynamically allocate memory for the arrayToSort array
      double *doubleArray = new double[ size ];

      for ( int count = 0; count < size; count++ )
      {
         // prompt user for and input floating-point values
         cout << "Enter double #" << count + 1 << ": ";
         cin >> doubleArray[ count ];
      } // end for

      // display header
      cout << endl << "Sorted array of doubles:" << endl;

      // sort and display doubleArray

   } // end else if

   cout << endl; // insert newline for readability

   return 0; // indicates successful termination

} // 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 + -