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

📄 testbinarysearch.cpp

📁 小程序,VC++处学者用的,关于二分法查找的 经典的BINARYSEARCH算法..不错哦
💻 CPP
字号:
//************      main.cpp      *************

# include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>
#include <iomanip>

# include "BinarySearch.h"

int main()
{
	// we will created the interface of my program.
	char enter;
    cout << "       ***************************************************************" << "\n" << endl;
	cout << "                         BINARY_SEARCH SYSTEM                               " << "\n" << endl;
	cout << "       ***************************************************************" << "\n" << endl;
	cout << "                 Are you sure to begin to search ?(Y(y),N(n))                 " << endl;
	cin >> enter;
	
	while( enter != 'y' && enter != 'n' && enter != 'Y' && enter != 'N' )
	{
		cout << "Your enter is error.Please enter again!" << endl;

		cin >> enter;
	}
	
	if( enter == 'n' || enter == 'N' )
	{
		cout << "                            Thank you for using!                    " << "\n" << endl;
	}
	
	else if( enter == 'y' || enter == 'Y' )
	{
	  while ( enter == 'y' || enter == 'Y' )
	  {
		  int arraySize;                 //size of array b
	      cout << "Please input array b's size (integer between 2 and 9999) : ";
	      cin >> arraySize;
		  
	      while ( arraySize < 2 || arraySize > 9999 )           //judge if the size of array is larger than 1
		  {
		     cout << "Please input the size of a array AGAIN!!:arraysize =  (>=2)"; 
		     cin >> arraySize;
		  }
	      int *b = new int[arraySize];             //create array b
	      int Key;                      //value to locate in b
         
	      cout << "\n\nPlease input an array of " << arraySize << " integers in increasing order: " << endl;
	
	      //input an array
	      for ( int i = 0; i < arraySize; i++ )      
		     cin >> b[i];
    
	      Binarysearch binarysearch1( b, 0, 0, arraySize-1, arraySize ); 
	
	      //judge whether the array is in increasing
	      int flag = 0;
	      for ( int k = 0 ; k < arraySize-1; k++ )
	      if ( b[k] >= b[k+1] && flag == 0 )
		  {
		   flag = 1;
		   cout << "\n\nYou are not entring an array of integers in increasing!!" << endl;	
           cout << "The new array which is in increasing is: " << endl;
		   binarysearch1.sort(b,arraySize);
		   
		  }
	      cout << endl;
	

          //search for key in array b
	      cout << "\nPlease input a number you want to search: ";
          cin >>Key;

	      Binarysearch binarysearch( b, Key, 0, arraySize-1, arraySize ); 	       

	      
	      int result =binarysearch.bSearch ( Key, b ); // call the bSearch function

	      if ( result != -1 )                        //display results
		    cout << '\n' << Key << " is in the array of element " << result+1 << "\n" << endl;
	      else
		    cout << '\n' << Key << " is not in the array.\n" << endl;

	      delete[] b;
	
	
	      cout << "                     Go on to search? (Y(y),N(n))                 " << endl;
			
	      cin >> enter;
		
	      while( enter != 'y' && enter != 'n' && enter != 'Y' && enter != 'N' )
		  {
		   cout << "Your enter is error.Please enter again!" << endl;

		   cin >> enter;
		  }
	
	  }

		if( enter == 'n' || enter == 'N' )
		{
			cout << "                        Thank you for using!                 " << "\n" << endl;
		} 
	}
	return 0;

}

⌨️ 快捷键说明

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