| View previous topic :: View next topic |
| Author |
Message |
heresy
Joined: 11 Mar 2008 Posts: 291
|
Posted: Tue Aug 05, 2008 11:13 am Post subject: did you know that ... |
|
|
you were already coding C?
factorial
| Code: | ; C Language
int factorial(int x)
{
if (x == 0) {
return 1;
} else {
return x * factorial(x - 1);
}
} |
| Code: | ; AutoHotkey
factorial(x)
{
if (x == 0) {
return 1
} else {
return x * factorial(x - 1)
}
} |
fibonacci
| Code: | ; C Language
unsigned int fibonacci(unsigned int n)
{
if ((n == 0) || (n == 1))
return n;
else
return fibonacci(n-1) + fibonacci(n-2);
} |
| Code: | ; AutoHotkey
fibonacci(n)
{
if ((n == 0) || (n == 1))
return n
else
return fibonacci(n-1) + fibonacci(n-2)
} |
even one-liner fibonacci
| Code: | ; C Language
unsigned int fib(unsigned int n)
{
return n < 2 ? n : fib(n-1) + fib(n-2);
} |
| Code: | ; AutoHotkey
fib(n)
{
return n < 2 ? n : fib(n-1) + fib(n-2)
} |
_________________ Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com |
|
| Back to top |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 1390 Location: The Interwebs
|
Posted: Tue Aug 05, 2008 6:43 pm Post subject: |
|
|
They become farther apart when doing things more complicated than math operations, though...
Or so I think? |
|
| Back to top |
|
 |
Red Hat Dude Guest
|
Posted: Wed Aug 06, 2008 9:50 pm Post subject: |
|
|
I remember my first attempts at learning c++ were complete failures. I could do some basic stuff (the most complicated thing I ever wrote was a hex/number converter).
I just decided to start learning Ruby today. Popular in Japan, I hear.
Sample code from a command-line gallon to liter converter in C++ (that I wrote):
| Code: |
#include <iostream.h>
#include <string>
int main()
{
int doStuff;
float gallonAmountInput, gallonAmount;
float literAmountInput, literAmount;
char gUnit, mUnit;
string gUnitWord, mUnitWord;
cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
cin >> doStuff;
while (doStuff != 0)
{
while (doStuff == 1)
{
// repeat this until user enters a valid unit
cout << "Enter units of measure. Units include: " << endl << "g for gallons" << endl;
cout << "q for quarts" << endl << "p for pints" << endl << "c for cups" << endl << "o for ounces" << endl;
cin >> gUnit;
cout << "Enter amount: " ;
cin >> gallonAmountInput;
gallonAmount = gallonAmountInput;
switch (gUnit)
{
case 'g' :
gUnitWord = "gallons";
break;
case 'q' :
gallonAmount /= 4;
gUnitWord = "quarts";
break;
case 'p' :
gallonAmount /= 8;
gUnitWord = "pints";
break;
case 'c' :
gallonAmount /= 16;
gUnitWord = "cups";
break;
case 'o' :
gallonAmount /= 128;
gUnitWord = "ounces";
break;
}
literAmount = gallonAmount * 3.785;
cout << gallonAmountInput << " " << gUnitWord << " is " << literAmount << " liters. " << endl;
cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
cin >> doStuff;
}
while (doStuff == 2)
{
cout << "1 for Liters, 2 for Milliliters (mL): ";
cin >> mUnit;
cout << "Enter amount: " ;
cin >> literAmountInput;
literAmount = literAmountInput;
switch (mUnit)
{
case '1' :
mUnitWord = "liters";
break;
case '2' :
mUnitWord = "milliliters";
literAmount /= 1000;
break;
}
gallonAmount = literAmount / 3.785;
cout << literAmountInput << " " << mUnitWord << " is " << gallonAmount << " gallons. " << endl;
cout << "Enter 1 to convert gallons to liters, 2 for liters to gallons, 0 to quit: ";
cin >> doStuff;
}
if (doStuff == 0)
{
return 0;
}
}
}
|
|
|
| Back to top |
|
 |
Dragonscloud
Joined: 16 Jul 2005 Posts: 96
|
Posted: Thu Aug 21, 2008 12:34 pm Post subject: |
|
|
I liked the structure of Ruby, and the scripts perform pretty well, but when I compiled them they ran very slowly in comparison to AHK, or even VBA. _________________ “yields falsehood when preceded by its quotation” yields falsehood when preceded by its quotation. |
|
| Back to top |
|
 |
Red Hat Dude Guest
|
Posted: Thu Aug 21, 2008 11:08 pm Post subject: |
|
|
Heh, I didn't care for the syntax of Ruby. I still plan on learning it, at least a little, but I'm gonna take a look at Python.
Both Ruby and Python are pretty high-level languages, which means they have less complicated code than a C++ program, but may run slower... |
|
| Back to top |
|
 |
|