All pastes #242366 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-11-07 04:22 UTC
rendered paste body
#pragma once


namespace reallyProject8 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListView^  lstNames;
	private: System::Windows::Forms::TextBox^  txtLog;
	private: System::Windows::Forms::TextBox^  txtTarget;
	private: System::Windows::Forms::Button^  btnSeqSearch;
	private: System::Windows::Forms::Button^  btnBinary;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	protected: 


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->lstNames = (gcnew System::Windows::Forms::ListView());
			this->txtLog = (gcnew System::Windows::Forms::TextBox());
			this->txtTarget = (gcnew System::Windows::Forms::TextBox());
			this->btnSeqSearch = (gcnew System::Windows::Forms::Button());
			this->btnBinary = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// lstNames
			// 
			this->lstNames->Location = System::Drawing::Point(24, 129);
			this->lstNames->Name = L"lstNames";
			this->lstNames->Size = System::Drawing::Size(121, 325);
			this->lstNames->TabIndex = 0;
			this->lstNames->UseCompatibleStateImageBehavior = false;
			this->lstNames->View = System::Windows::Forms::View::List;
			// 
			// txtLog
			// 
			this->txtLog->Location = System::Drawing::Point(175, 129);
			this->txtLog->Multiline = true;
			this->txtLog->Name = L"txtLog";
			this->txtLog->Size = System::Drawing::Size(205, 325);
			this->txtLog->TabIndex = 1;
			// 
			// txtTarget
			// 
			this->txtTarget->Location = System::Drawing::Point(141, 49);
			this->txtTarget->Name = L"txtTarget";
			this->txtTarget->Size = System::Drawing::Size(100, 20);
			this->txtTarget->TabIndex = 2;
			// 
			// btnSeqSearch
			// 
			this->btnSeqSearch->Location = System::Drawing::Point(265, 12);
			this->btnSeqSearch->Name = L"btnSeqSearch";
			this->btnSeqSearch->Size = System::Drawing::Size(75, 45);
			this->btnSeqSearch->TabIndex = 3;
			this->btnSeqSearch->Text = L"Sequential Search";
			this->btnSeqSearch->UseVisualStyleBackColor = true;
			this->btnSeqSearch->Click += gcnew System::EventHandler(this, &Form1::btnSeqSearch_Click);
			// 
			// btnBinary
			// 
			this->btnBinary->Location = System::Drawing::Point(265, 63);
			this->btnBinary->Name = L"btnBinary";
			this->btnBinary->Size = System::Drawing::Size(75, 45);
			this->btnBinary->TabIndex = 4;
			this->btnBinary->Text = L"Binary Search";
			this->btnBinary->UseVisualStyleBackColor = true;
			this->btnBinary->Click += gcnew System::EventHandler(this, &Form1::btnBinary_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(69, 12);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(138, 20);
			this->label1->TabIndex = 5;
			this->label1->Text = L"Search Program";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(21, 52);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(103, 13);
			this->label2->TabIndex = 6;
			this->label2->Text = L"Enter a target value:";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(21, 113);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(69, 13);
			this->label3->TabIndex = 7;
			this->label3->Text = L"List of names";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(172, 113);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(58, 13);
			this->label4->TabIndex = 8;
			this->label4->Text = L"Search log";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(392, 466);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->btnBinary);
			this->Controls->Add(this->btnSeqSearch);
			this->Controls->Add(this->txtTarget);
			this->Controls->Add(this->txtLog);
			this->Controls->Add(this->lstNames);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		private: String^ strTarget;
		private: String^ strLog;
		private: String^ strMidName;
		private: array<String^>^ strName;

	private: System::Void btnSeqSearch_Click(System::Object^  sender, System::EventArgs^  e) {
				 int counter = 0;
				 bool found = false;
				 int i = 0;
				 strTarget = txtTarget->Text;
				 strLog = "SEQUENTIAL SEARCH LOG " + Environment::NewLine;
				 while ((!found) && (i < strName->Length))
				 {
					 strLog += "Checking location " + i.ToString() + " (" + strName[i] + ")" + Environment::NewLine;
					 if (String::Compare(strTarget,strName[i])==0)
						 found = true;
					 i++;
				 }
				 if (found) strLog += "FOUND!\r\n";
				 else strLog += "NOT FOUND!\r\n";
				 txtLog->Text = strLog + i.ToString() + " comparisons were made.";
			 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 strTarget = gcnew String("");
			 strLog = gcnew String("");
			 strName = gcnew array<String^>(20);
			 strName->SetValue("Z1",0);
			 strName->SetValue("ABC",1);
			 strName->SetValue("Mark I",2);
			 strName->SetValue("ENIAC",3);
			 strName->SetValue("UNIVAC",4);
			 strName->SetValue("Analytical Engine",5);
			 strName->SetValue("IBM 650",6);
			 strName->SetValue("Cray",7);
			 strName->SetValue("Apple II",8);
			 strName->SetValue("IBM PC",9);
			 strName->SetValue("Macintosh",10);
			 strName->SetValue("HAL 9000",11);
			 strName->SetValue("MITS Altair",12);
			 strName->SetValue("GRAPE",13);
			 strName->SetValue("Colossus",14);
			 strName->SetValue("Xerox Alto",15);
			 strName->SetValue("EDSAC",16);
			 strName->SetValue("EDVAC",17);
			 strName->SetValue("PDP-11",18);
			 strName->SetValue("Sun Solaris",19);
			 strName->Sort(strName);
			 for (int i = 0; i < 20; i++)
				 lstNames->Items->Add("(" + i.ToString() + ")    " + strName[i]);
		 }
private: System::Void btnBinary_Click(System::Object^  sender, System::EventArgs^  e) {
			 int low = 0;
			 strTarget = txtTarget->Text;
			 int high = Convert::ToInt32(strName->Length);
			 int mid = Convert::ToInt32((low+high)/2);
			 bool found = false;
			 int i = 0;
			 strLog ="BINARY SEARCH LOG " + Environment::NewLine;
			 MessageBox::Show("Mid: " + mid.ToString() + " Low: " + low.ToString() + " High: " + high.ToString() + " strName: " + strName[mid]->ToString() + " strTarget " + strTarget);
			 while (!(found) && (low <= high))
			 {
//				 strMidName = strName[mid];
				 strMidName = strName[1];
				 strLog += "Checking location " + mid.ToString() + " (" + strMidName + ") against " + strTarget + " (Low: " + low.ToString() + " & High: " + high.ToString() + ")" + Environment::NewLine;
				 if (String::Compare(strTarget,strMidName)==0)
					 found = true;
				 else 
				 {
					 if(String::Compare(strTarget,strName[mid])< 0)
					 {
						 high = mid-1;
						 mid = (low + high)/2;
					 }
					 else if(String::Compare(strTarget,strName[mid])> 0)
					 {
						 low = mid+1;
						 mid = (low + high)/2;
					 }
				 }
				 i++;
			 }
			 if (found) strLog += "FOUND!\r\n";
			 else strLog += "NOT FOUND!\r\n";
			 txtLog->Text += strLog + i.ToString() + " comparisons were made.";
		 }
};
}