I have got a homework named greatest common divisor of two integers write in C++ in MS visual c++ 2008 in windows form. Using listbox, textbox... I don't know how write this programme. Please help me thank you. Sorry for my English.
kooia 1 Light Poster
I have got a homework named greatest common divisor of two integers write in C++ in MS visual c++ 2008 in windows form. Using listbox, textbox... I don't know how write this programme. Please help me thank you. Sorry for my English.
Not exactly sure what you mean
You want a program, that has two textboxes. You press a button and it finds the greatest common divisor.
If this is the case, I can't help you. However, if you just want a simple .exe program (like, run from MS c++ or Linux Terminal), I can help you.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cout << "First #: ";
cin >> a;
cout << "Second #: ";
cin >> b;
cout << "\n\n";
int biggest = 0;
int counta = 1;
int gcd = 0;
if(a >= b){biggest = a;}
if(b > a){biggest = b;}
while(counta <= biggest/2)
{
if(a%counta==0 && b%counta==0){gcd = counta;}
++counta;
}
cout << gcd;
cout << "\n\n";
return 0;
}
goback 0 Newbie Poster
Thank you for reply. But I need program which has two textboxes maybe listbox and button like you said. Can help me somebody else?
goback 0 Newbie Poster
Nobody knows?
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Do you know how to calculate the GCD of a value? That's the code that goes into you CALC button.
If you are asking how to interface with the form, textboxes, and buttons from the beginning, you need a tutorial, not a forum. We help people solve problems, we are not teachers.
n.utiu 0 Light Poster
Try to use this function to calculate the GCD of two numbers:
int computeGCD (const int& intA, const int& intB) {
if (intB == 0)
return intA;
return computeGCD (intB, intA % intB);
}
Edited by n.utiu because: n/a
goback 0 Newbie Poster
Well. I tried wrote this programme but i have problem. I create 1x listbox, 2x textboxes and 1x button. When I will compile this, then I will write some numbers to textboxes (example 8 to textbox1 and 4 to textbox2). Afterward I will click on the button but a answer is 0.
Please tell me where I made mistake. Thank you.
#pragma once
namespace av {
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </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::Button^ button1;
protected:
private: System::Windows::Forms::ListBox^ listBox1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
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->button1 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(102, 231);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System::Drawing::Point(81, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 159);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(180, 159);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 3;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
int a,b,answer;
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1->Clear();
textBox2->Clear();
while (a!=b) {
if (a>b)
a=a-b;
else
b=b-a;
}
listBox1->Items->Add(answer);
}
};
}
goback 0 Newbie Poster
Hi. Homework is almost done. When I enter to textbox1 example number 1 and to textbox2 number 0 and conversely then the programme will brake down. But if I enter only 0 so the programme running normal. Please tell me how write this part. Thank you.
#pragma once
namespace skoroporeseny {
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </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::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::ListBox^ listBox1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(107, 214);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 162);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 1;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(180, 162);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 2;
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System::Drawing::Point(86, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(120, 95);
this->listBox1->TabIndex = 3;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
String ^st;
double a,b;
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (textBox1->Text !="" || textBox2->Text !="" ){
st = textBox1->Text;
a = Convert::ToDouble(st);
st = textBox2->Text;
b = Convert::ToDouble(st);
textBox1->Clear();
textBox2->Clear();
listBox1->Items->Clear();
if (a <= 0 || b <= 0)
{
MessageBox::Show("Number must be greater than 0");
}
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
st = Convert::ToString(a);
listBox1->Items->Add(st);}
else
{
MessageBox::Show("No numbers entered");
}
}
};
}
goback 0 Newbie Poster
Nobody knows?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.