Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)

MyForm.h

By: GGMethos on Mar 7th, 2014  |  syntax: C++  |  size: 11.58 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #pragma once
  2. #include <cmath>
  3.  
  4. namespace Lab3 {
  5.  
  6.         using namespace System;
  7.         using namespace System::ComponentModel;
  8.         using namespace System::Collections;
  9.         using namespace System::Windows::Forms;
  10.         using namespace System::Data;
  11.         using namespace System::Drawing;
  12.  
  13.         /// <summary>
  14.         /// Summary for MyForm
  15.         /// </summary>
  16.         public ref class MyForm : public System::Windows::Forms::Form
  17.         {
  18.         public:
  19.                 MyForm(void)
  20.                 {
  21.                         InitializeComponent();
  22.                         //
  23.                         //TODO: Add the constructor code here
  24.                         //
  25.                 }
  26.  
  27.         protected:
  28.                 /// <summary>
  29.                 /// Clean up any resources being used.
  30.                 /// </summary>
  31.                 ~MyForm()
  32.                 {
  33.                         if (components)
  34.                         {
  35.                                 delete components;
  36.                         }
  37.                 }
  38.         private: System::Windows::Forms::Label^  label1;
  39.         private: System::Windows::Forms::TextBox^  textBox1;
  40.         private: System::Windows::Forms::Label^  label2;
  41.         private: System::Windows::Forms::TextBox^  textBox2;
  42.         private: System::Windows::Forms::Label^  label3;
  43.         private: System::Windows::Forms::TextBox^  textBox3;
  44.         private: System::Windows::Forms::Label^  label4;
  45.         private: System::Windows::Forms::TextBox^  textBox4;
  46.         private: System::Windows::Forms::Button^  button1;
  47.         private: System::Windows::Forms::Button^  button2;
  48.         private: System::Windows::Forms::Button^  button3;
  49.         private: System::Windows::Forms::ErrorProvider^  errorProvider1;
  50.  
  51.         private: System::ComponentModel::IContainer^  components;
  52.  
  53.         protected:
  54.  
  55.         private:
  56.                 /// <summary>
  57.                 /// Required designer variable.
  58.                 /// </summary>
  59.  
  60.  
  61. #pragma region Windows Form Designer generated code
  62.                 /// <summary>
  63.                 /// Required method for Designer support - do not modify
  64.                 /// the contents of this method with the code editor.
  65.                 /// </summary>
  66.                 void InitializeComponent(void)
  67.                 {
  68.                         this->components = (gcnew System::ComponentModel::Container());
  69.                         this->label1 = (gcnew System::Windows::Forms::Label());
  70.                         this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  71.                         this->label2 = (gcnew System::Windows::Forms::Label());
  72.                         this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  73.                         this->label3 = (gcnew System::Windows::Forms::Label());
  74.                         this->textBox3 = (gcnew System::Windows::Forms::TextBox());
  75.                         this->label4 = (gcnew System::Windows::Forms::Label());
  76.                         this->textBox4 = (gcnew System::Windows::Forms::TextBox());
  77.                         this->button1 = (gcnew System::Windows::Forms::Button());
  78.                         this->button2 = (gcnew System::Windows::Forms::Button());
  79.                         this->button3 = (gcnew System::Windows::Forms::Button());
  80.                         this->errorProvider1 = (gcnew System::Windows::Forms::ErrorProvider(this->components));
  81.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->errorProvider1))->BeginInit();
  82.                         this->SuspendLayout();
  83.                         //
  84.                         // label1
  85.                         //
  86.                         this->label1->AutoSize = true;
  87.                         this->label1->Location = System::Drawing::Point(47, 9);
  88.                         this->label1->Name = L"label1";
  89.                         this->label1->Size = System::Drawing::Size(90, 13);
  90.                         this->label1->TabIndex = 0;
  91.                         this->label1->Text = L"Loan Amount (C)";
  92.                         this->label1->Click += gcnew System::EventHandler(this, &MyForm::label1_Click);
  93.                         //
  94.                         // textBox1
  95.                         //
  96.                         this->textBox1->Location = System::Drawing::Point(62, 37);
  97.                         this->textBox1->Name = L"textBox1";
  98.                         this->textBox1->Size = System::Drawing::Size(51, 20);
  99.                         this->textBox1->TabIndex = 1;
  100.                         this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
  101.                         //
  102.                         // label2
  103.                         //
  104.                         this->label2->AutoSize = true;
  105.                         this->label2->Location = System::Drawing::Point(188, 9);
  106.                         this->label2->Name = L"label2";
  107.                         this->label2->Size = System::Drawing::Size(148, 13);
  108.                         this->label2->TabIndex = 2;
  109.                         this->label2->Text = L"Interest Rate Percentage (R)";
  110.                         this->label2->Click += gcnew System::EventHandler(this, &MyForm::label2_Click);
  111.                         //
  112.                         // textBox2
  113.                         //
  114.                         this->textBox2->Location = System::Drawing::Point(222, 37);
  115.                         this->textBox2->Name = L"textBox2";
  116.                         this->textBox2->Size = System::Drawing::Size(67, 20);
  117.                         this->textBox2->TabIndex = 3;
  118.                         //
  119.                         // label3
  120.                         //
  121.                         this->label3->AutoSize = true;
  122.                         this->label3->Location = System::Drawing::Point(379, 9);
  123.                         this->label3->Name = L"label3";
  124.                         this->label3->Size = System::Drawing::Size(171, 13);
  125.                         this->label3->TabIndex = 4;
  126.                         this->label3->Text = L"Number of Monthly Payments (N)";
  127.                         //
  128.                         // textBox3
  129.                         //
  130.                         this->textBox3->Location = System::Drawing::Point(391, 37);
  131.                         this->textBox3->Name = L"textBox3";
  132.                         this->textBox3->Size = System::Drawing::Size(140, 20);
  133.                         this->textBox3->TabIndex = 5;
  134.                         this->textBox3->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox3_TextChanged);
  135.                         //
  136.                         // label4
  137.                         //
  138.                         this->label4->AutoSize = true;
  139.                         this->label4->Location = System::Drawing::Point(614, 9);
  140.                         this->label4->Name = L"label4";
  141.                         this->label4->Size = System::Drawing::Size(110, 13);
  142.                         this->label4->TabIndex = 6;
  143.                         this->label4->Text = L"Monthly Payment (P)";
  144.                         //
  145.                         // textBox4
  146.                         //
  147.                         this->textBox4->Location = System::Drawing::Point(604, 37);
  148.                         this->textBox4->Name = L"textBox4";
  149.                         this->textBox4->Size = System::Drawing::Size(141, 20);
  150.                         this->textBox4->TabIndex = 7;
  151.                         //
  152.                         // button1
  153.                         //
  154.                         this->button1->Location = System::Drawing::Point(62, 82);
  155.                         this->button1->Name = L"button1";
  156.                         this->button1->Size = System::Drawing::Size(51, 23);
  157.                         this->button1->TabIndex = 8;
  158.                         this->button1->Text = L"P=";
  159.                         this->button1->UseVisualStyleBackColor = true;
  160.                         this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
  161.                         //
  162.                         // button2
  163.                         //
  164.                         this->button2->Location = System::Drawing::Point(222, 82);
  165.                         this->button2->Name = L"button2";
  166.                         this->button2->Size = System::Drawing::Size(67, 23);
  167.                         this->button2->TabIndex = 9;
  168.                         this->button2->Text = L"R=";
  169.                         this->button2->UseVisualStyleBackColor = true;
  170.                         this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
  171.                         //
  172.                         // button3
  173.                         //
  174.                         this->button3->Location = System::Drawing::Point(391, 82);
  175.                         this->button3->Name = L"button3";
  176.                         this->button3->Size = System::Drawing::Size(140, 23);
  177.                         this->button3->TabIndex = 10;
  178.                         this->button3->Text = L"N=";
  179.                         this->button3->UseVisualStyleBackColor = true;
  180.                         this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
  181.                         //
  182.                         // errorProvider1
  183.                         //
  184.                         this->errorProvider1->ContainerControl = this;
  185.                         //
  186.                         // MyForm
  187.                         //
  188.                         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  189.                         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  190.                         this->ClientSize = System::Drawing::Size(758, 125);
  191.                         this->Controls->Add(this->button3);
  192.                         this->Controls->Add(this->button2);
  193.                         this->Controls->Add(this->button1);
  194.                         this->Controls->Add(this->textBox4);
  195.                         this->Controls->Add(this->label4);
  196.                         this->Controls->Add(this->textBox3);
  197.                         this->Controls->Add(this->label3);
  198.                         this->Controls->Add(this->textBox2);
  199.                         this->Controls->Add(this->label2);
  200.                         this->Controls->Add(this->textBox1);
  201.                         this->Controls->Add(this->label1);
  202.                         this->Name = L"MyForm";
  203.                         this->Text = L"Loan Calculator";
  204.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->errorProvider1))->EndInit();
  205.                         this->ResumeLayout(false);
  206.                         this->PerformLayout();
  207.  
  208.                 }
  209. #pragma endregion
  210.         private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
  211.         }
  212.         private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  213.         }
  214.         private: System::Void label2_Click(System::Object^  sender, System::EventArgs^  e) {
  215.         }
  216.         private: System::Void textBox3_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  217.         }
  218. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
  219. {
  220.                          double loanAmount = System::Convert::ToDouble(textBox1->Text);
  221.                          double interestRate = System::Convert::ToDouble(textBox2->Text);
  222.                          double numberOfMonths = System::Convert::ToDouble(textBox3->Text);
  223.                          double rRate = interestRate / 1200;
  224.                          double numerater = ((loanAmount * rRate) * (pow((1 + rRate), numberOfMonths)));
  225.                          double denominater = ((pow((1 + rRate), numberOfMonths)) - 1);
  226.                          double newr = numerater / denominater;
  227.                          textBox4->Text = System::Convert::ToString(newr);
  228.  
  229. }
  230.  
  231.  
  232.         private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  233.                                  if (textBox2->Text == ""){
  234.                                          double loanAmount = System::Convert::ToDouble(textBox1->Text);
  235.                                          double payment = System::Convert::ToDouble(textBox4->Text);
  236.                                          double numberOfMonths = System::Convert::ToDouble(textBox3->Text);
  237.  
  238.                                          double initial = (2 * (numberOfMonths * payment - loanAmount) / (numberOfMonths * loanAmount));
  239.                                          double rOld = (initial * (pow((1 + initial), numberOfMonths)) / ((pow((1 + initial), numberOfMonths) - 1))) - (payment / loanAmount);
  240.  
  241.                                          double b1 = payment * pow((1. + rOld), -numberOfMonths);
  242.                                          double c1 = (rOld * loanAmount);
  243.                                          double num = payment - (b1 - c1);
  244.  
  245.                                          double denom = numberOfMonths * (payment * pow((1. + rOld), (-numberOfMonths - 1)) - loanAmount);
  246.                                          int terminate = 0;
  247.                                          double rNew;
  248.  
  249.                                          while (terminate == 0)
  250.                                          { //terminating factor to exit the loop
  251.                                                  rNew = rOld - (num / denom);
  252.                                                  if (fabs(rNew - rOld) >= 0.01) //loops until difference is less than .01, then terminates
  253.                                                  {
  254.                                                          rOld = rNew;
  255.                                                  }
  256.                                                  else
  257.                                                  {
  258.                                                          terminate = 1;
  259.                                                  }
  260.                                                  textBox2->Text = System::Convert::ToString(rNew); //outputs desired results
  261.                                          }
  262.  
  263.                                  }
  264.                                  else{
  265.                                   errorProvider1->SetError(textBox2, "Please keep this blank to calculate the interest rate."); //error check
  266.                                   }
  267.         }
  268.  
  269.  
  270.  
  271. /*private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)
  272.  
  273. {
  274.  
  275.  
  276.                          double loanAmount = System::Convert::ToDouble(textBox1->Text); //converts the input value to double, if it wasn't already input as such
  277.                          double annualInterestRate = System::Convert::ToDouble(textBox2->Text);
  278.                          double numberOfYears = System::Convert::ToDouble(textBox3->Text);
  279.                          double monthlyInterestRate = annualInterestRate / 1200;
  280.                          double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
  281.                          double totalPayment = monthlyPayment * numberOfYears * 12;
  282.  
  283.                          monthlyPayment = floor(monthlyPayment * 100 + 0.5) / 100;
  284.                          totalPayment = floor(totalPayment * 100 + 0.5) / 100;
  285.                          textBox3->Text = System::Convert::ToString(monthlyPayment); //computes result, and displays values in text box
  286.                          //tbTotalPayment->Text = System::Convert::ToString(totalPayment);
  287.  
  288.  
  289. }*/
  290.  
  291.                          private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
  292.  
  293.                                                   if (textBox3->Text == ""){
  294.  
  295.                                                           double loanAmount = System::Convert::ToDouble(textBox1->Text); //converts the input value to double, if it wasn't already input as such
  296.                                                           double annualInterestRate = System::Convert::ToDouble(textBox2->Text);
  297.                                                           double payment = System::Convert::ToDouble(textBox4->Text);
  298.  
  299.                                                           annualInterestRate = annualInterestRate / 1200; //computes the value of number of payments with the given information
  300.                                                           double top = log(payment / loanAmount) - log((payment / loanAmount) - annualInterestRate);
  301.                                                           double bottom = log(1 + annualInterestRate);
  302.                                                           double months = top / bottom;
  303.                                                           textBox3->Text = System::Convert::ToString(months);
  304.                                                   }
  305.                                                   else
  306.                                                   {
  307.                                                           errorProvider1->SetError(textBox3, "Please keep this blank to calculate the number of months.");
  308.                                                   }
  309.  
  310.                          }
  311.  
  312. private: System::Void errorProvider1_RightToLeftChanged(System::Object^  sender, System::EventArgs^  e) {
  313. }
  314. };
  315. }