Fibonacci-verbose-vt100.C 1/5
[top][prev][next]
#include <iostream>
#include <string>
using namespace std;
// vt100 codes to do some special drawing characters
// DO NOT DO THIS ON A PROJECT - if (terminal != vt100) { UGLY }
const string ESC = string(1, char(27));
const string STR1 = ESC + "(0tqqq" + ESC + "(B";
const string STR2 = ESC + "(0mqqq" + ESC + "(B";
const string STR3 = ESC + "(0x" + ESC + "(B ";
const string STR4 = " ";
// probably a better way to do this but it is eluding me at the moment
int Fibonacci(int x, string now, string future) {
cout << now << "Fibonacci(" << x << ") " << endl;
if(x==1 || x==2) {
return 1;
} else {
return Fibonacci(x-1, future + STR1, future + STR3) +
Fibonacci(x-2, future + STR2, future + STR4);
}
}
int main (int argc, char** argv) {
if(argc!=2) {
cerr << "Usage: " << argv[0] << " n" << endl;
exit(EXIT_FAILURE);
}
int x = atoi(argv[1]);
cout << endl << "Result: Fibonacci(" << x << "): "
<< Fibonacci(x,"","") << endl;
return EXIT_SUCCESS;
}
Fibonacci-verbose.C 2/5
[top][prev][next]
#include <iostream>
#include <string>
using namespace std;
// string to have a nice tree like drawing
const string STR1 = "+---";
const string STR2 = "+---";
const string STR3 = "| ";
const string STR4 = " ";
// probably a better way to do this but it is eluding me at the moment
int Fibonacci(int x, string now, string future) {
cout << now << "Fibonacci(" << x << ") " << endl;
if(x==1 || x==2) {
return 1;
} else {
return Fibonacci(x-1, future + STR1, future + STR3) +
Fibonacci(x-2, future + STR2, future + STR4);
}
}
int main (int argc, char** argv) {
if(argc!=2) {
cerr << "Usage: " << argv[0] << " n" << endl;
exit(EXIT_FAILURE);
}
int x = atoi(argv[1]);
cout << endl << "Result: Fibonacci(" << x << "): "
<< Fibonacci(x,"","") << endl;
return EXIT_SUCCESS;
}
Fibonacci.C 3/5
[top][prev][next]
#include <iostream>
using namespace std;
int Fibonacci(int x) {
if(x==1 || x==2) {
return 1;
} else {
return Fibonacci(x-1) + Fibonacci(x-2);
}
}
int main (int argc, char** argv) {
if(argc!=2) {
cerr << "Usage: " << argv[0] << " n" << endl;
exit(EXIT_FAILURE);
}
int x = atoi(argv[1]);
cout << "Result: Fibonacci(" << x << "): " << Fibonacci(x) << endl;
return EXIT_FAILURE;
}
factorial-verbose.C 4/5
[top][prev][next]
#include <iostream>
using namespace std;
// global - yuk!
int indenting = 0;
int factorial(int x) {
indenting++;
for(int i = 0; i < indenting - 1; i++ ) { cout << " "; }
// base case
if(x==0) {
cout << "1" << endl;
indenting--;
return 1;
// recurrence case
} else {
cout << x << " * factorial(" << x - 1 << ")" << endl;
int result = factorial(x-1);
indenting--;
return x * result;
}
}
int main(int argc, char** argv) {
if(argc!=2) {
cerr << "Usage: " << argv[0] << " n" << endl;
exit(EXIT_FAILURE);
}
int x = atoi(argv[1]);
cout << endl << "factorial(" << x << "): " << factorial(x) << endl;
return EXIT_FAILURE;
}
factorial.C 5/5
[top][prev][next]
#include <iostream>
using namespace std;
int factorial(int x) {
// base case
if(x==0) {
return 1;
// recurrence case
} else {
return x * factorial(x-1);
}
}
int main(int argc, char** argv) {
if(argc!=2) {
cerr << "Usage: " << argv[0] << " n" << endl;
exit(EXIT_FAILURE);
}
int x = atoi(argv[1]);
cout << "factorial(" << x << "): " << factorial(x) << endl;
return EXIT_FAILURE;
}
Generated by GNU enscript 1.6.1.