Загрузка данных


#pragma once

namespace MEdit {

	using namespace System;
	using namespace System::Windows::Forms;
	using namespace System::Drawing;

	public ref class Form1 : public Form
	{
	public:
		Form1()
		{
			InitializeComponent();
		}

	private:

		TextBox^ sumBox;
		TextBox^ monthsBox;
		TextBox^ percentBox;
		Button^ calcBtn;
		DataGridView^ table;

	void InitializeComponent()
	{
		this->Text = "Финансовый калькулятор";
		this->Width = 500;
		this->Height = 400;

		Label^ l1 = gcnew Label();
		l1->Text = "Сумма:";
		l1->Left = 20; l1->Top = 20;

		Label^ l2 = gcnew Label();
		l2->Text = "Срок (мес.):";
		l2->Left = 20; l2->Top = 50;

		Label^ l3 = gcnew Label();
		l3->Text = "Проц. ставка:";
		l3->Left = 20; l3->Top = 80;

		sumBox = gcnew TextBox();
		sumBox->Left = 150; sumBox->Top = 20;

		monthsBox = gcnew TextBox();
		monthsBox->Left = 150; monthsBox->Top = 50;

		percentBox = gcnew TextBox();
		percentBox->Left = 150; percentBox->Top = 80;

		calcBtn = gcnew Button();
		calcBtn->Text = "OK";
		calcBtn->Left = 150;
		calcBtn->Top = 110;
		calcBtn->Click += gcnew EventHandler(this, &Form1::Calculate);

		table = gcnew DataGridView();
		table->Left = 20;
		table->Top = 150;
		table->Width = 440;
		table->Height = 180;

		table->ColumnCount = 4;
		table->Columns[0]->Name = "Месяц";
		table->Columns[1]->Name = "Долг";
		table->Columns[2]->Name = "Процент";
		table->Columns[3]->Name = "Платеж";

		this->Controls->Add(l1);
		this->Controls->Add(l2);
		this->Controls->Add(l3);
		this->Controls->Add(sumBox);
		this->Controls->Add(monthsBox);
		this->Controls->Add(percentBox);
		this->Controls->Add(calcBtn);
		this->Controls->Add(table);
	}

	void Calculate(Object^ sender, EventArgs^ e)
	{
		table->Rows->Clear();

		double sum = Convert::ToDouble(sumBox->Text);
		int months = Convert::ToInt32(monthsBox->Text);
		double percent = Convert::ToDouble(percentBox->Text) / 100.0;

		double monthlyRate = percent / 12.0;
		double payment = sum * (monthlyRate / (1 - Math::Pow(1 + monthlyRate, -months)));

		double balance = sum;

		for (int i = 1; i <= months; i++)
		{
			double interest = balance * monthlyRate;
			double mainDebt = payment - interest;

			balance -= mainDebt;

			table->Rows->Add(
				i,
				balance.ToString("F2"),
				interest.ToString("F2"),
				payment.ToString("F2")
			);
		}
	}
	};
}