0
#pragma once

namespace PointerArray {

    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
    /// </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::Label^  label1;
    protected: 
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Label^  label6;
    private: System::Windows::Forms::Label^  label7;
    private: System::Windows::Forms::Label^  label8;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox5;
    private: System::Windows::Forms::TextBox^  textBox6;
    private: System::Windows::Forms::TextBox^  textBox7;
    private: System::Windows::Forms::TextBox^  textBox8;
    private: System::Windows::Forms::TextBox^  textBox9;
    private: System::Windows::Forms::TextBox^  textBox10;
    private: System::Windows::Forms::TextBox^  textBox11;
    private: System::Windows::Forms::TextBox^  textBox12;
    private: System::Windows::Forms::TextBox^  textBox13;
    private: System::Windows::Forms::TextBox^  textBox14;
    private: System::Windows::Forms::TextBox^  textBox15;
    private: System::Windows::Forms::TextBox^  textBox16;
    private: System::Windows::Forms::Label^  label9;
    private: System::Windows::Forms::Label^  label10;
    private: System::Windows::Forms::Button^  btnData;

    private: System::Windows::Forms::Button^  btnSort;
    private: System::Windows::Forms::Button^  btnSearch;
    private: System::Windows::Forms::Label^  label11;
    private: System::Windows::Forms::TextBox^  txtTarget;

    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->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->label5 = (gcnew System::Windows::Forms::Label());
            this->label6 = (gcnew System::Windows::Forms::Label());
            this->label7 = (gcnew System::Windows::Forms::Label());
            this->label8 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox5 = (gcnew System::Windows::Forms::TextBox());
            this->textBox6 = (gcnew System::Windows::Forms::TextBox());
            this->textBox7 = (gcnew System::Windows::Forms::TextBox());
            this->textBox8 = (gcnew System::Windows::Forms::TextBox());
            this->textBox9 = (gcnew System::Windows::Forms::TextBox());
            this->textBox10 = (gcnew System::Windows::Forms::TextBox());
            this->textBox11 = (gcnew System::Windows::Forms::TextBox());
            this->textBox12 = (gcnew System::Windows::Forms::TextBox());
            this->textBox13 = (gcnew System::Windows::Forms::TextBox());
            this->textBox14 = (gcnew System::Windows::Forms::TextBox());
            this->textBox15 = (gcnew System::Windows::Forms::TextBox());
            this->textBox16 = (gcnew System::Windows::Forms::TextBox());
            this->label9 = (gcnew System::Windows::Forms::Label());
            this->label10 = (gcnew System::Windows::Forms::Label());
            this->btnData = (gcnew System::Windows::Forms::Button());
            this->btnSort = (gcnew System::Windows::Forms::Button());
            this->btnSearch = (gcnew System::Windows::Forms::Button());
            this->label11 = (gcnew System::Windows::Forms::Label());
            this->txtTarget = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(335, 55);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(35, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"label1";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Location = System::Drawing::Point(335, 91);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(35, 13);
            this->label2->TabIndex = 1;
            this->label2->Text = L"label2";
            // 
            // label3
            // 
            this->label3->AutoSize = true;
            this->label3->Location = System::Drawing::Point(335, 123);
            this->label3->Name = L"label3";
            this->label3->Size = System::Drawing::Size(35, 13);
            this->label3->TabIndex = 2;
            this->label3->Text = L"label3";
            // 
            // label4
            // 
            this->label4->AutoSize = true;
            this->label4->Location = System::Drawing::Point(335, 155);
            this->label4->Name = L"label4";
            this->label4->Size = System::Drawing::Size(35, 13);
            this->label4->TabIndex = 3;
            this->label4->Text = L"label4";
            // 
            // label5
            // 
            this->label5->AutoSize = true;
            this->label5->Location = System::Drawing::Point(335, 188);
            this->label5->Name = L"label5";
            this->label5->Size = System::Drawing::Size(35, 13);
            this->label5->TabIndex = 4;
            this->label5->Text = L"label5";
            // 
            // label6
            // 
            this->label6->AutoSize = true;
            this->label6->Location = System::Drawing::Point(335, 221);
            this->label6->Name = L"label6";
            this->label6->Size = System::Drawing::Size(35, 13);
            this->label6->TabIndex = 5;
            this->label6->Text = L"label6";
            // 
            // label7
            // 
            this->label7->AutoSize = true;
            this->label7->Location = System::Drawing::Point(335, 254);
            this->label7->Name = L"label7";
            this->label7->Size = System::Drawing::Size(35, 13);
            this->label7->TabIndex = 6;
            this->label7->Text = L"label7";
            // 
            // label8
            // 
            this->label8->AutoSize = true;
            this->label8->Location = System::Drawing::Point(335, 290);
            this->label8->Name = L"label8";
            this->label8->Size = System::Drawing::Size(35, 13);
            this->label8->TabIndex = 7;
            this->label8->Text = L"label8";
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(229, 52);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 20);
            this->textBox1->TabIndex = 8;
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(229, 88);
            this->textBox2->Name = L"textBox2";
            this->textBox2->Size = System::Drawing::Size(100, 20);
            this->textBox2->TabIndex = 9;
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(229, 120);
            this->textBox3->Name = L"textBox3";
            this->textBox3->Size = System::Drawing::Size(100, 20);
            this->textBox3->TabIndex = 10;
            // 
            // textBox4
            // 
            this->textBox4->Location = System::Drawing::Point(229, 155);
            this->textBox4->Name = L"textBox4";
            this->textBox4->Size = System::Drawing::Size(100, 20);
            this->textBox4->TabIndex = 11;
            // 
            // textBox5
            // 
            this->textBox5->Location = System::Drawing::Point(229, 185);
            this->textBox5->Name = L"textBox5";
            this->textBox5->Size = System::Drawing::Size(100, 20);
            this->textBox5->TabIndex = 12;
            // 
            // textBox6
            // 
            this->textBox6->Location = System::Drawing::Point(229, 218);
            this->textBox6->Name = L"textBox6";
            this->textBox6->Size = System::Drawing::Size(100, 20);
            this->textBox6->TabIndex = 13;
            // 
            // textBox7
            // 
            this->textBox7->Location = System::Drawing::Point(229, 251);
            this->textBox7->Name = L"textBox7";
            this->textBox7->Size = System::Drawing::Size(100, 20);
            this->textBox7->TabIndex = 14;
            // 
            // textBox8
            // 
            this->textBox8->Location = System::Drawing::Point(229, 283);
            this->textBox8->Name = L"textBox8";
            this->textBox8->Size = System::Drawing::Size(100, 20);
            this->textBox8->TabIndex = 15;
            // 
            // textBox9
            // 
            this->textBox9->Location = System::Drawing::Point(30, 52);
            this->textBox9->Name = L"textBox9";
            this->textBox9->Size = System::Drawing::Size(100, 20);
            this->textBox9->TabIndex = 23;
            // 
            // textBox10
            // 
            this->textBox10->Location = System::Drawing::Point(30, 85);
            this->textBox10->Name = L"textBox10";
            this->textBox10->Size = System::Drawing::Size(100, 20);
            this->textBox10->TabIndex = 22;
            // 
            // textBox11
            // 
            this->textBox11->Location = System::Drawing::Point(30, 120);
            this->textBox11->Name = L"textBox11";
            this->textBox11->Size = System::Drawing::Size(100, 20);
            this->textBox11->TabIndex = 21;
            // 
            // textBox12
            // 
            this->textBox12->Location = System::Drawing::Point(30, 155);
            this->textBox12->Name = L"textBox12";
            this->textBox12->Size = System::Drawing::Size(100, 20);
            this->textBox12->TabIndex = 20;
            // 
            // textBox13
            // 
            this->textBox13->Location = System::Drawing::Point(30, 185);
            this->textBox13->Name = L"textBox13";
            this->textBox13->Size = System::Drawing::Size(100, 20);
            this->textBox13->TabIndex = 19;
            // 
            // textBox14
            // 
            this->textBox14->Location = System::Drawing::Point(30, 218);
            this->textBox14->Name = L"textBox14";
            this->textBox14->Size = System::Drawing::Size(100, 20);
            this->textBox14->TabIndex = 18;
            // 
            // textBox15
            // 
            this->textBox15->Location = System::Drawing::Point(30, 251);
            this->textBox15->Name = L"textBox15";
            this->textBox15->Size = System::Drawing::Size(100, 20);
            this->textBox15->TabIndex = 17;
            // 
            // textBox16
            // 
            this->textBox16->Location = System::Drawing::Point(30, 283);
            this->textBox16->Name = L"textBox16";
            this->textBox16->Size = System::Drawing::Size(100, 20);
            this->textBox16->TabIndex = 16;
            // 
            // label9
            // 
            this->label9->AutoSize = true;
            this->label9->Location = System::Drawing::Point(52, 24);
            this->label9->Name = L"label9";
            this->label9->Size = System::Drawing::Size(67, 13);
            this->label9->TabIndex = 24;
            this->label9->Text = L"Pointer Array";
            // 
            // label10
            // 
            this->label10->AutoSize = true;
            this->label10->Location = System::Drawing::Point(247, 24);
            this->label10->Name = L"label10";
            this->label10->Size = System::Drawing::Size(57, 13);
            this->label10->TabIndex = 25;
            this->label10->Text = L"Data Array";
            // 
            // btnData
            // 
            this->btnData->Location = System::Drawing::Point(31, 391);
            this->btnData->Name = L"btnData";
            this->btnData->Size = System::Drawing::Size(75, 23);
            this->btnData->TabIndex = 26;
            this->btnData->Text = L"Data";
            this->btnData->UseVisualStyleBackColor = true;
            this->btnData->Click += gcnew System::EventHandler(this, &Form1::btnData_Click);
            // 
            // btnSort
            // 
            this->btnSort->Location = System::Drawing::Point(146, 391);
            this->btnSort->Name = L"btnSort";
            this->btnSort->Size = System::Drawing::Size(75, 23);
            this->btnSort->TabIndex = 27;
            this->btnSort->Text = L"Sort";
            this->btnSort->UseVisualStyleBackColor = true;
            this->btnSort->Click += gcnew System::EventHandler(this, &Form1::btnSort_Click);
            // 
            // btnSearch
            // 
            this->btnSearch->Location = System::Drawing::Point(255, 391);
            this->btnSearch->Name = L"btnSearch";
            this->btnSearch->Size = System::Drawing::Size(75, 23);
            this->btnSearch->TabIndex = 28;
            this->btnSearch->Text = L"Search";
            this->btnSearch->UseVisualStyleBackColor = true;
            this->btnSearch->Click += gcnew System::EventHandler(this, &Form1::btnSearch_Click);
            // 
            // label11
            // 
            this->label11->AutoSize = true;
            this->label11->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->label11->Location = System::Drawing::Point(27, 334);
            this->label11->Name = L"label11";
            this->label11->Size = System::Drawing::Size(110, 20);
            this->label11->TabIndex = 29;
            this->label11->Text = L"Target Integer";
            // 
            // txtTarget
            // 
            this->txtTarget->Location = System::Drawing::Point(143, 336);
            this->txtTarget->Name = L"txtTarget";
            this->txtTarget->Size = System::Drawing::Size(100, 20);
            this->txtTarget->TabIndex = 30;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(467, 641);
            this->Controls->Add(this->txtTarget);
            this->Controls->Add(this->label11);
            this->Controls->Add(this->btnSearch);
            this->Controls->Add(this->btnSort);
            this->Controls->Add(this->btnData);
            this->Controls->Add(this->label10);
            this->Controls->Add(this->label9);
            this->Controls->Add(this->textBox9);
            this->Controls->Add(this->textBox10);
            this->Controls->Add(this->textBox11);
            this->Controls->Add(this->textBox12);
            this->Controls->Add(this->textBox13);
            this->Controls->Add(this->textBox14);
            this->Controls->Add(this->textBox15);
            this->Controls->Add(this->textBox16);
            this->Controls->Add(this->textBox8);
            this->Controls->Add(this->textBox7);
            this->Controls->Add(this->textBox6);
            this->Controls->Add(this->textBox5);
            this->Controls->Add(this->textBox4);
            this->Controls->Add(this->textBox3);
            this->Controls->Add(this->textBox2);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label8);
            this->Controls->Add(this->label7);
            this->Controls->Add(this->label6);
            this->Controls->Add(this->label5);
            this->Controls->Add(this->label4);
            this->Controls->Add(this->label3);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->label1);
            this->Name = L"Form1";
            this->Text = L"Pointer Array Program";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
        int* arr;
        int** ptr;
        Graphics^ g;
        Pen^ blackPen; 
        private: String^ strTarget;


private: void swap(int** a, int** b)
{
    int* temp;
    temp = *a;
    *a = *b;
    *b = temp;
}


private: void displayPtr()
{
    // Display ptr values in textBox9 - textBox16
    textBox9->Text = ((int) ptr[0]).ToString();
    textBox10->Text = ((int) ptr[1]).ToString();
    textBox11->Text = ((int) ptr[2]).ToString();
    textBox12->Text = ((int) ptr[3]).ToString();
    textBox13->Text = ((int) ptr[4]).ToString();
    textBox14->Text = ((int) ptr[5]).ToString();
    textBox15->Text = ((int) ptr[6]).ToString();
    textBox16->Text = ((int) ptr[7]).ToString();

    // Assigns textBox values to corresponding labels
    label1->Text = ((int) ptr[0]).ToString();
    label2->Text = ((int) ptr[1]).ToString();
    label3->Text = ((int) ptr[2]).ToString();
    label4->Text = ((int) ptr[3]).ToString();
    label5->Text = ((int) ptr[4]).ToString();
    label6->Text = ((int) ptr[5]).ToString();
    label7->Text = ((int) ptr[6]).ToString();
    label8->Text = ((int) ptr[7]).ToString();
}


private: void drawLines()
{
    // Calculate start points (ptrX, ptrY)
    int ptrX = textBox9->Location.X + textBox9->Width;
    int ptrY = textBox9->Location.Y + textBox9->Height/2;

    // Calculate end point (arrX)
    int arrX = textBox1->Location.X;
    int arrY;
    int startY = textBox1->Location.Y + textBox1->Height/2;
    // Refresh the form and draw lines 
    this->Refresh();
    for (int i = 0; i < 8; i++)
    {
        arrY = startY + ((ptr[i] - &(arr[0])) * 32);
        g->DrawLine(blackPen, ptrX, ptrY, arrX, arrY);
        ptrY += 32;
    }
}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             arr = new int[8];
             ptr = new int*[8];
             g = this->CreateGraphics();
             blackPen = gcnew System::Drawing::Pen(Color::Black);
             btnSort->Enabled = false;
             }

private: System::Void btnData_Click(System::Object^  sender, System::EventArgs^  e) {
             // Declare Variables and Create Random Number Generator
             int i; // Loop Control Variable
             Random^ randomNumber;
             DateTime now = DateTime::Now;
             randomNumber = gcnew Random(now.Millisecond);

             // Assign random numbers to arr
             for (i = 0; i < 8; i++) 
                 arr[i] = randomNumber->Next(0, 100);

             // Display arr values in textBox1 - textBox8
             textBox1->Text = arr[0].ToString();
             textBox2->Text = arr[1].ToString();
             textBox3->Text = arr[2].ToString();
             textBox4->Text = arr[3].ToString();
             textBox5->Text = arr[4].ToString();
             textBox6->Text = arr[5].ToString();
             textBox7->Text = arr[6].ToString();
             textBox8->Text = arr[7].ToString();

             // Assign element addresses to ptr
             for (int i = 0; i < 8; i++) ptr[i] = &arr[i];


             // Display the pointer array values
             displayPtr();

             // Draw lines connecting the pointer and data arrays
             drawLines();

             // Turn on Sort Button
             btnSort->Enabled = true;
         }
private: System::Void btnSort_Click(System::Object^  sender, System::EventArgs^  e) {
             // Declare Variables
             String^ output = "";
             int i, j;
             int arrTotal;
             // Bubble sort the ptr array
             for (i = 0; i < 7; i++)
                 for (j = 0; j < 7; j++)
                     if (*ptr[j] > *ptr[j+1])
                         swap(&(ptr[j]), &(ptr[j+1]));

             // Display pointers with their new address contents
             displayPtr();
             // Concatenate values in sorted order onto output String
             for (i = 0; i < 8; i++) output += (*ptr[i]).ToString() + "\r\n";
             // Draw lines connecting pointer array and data array
             drawLines();
             // Display sorted data in MessageBox

             arrTotal = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5] + arr[6] + arr[7];
             MessageBox::Show(output, "Sorted Data");
             MessageBox::Show("Sorted Data Sum = " + arrTotal.ToString());

             // Draw lines again after MessageBox is closed
             drawLines();
         }

private: System::Void btnSearch_Click(System::Object^  sender, System::EventArgs^  e) {
             // Declare and Initalize Variables
             String^ output = "";
             int i; 
             bool found = false;
             // Read the Target

这里有错误------------------------------------------------ -------------

我不知道为什么我的 for 循环应该跳到用户输入到我的 txtTarget 文本框的数字(说“输入整数”)我不知道如何解决这个问题,就我而言可以告诉我的循环是有道理的,但我不确定任何和所有的帮助都会受到赞赏

             for (i = 0; i < 8; i++) output = (*ptr[i]).ToString(); + "\r\n";
             MessageBox::Show(output->ToString());
             MessageBox::Show(output, "Target Found "); 
         }
};
}
4

1 回答 1

0

此时任何机会都ptr[i]包含错误的指针 - 不是 NULL 因为那会崩溃,而是挂起正在破坏基于堆栈的计数器的指针i?这是我的猜测。此外:

你的交换函数在使用双指针时让我有点害怕。 std::swap会更好。

这个:

        // Bubble sort the ptr array
         for (i = 0; i < 7; i++)
             for (j = 0; j < 7; j++)
                 if (*ptr[j] > *ptr[j+1])
                     swap(&(ptr[j]), &(ptr[j+1]));

是一种非常糟糕的类型,并且可能是错误的,因为它确实使所写的工作增加了一倍。可以在这里使用std::sort()吗?

        // Display sorted data in MessageBox

         arrTotal = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5] + arr[6] + arr[7];

如果您向数组添加另一个值,请改用 for 循环或算法调用来帮助您。

接下来,这个:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         arr = new int[8];
         ptr = new int*[8];

那些 8s 是神奇的数字,并要求未来的麻烦。使用 RAII 来处理您的分配会更清洁、更安全。

最后是现在:

textBox9->Text = ((int) ptr[0]).ToString();

不是 64 位安全的,应该抛出警告。

您是否将编译器警告设置为高级别并全部修复?因为他们可能会向您展示我还没有看到的其他错误。

于 2013-06-07T18:04:55.433 回答