C++ da Dizide Arama Yapmak
Merhaba arkadaşlar bu makalemizde C++ da dizi tanımlayacağız. Daha sonra diziye girmiş olduğumuz kişi id no suna göre arama yapacağız ve aranan kişiye ait bilgileri messagebox ta göstereceğiz.

Şekil 1

Şekil 2
MyForm.h
#pragma once
namespace cppsearchingwithinanarray {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ btnSearch;
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->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->btnSearch = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->textBox1->Location = System::Drawing::Point(125, 53);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(190, 29);
this->textBox1->TabIndex = 2;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label1->Location = System::Drawing::Point(154, 19);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(92, 21);
this->label1->TabIndex = 3;
this->label1->Text = L"Staff Search";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->label2->Location = System::Drawing::Point(12, 56);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(105, 21);
this->label2->TabIndex = 4;
this->label2->Text = L"Enter Staff Id :";
//
// btnSearch
//
this->btnSearch->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->btnSearch->Location = System::Drawing::Point(335, 47);
this->btnSearch->Name = L"btnSearch";
this->btnSearch->Size = System::Drawing::Size(145, 39);
this->btnSearch->TabIndex = 5;
this->btnSearch->Text = L"Search";
this->btnSearch->UseVisualStyleBackColor = true;
this->btnSearch->Click += gcnew System::EventHandler(this, &MyForm::btnSearch_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(492, 140);
this->Controls->Add(this->btnSearch);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox1);
this->Name = L"MyForm";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void btnSearch_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ search;
System::String^ value;
array<String^>^ staffId = { "11000023", "11000034", "84390025", "11000193", "11009802", "11000789", "11000546", "11000328", "11000049", "11007836" };
array<String^>^ name = { "Bahadir Sahin", "Melissa Parker", "Tommy Paton", "Alice May", "Michael Alon", "Mike Carson", "Tracy Abbot", "Nancy White", "Jack Fox", "Alina Benson" };
array<String^>^ phone = { "+1 (212) 221-4937","+1 (425) 882-8080","+1 (867) 920-2233","+1 (408) 996-1010","+1 (650) 644-3358","+1 (775) 223-7665","+1 (407) 999-7788","+1 (617) 522-1333","+1 (312) 332-3232","+1 (608) 257-0597" };
array<String^>^ mail = { "basahin@hotmail.com", "melparker@gmail.com", "tommypat@hotmail.com", "alicemay12@hotmail.com", "michaelal57@gmail.com", "mikecarson33@hotmail.com", "tracyabbot82@hotmail.com", "nancywh@gmail.com", "jackfox89@hotmail.com", "alinabens2@hotmail.com" };
search = textBox1->Text;
value = "";
for (int i = 0; i < staffId->Length; i++)
{
if (search == staffId[i])
{
value = staffId[i]->ToString();
MessageBox::Show("Staff Record Found! Index No : " + i + "\n" + "Staff Id : " + staffId[i] + "\n" + "Name : " + name[i] + "\n" + "Phone : " + phone[i] + "\n" + "Mail : " + mail[i]);
break;
}
}
if (search !=value)
{
MessageBox::Show("Incorrect Staff Id!", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
}
}
};
}
MyForm.cp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//proje isminizi asagiya yaziniz.
cppsearchingwithinanarray::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN