Borland C++ Builder 6

Version of implementation Borland C++ Builder of programming language C++

Borland C++ Builder, version 6. Appeared in 2002.

Examples:

Hello, World! - C++ (1):

#include <iostream>

int main(void)
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Factorial - C++ (2):

This example uses recursive factorial definition.

#include "stdio.h"

__int64 factorial(__int64 n)
{
	return ( n==0 ? 1 : n*factorial(n-1) );
}

int main(int argc, char* argv[])
{
	for (int n=0; n<=16; n++)
		printf( "%d! = %I64d\n", n, factorial(n) );
	return 0;
}

Factorial - C++ (3):

This example uses recursive factorial definition.

#include <iostream>

unsigned long long factorial(unsigned long long n)
{
	if (n == 0) 
		return 1;
	else 
		return n * factorial (n - 1);
}

int main(void)
{
    for (int n = 0; n <= 16; n++) 
        std::cout << n << "! = " << factorial(n) << std::endl;
    return 0;
}

Fibonacci numbers - C++ (4):

This example uses recursive definition of Fibonacci numbers.

#include <iostream>

int fibonacci(int n)
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

int main(void)
{
    for (int n=1; n<=16; n++)
        std::cout << fibonacci(n) << ", ";
    std::cout << "..." << std::endl;
    return 0;
}

Quadratic equation - C++ (162):

This example uses a template class complex<>, provided in STL. All calculations are done in complex numbers, because this allows not to worry about the sign of discriminant and representation of the roots.

Operator >> of complex is overloaded so that it can recognize several formats of the number, so the input constants are read not as integers but as complex numbers without imaginary part. This implementation allows to solve quadratic equations not only with integer coefficients but also with floating-point and even complex ones.

Operator << is also overloaded to print the number x as (x.real(),x.imag()), so to print roots without imaginary part as a single double, function print was created.

#include <iostream>
#include <complex>
#include <math>

using namespace std;

void print(int ind, complex<double> x)
{   cout << "x" << ind << " = ";
    if (fabs(x.imag()) < 1E-6)
        cout << x.real() << endl;
    else cout << x << endl;
}

void main()
{   complex<double> A, B, C, D, x1, x2;
    cout << "A = ";
    cin >> A;
    if (abs(A)<1E-3)
    {   cout << "Not a quadratic equation" << endl;
        return;
    }
    cout << "B = ";
    cin >> B;
    cout << "C = ";
    cin >> C;

    A *= 2;
    D = B*B-A*C*2.0;
    if (abs(D)<1E-3)
        cout << "x = " << (-B/A).real();
    else
    {   print(1, (-B+sqrt(D))/A);
        print(2, (-B-sqrt(D))/A);
    }
}