Tuesday, October 12, 2010

GCD (Greatest Common Divisor) calculator

There are various ways to calculate the GCD (Greatest Common Divisor). One way was developed by Marcelo Polezzi:


I converted the above into C++:

Header File:

// Gcd.h : Header file for Gcd Class.
//

#pragma once

#include // input/output

// include namespace to type short-form of functions
using namespace std;

class Gcd
{
public:
Gcd(void);
~Gcd(void);
int getFirstInteger();
int getSecondInteger();
void setFirstInteger(int);
void setSecondInteger(int);
int getGcd();
private:
int firstInteger;
int secondInteger;
};

Implementation:
// Gcd.cpp : Gcd Class.
//

#include "StdAfx.h"
#include "Gcd.h"


Gcd::Gcd(void)
{
}

Gcd::~Gcd(void)
{
}

// *****************
// * get functions *
// *****************

int Gcd::getFirstInteger()
{
return firstInteger;
}

int Gcd::getSecondInteger()
{
return secondInteger;
}

// *****************
// * set functions *
// *****************

void Gcd::setFirstInteger(int x)
{
firstInteger = x;
}

void Gcd::setSecondInteger(int x)
{
secondInteger = x;
}

// Creates the GCD
int Gcd::getGcd()
{
// GCD Math Formula
// Mathematician: Marcelo Polezzi

int part1Formula = 0;
int part2Formula = 0;
int firstNumber, secondNumber, gcd;

// Set numbers entered
firstNumber = getFirstInteger();
secondNumber = getSecondInteger();

// Set part 1 of formula
for (int i = 1; i < firstNumber; i++)
{
part1Formula += (i * ((double)secondNumber / (double)firstNumber));
}
part1Formula *= 2;

// Set part 2 of formula
part2Formula = firstNumber + secondNumber - (firstNumber * secondNumber);

// Create GCD
gcd = part1Formula + part2Formula;

return gcd;

}

Instantiating and calling the object:

// This program will display a simple GCD calculator.

#include "stdafx.h"
#include "Gcd.h"

void getTwoIntegers(int &, int &);

int _tmain(int argc, _TCHAR* argv[])
{

// Variables to hold two integers entered by user
int firstInteger, secondInteger;

// Instantiate Gcd class
Gcd myGcd;

// Initial first two integer
getTwoIntegers(firstInteger, secondInteger);
myGcd.setFirstInteger(firstInteger);
myGcd.setSecondInteger(secondInteger);

// Loop calculator
while (firstInteger != -1)
{
// Display two integers
cout << "The GCD of " << firstInteger << " and "
<< secondInteger << " is "
<< myGcd.getGcd() << endl;
// Get next two integers
getTwoIntegers(firstInteger, secondInteger);
myGcd.setFirstInteger(firstInteger);
myGcd.setSecondInteger(secondInteger);
}

system("pause");

return 0;
}

void getTwoIntegers(int &x, int &y)
{

// Get two integers from user
cout << "GCD Calculator (Enter -1 to Quit)" << endl;
cout << "Enter first integer: ";
cin >> x;
if (x != -1)
{
cout << "Enter second integer: ";
cin >> y;
}

}


Adiel

Thursday, February 25, 2010

Resistance Calculator

I have created a small program to calculate Resistance, Power and Current Density. To download the program click here. Here is a sample screen:















Please let me know if you have any suggestions for improving the program.

Adiel