#pragma once

#include "chisqrdistri.h"

using namespace ChiSqrD;


namespace calcChiSqr {

	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>
	/// Form1 の概要
	///
	/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
	///          マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
	///          変更する必要があります。この変更を行わないと、
	///          デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
	///          正しく相互に利用できなくなります。
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクタ コードを追加します
			//
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  buttonCalc;
	protected: 

	private: System::Windows::Forms::Button^  buttonClose;
	protected: 

	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBoxP;

	private: System::Windows::Forms::TextBox^  textBoxDF;


	private: System::Windows::Forms::Label^  labelMsg;


	private:
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->buttonCalc = (gcnew System::Windows::Forms::Button());
			this->buttonClose = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBoxP = (gcnew System::Windows::Forms::TextBox());
			this->textBoxDF = (gcnew System::Windows::Forms::TextBox());
			this->labelMsg = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// buttonCalc
			// 
			this->buttonCalc->Location = System::Drawing::Point(47, 31);
			this->buttonCalc->Name = L"buttonCalc";
			this->buttonCalc->Size = System::Drawing::Size(75, 23);
			this->buttonCalc->TabIndex = 0;
			this->buttonCalc->Text = L"Calc";
			this->buttonCalc->UseVisualStyleBackColor = true;
			this->buttonCalc->Click += gcnew System::EventHandler(this, &Form1::buttonCalc_Click);
			// 
			// buttonClose
			// 
			this->buttonClose->Location = System::Drawing::Point(166, 31);
			this->buttonClose->Name = L"buttonClose";
			this->buttonClose->Size = System::Drawing::Size(75, 23);
			this->buttonClose->TabIndex = 1;
			this->buttonClose->Text = L"Close";
			this->buttonClose->UseVisualStyleBackColor = true;
			this->buttonClose->Click += gcnew System::EventHandler(this, &Form1::buttonClose_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label1->Location = System::Drawing::Point(79, 93);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(29, 16);
			this->label1->TabIndex = 2;
			this->label1->Text = L"p =";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->label2->Location = System::Drawing::Point(74, 141);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(34, 16);
			this->label2->TabIndex = 3;
			this->label2->Text = L"df =";
			// 
			// textBoxP
			// 
			this->textBoxP->Location = System::Drawing::Point(114, 94);
			this->textBoxP->Name = L"textBoxP";
			this->textBoxP->Size = System::Drawing::Size(100, 19);
			this->textBoxP->TabIndex = 4;
			this->textBoxP->Text = L"0.95";
			// 
			// textBoxDF
			// 
			this->textBoxDF->Location = System::Drawing::Point(114, 142);
			this->textBoxDF->Name = L"textBoxDF";
			this->textBoxDF->Size = System::Drawing::Size(100, 19);
			this->textBoxDF->TabIndex = 5;
			this->textBoxDF->Text = L"5";
			// 
			// labelMsg
			// 
			this->labelMsg->AutoSize = true;
			this->labelMsg->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(128)));
			this->labelMsg->Location = System::Drawing::Point(44, 202);
			this->labelMsg->Name = L"labelMsg";
			this->labelMsg->Size = System::Drawing::Size(64, 16);
			this->labelMsg->TabIndex = 6;
			this->labelMsg->Text = L"labelMsg";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 263);
			this->Controls->Add(this->labelMsg);
			this->Controls->Add(this->textBoxDF);
			this->Controls->Add(this->textBoxP);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->buttonClose);
			this->Controls->Add(this->buttonCalc);
			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: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				 labelMsg->Text = "";
			 }
private: System::Void buttonClose_Click(System::Object^  sender, System::EventArgs^  e) {
			 Close();
		 }
private: System::Void buttonCalc_Click(System::Object^  sender, System::EventArgs^  e) {
			 labelMsg->Text = "計算中です。";
			 Update();
			 Application::DoEvents();

			 df = int::Parse(textBoxDF->Text);
			 double p = double::Parse(textBoxP->Text);
			 double L_b = 0.0;
			 double U_b = 0.0;
			 double v;
			 do {
				 U_b += 1.0;
				 v = Cum_Chi_distribution( U_b );
			 } while ( p >= v);

			 double c;
			 Bisection( Cum_Chi_distribution, 1, p, L_b, U_b, c, 1.0e-12 );

			 labelMsg->Text = "c = " + c.ToString("F5") + " , P(ChiSqr < c) = p";
		 }

};
}

