document string_fibonacci

This commit is contained in:
Krishna Vedala 2020-05-28 15:17:53 -04:00
parent ec8f410558
commit 4e12f5361f
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,82 +1,88 @@
//This Programme returns the Nth fibonacci as a string. /**
//The method used is manual addition with carry and placing it in a string which is called string addition * @file
//This makes it have no bounds or limits * @brief This Programme returns the Nth fibonacci as a string.
*
* The method used is manual addition with carry and placing it in a string
* which is called string addition This makes it have no bounds or limits
*
* @see fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp
*/
#include <iostream> #include <iostream>
#include <string> #ifdef _MSC_VER
#include <string> // use this for MS Visual C
#else
#include <cstring> // otherwise
#endif
using namespace std; /**
* function to add two string numbers
string add(string a, string b) * \param [in] a first number in string to add
{ * \param [in] b second number in string to add
string temp = ""; * \returns sum as a std::string
*/
std::string add(std::string a, std::string b) {
std::string temp = "";
// carry flag // carry flag
int carry = 0; int carry = 0;
// fills up with zeros // fills up with zeros
while ((int)a.length() < (int)b.length()) while ((int)a.length() < (int)b.length()) {
{
a = "0" + a; a = "0" + a;
} }
// fills up with zeros // fills up with zeros
while ((int)b.length() < (int)a.length()) while ((int)b.length() < (int)a.length()) {
{
b = "0" + b; b = "0" + b;
} }
// adds the numbers a and b // adds the numbers a and b
for (int i = a.length() - 1; i >= 0; i--) for (int i = a.length() - 1; i >= 0; i--) {
{
char val = (char)(((a[i] - 48) + (b[i] - 48)) + 48 + carry); char val = (char)(((a[i] - 48) + (b[i] - 48)) + 48 + carry);
if (val > 57) if (val > 57) {
{
carry = 1; carry = 1;
val -= 10; val -= 10;
} } else {
else
{
carry = 0; carry = 0;
} }
temp = val + temp; temp = val + temp;
} }
// processes the carry flag // processes the carry flag
if (carry == 1) if (carry == 1) {
{
temp = "1" + temp; temp = "1" + temp;
} }
// removes leading zeros. // removes leading zeros.
while (temp[0] == '0' && temp.length() > 1) while (temp[0] == '0' && temp.length() > 1) {
{
temp = temp.substr(1); temp = temp.substr(1);
} }
return temp; return temp;
} }
void fib_Accurate(long long n) /** Fibonacci iterator
{ * \param [in] n n^th Fibonacci number
string tmp = ""; */
string fibMinus1 = "1"; void fib_Accurate(long long n) {
string fibMinus2 = "0"; std::string tmp = "";
for (long long i = 0; i < n; i++) std::string fibMinus1 = "1";
{ std::string fibMinus2 = "0";
for (long long i = 0; i < n; i++) {
tmp = add(fibMinus1, fibMinus2); tmp = add(fibMinus1, fibMinus2);
fibMinus2 = fibMinus1; fibMinus2 = fibMinus1;
fibMinus1 = tmp; fibMinus1 = tmp;
} }
cout << fibMinus2; std::cout << fibMinus2;
} }
int main() /** main function */
{ int main() {
int n; int n;
cout << "Enter whatever number N you want to find the fibonacci of\n"; std::cout << "Enter whatever number N you want to find the fibonacci of\n";
cin >> n; std::cin >> n;
cout << n << " th Fibonacci is \n"; std::cout << n << " th Fibonacci is \n";
fib_Accurate(n); fib_Accurate(n);
return 0; return 0;