User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,942 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,585 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Aug 27th, 2007
Views: 2,527
A simple solution that prints the first n values in the fibonacci series up to the limit of an unsigned long. The program takes either command line arguments or interactive input for n and has code in place to make the program more flexible, like printing to a file.
cplusplus Syntax
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <limits>
  4. #include <sstream>
  5. #include <stdexcept>
  6.  
  7. using namespace std;
  8.  
  9. /// <summary> Make sure that two values can be summed without overflow. </summary>
  10. template <class T>
  11. bool CanSum( T a, T b ) {
  12. // If the difference of the largest value of T and a
  13. // is less than b, the addition will overflow.
  14. return numeric_limits<T>::max() - a >= b;
  15. }
  16.  
  17. /// <summary> Find the next Fibonacci value, and update a and b. </summary>
  18. template <class T>
  19. T NextInSequence( T& a, T& b ) {
  20. T next = a + b;
  21.  
  22. b = a;
  23. a = next;
  24.  
  25. return next;
  26. }
  27.  
  28. /// <summary> Print the first n values of the Fibonacci series. </summary>
  29. template <class CharT>
  30. void FibonacciSequence( basic_ostream<CharT>& stream, int n ) {
  31. unsigned long a = 0;
  32. unsigned long b = 1;
  33.  
  34. for ( int i = 0; i < n && CanSum( a, b ); ++i ) {
  35. cout<< NextInSequence( a, b ) <<"\n";
  36. }
  37. }
  38.  
  39. /// <summary> Get the next value of type T from the stream. </summary>
  40. /// <parameter name="isConsole"> True if the stream is a console. </parameter>
  41. /// <returns> The input item or T's default value on eof. </returns>
  42. template <class T, class CharT>
  43. T GetNextItem( basic_istream<CharT>& stream, bool isConsole ) {
  44. T item = T();
  45.  
  46. while ( !( cin >> item ) && !cin.eof() ) {
  47. if ( !isConsole ) {
  48. // Input is automated. All we can do is error.
  49. throw runtime_error( "Invalid item type." );
  50. } else {
  51. // Input is interactive. Get the user to recover.
  52. stream.clear();
  53. stream.sync();
  54. cerr<<"Invalid item type. Try again> ";
  55. }
  56. }
  57.  
  58. return item;
  59. }
  60.  
  61. const int INVALID_INDEX = -1;
  62.  
  63. int main( int argc, char *argv[] ) {
  64. // The index of the Fibonacci number to stop at.
  65. int n = INVALID_INDEX;
  66.  
  67. if ( argc >= 2 ) {
  68. // Try to get the command line index first.
  69. stringstream( argv[1] ) >> n;
  70. }
  71.  
  72. if ( n == INVALID_INDEX ) {
  73. // If there's no command line index or the command line
  74. // didn't have a valid index, get it interactively.
  75. cout<<"Fibonacci numbers to generate> ";
  76. n = GetNextItem<int>( cin, true );
  77. }
  78.  
  79. if ( cin.good() ) {
  80. // Only run the sequence if there were no errors or eof.
  81. FibonacciSequence( cout, n );
  82. }
  83.  
  84. return 0;
  85. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 6:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC