Borland C++ Builder

Implementation of programming language C++

Borland C++ Builder is an IDE created by Borland Software Corporation. C++ Builder 2007 is incorporated in CodeGear RAD Studio 2007.

Latest version (C++ Builder 2010) key features:

  • supports rapid application development methodology;
  • includes drag-and-drop WYSIWYG graphical user interface builder;
  • supports the draft of C++0x standard;
  • includes Boost and TR1 libraries;
  • incorporates Borland Visual Component Library (VCL) and Component Library for Cross Platform (CLX);
  • has built-in support for major databases and touch and gesture processing.

Examples:

Hello, World!:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5
#include <iostream>

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

Factorial:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

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:

Example for versions Borland C++ Builder 6, g++ 3.4.5

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:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

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;
}

CamelCase:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

This example is based on character-by-character string processing.

getline reads a string (delimited with end of line) from argument stream. Function tolower works only with single characters, so to convert whole string to lower case it is used with transform function. The latter applies tolower to all elements in the range [text.begin(), text.end()) and stores the results in a range starting with text.begin() again.

After this the string is processed char-by-char. Each character is checked for being alphabetic; if it is, it is appended to the resulting string (converted to upper case if previous character was non-alphabetic); if it is not, it only affects lastSpace (which is true only if last character was non-alphabetic).

isalpha works with both uppercase and lowercase letters, so it was possible not to convert the input string to lowercase, but rather to convert each appended character.

#include <string> 
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    string text, cc="";
    bool lastSpace = true;
    getline(cin, text);
    transform(text.begin(), text.end(), text.begin(), (int (*)(int))tolower);
    for (int i=0; i<text.size(); i++)
        if (isalpha(text[i])) {
            if (lastSpace)
                cc += toupper(text[i]);
            else
                cc += text[i];
            lastSpace = false;
        }
        else {
            lastSpace = true;        
        }
    cout << cc << endl;
    return 0;
}

Quadratic equation:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008)

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 <cmath>

using namespace std;

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

int main()
{   complex<double> A, B, C, D;
    cout << "A = ";
    cin >> A;
    if (abs(A)<1E-3)
    {   cout << "Not a quadratic equation" << endl;
        return 1;
    }
    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);
    }
    return 0;
}

Quadratic equation:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25

This example works both for C and C++, as well as for Objective-C which is superset of C.

#include <math.h> 
#include <stdio.h>

int main()
{
  int A, B, C;
  double D;
  printf("A = ");
  scanf("%d", &A);
  if (A == 0) {
    printf("Not a quadratic equation.\n");
    return 0;
  }
  
  printf("B = ");
  scanf("%d", &B);
  printf("C = ");
  scanf("%d", &C);

  D = B * B - 4 * A * C;
  if (D == 0) {
    printf("x = %f\n", -B / 2.0 / A);
    return 0;
  }
  
  if (D > 0) {
    printf("x1 = %f\nx2 = %f\n",
           (-B + sqrt(D)) / 2.0 / A, (-B - sqrt(D))/ 2.0 / A);
  } else {
    printf("x1 = (%f, %f)\nx2 = (%f, %f)\n",
           -B / 2.0 / A, sqrt(-D) / 2.0 / A, -B / 2.0 / A, -sqrt(-D) / 2.0 /A);
  }
  return 0;
}

Hello, World!:

Example for versions Borland C++ Builder 6, Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}