C ++ program abruptly ends after cin

I am writing code to get the last digit of very large fibonacci numbers like fib (239), etc. I use strings to store numbers, capturing individual characters from the end to the beginning, and then converting them to int and instead of saving the values ​​back to another string. I could not verify what I wrote because my program abruptly closes after a line std::cin >> n;. Here is what I still have.

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using namespace std; 

char get_fibonacci_last_digit_naive(int n) {
cout << "in func";
if (n <= 1)
    return (char)n;

string previous= "0";
string current= "1";

for (int i = 0; i < n - 1; ++i) {
    //long long tmp_previous = previous;
    string tmp_previous= previous; 

    previous = current;

    //current = tmp_previous + current; // could also use previous instead of current
    // for with the current length of the longest of the two strings
    //iterates from the end of the string to the front
    for (int j=current.length(); j>=0; --j) {
        // grab consectutive positions in the strings & convert them to integers
        int t;
        if (tmp_previous.at(j) == '\0') 
            // tmp_previous is empty use 0 instead
            t=0;  
        else
            t = stoi((string&)(tmp_previous.at(j))); 
        int c = stoi((string&)(current.at(j)));
        // add the integers together
        int valueAtJ= t+c;
        // store the value into the equivalent position in current
        current.at(j) = (char)(valueAtJ); 
    }
    cout << current << ":current value"; 
}

return current[current.length()-1];
}

int main() {
int n;
std::cin >> n;

//char& c = get_fibonacci_last_digit_naive(n);  // reference to a local variable returned WARNING
// http://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable
cout << "before call";
char c = get_fibonacci_last_digit_naive(n);
std::cout << c << '\n';

return 0;
}

The conclusion is consistently the same. No matter what I input for n, the output is always the same. This is the line I used to run the code and its output.

$ g++ -pipe -O2 -std=c++14 fibonacci_last_digit.cpp -lm

$ ./a.exe
10

After 10 and 10, a new line appears, which I entered for n. I appreciate any help. And happy holidays!

+4
source share
4

, , , , . XY, , , .

Nth , N . ,

fib(0) = 0 
fib(1) = 1 
fib(n) = fib(n-1) + fib(n-2), for all n larger than 1.

fib(N) :

unsigned fib(unsigned n)
{
    if (n <= 1)
        return n;

    unsigned previous = 0;
    unsigned current = 1;
    for (int i=1; i<n; ++i)
    {
        unsigned value = previous + current;
        previous = current;
        current = value;
    }
    return current;
}

, , , N ( unsigned 32- 47 ).

fib . . , base-10 last-digit . :

current = value;

:

current = value % 10;

, , "" :

unsigned fib_last_digit(unsigned n)
{
    if (n <= 1)
        return n;

    unsigned previous = 0;
    unsigned current = 1;
    for (int i=1; i<n; ++i)
    {
        unsigned value = previous + current;
        previous = current;
        current = value % 10; // HERE
    }
    return current;
}

current , , 10 . , , 18, .. , , , .

Validation

, 20 : fib:

0:0
1:1
2:1
3:2
4:3
5:5
6:8
7:13
8:21
9:34
10:55
11:89
12:144
13:233
14:377
15:610
16:987
17:1597
18:2584
19:4181
20:6765

, fib_last_digit :

0:0
1:1
2:1
3:2
4:3
5:5
6:8
7:3
8:1
9:4
10:5
11:9
12:4
13:3
14:7
15:0
16:7
17:7
18:4
19:1
20:5

, , , , , .

+7

Mac :

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string before callin funcAbort trap: 6


:

for (int j=current.length(); j>=0; --j) {

:

  • , current.at(j), . , "blah" 4, 4 .
  • tmp_previous . tmp_previous.at(j) 8 13, .

, , , , , , . , , , , stoi.

+1

. 100000, . . , , :

std::string str_add(std::string a, std::string b)
{
    // http://ideone.com/o7wLTt

    size_t n = max(a.size(), b.size());
    if (n > a.size()) {
        a = string(n-a.size(), '0') + a;
    }

    if (n > b.size()) {
        b = string(n-b.size(), '0') + b;
    }

    string result(n + 1, '0');

    char carry = 0;

    std::transform(a.rbegin(), a.rend(), b.rbegin(), result.rbegin(), [&carry](char x,  char y)
    {
        char z = (x - '0') + (y - '0') + carry;
        if (z > 9) {
            carry = 1;
            z -= 10;
        } else {
            carry = 0;
        }
        return z + '0';
    });

    result[0] = carry + '0';

    n = result.find_first_not_of("0");
    if (n != string::npos) {
        result = result.substr(n);
    }

    return result;
}

std::string str_fib(size_t i)
{
    std::string n1 = "0";
    std::string n2 = "1";
    for (size_t idx = 0; idx < i; ++idx) {
        const std::string f = str_add(n1, n2);
        n1 = n2;
        n2 = f;
    }
    return n1;
}

int main() {
    const size_t i = 100000;
    const std::string f = str_fib(i);
    if (!f.empty()) {
        std::cout << "fibonacci of " << i << " = " << f << " | last digit: " << f[f.size() - 1] << std::endl;
    }

    std::cin.sync(); std::cin.get();
    return 0;
}
+1

Try to calculate the fibonacci number first and then convert intto std::stringwith std::to_string(). in the following, you can extract the last digit using the operator []at the last index.

int fib(int i)
{
    int number = 1;
    if (i > 2) {
        number = fib(i - 1) + fib(i - 2);
    }
    return number;
}

int main() {
    const int i = 10;
    const int f = fib(i);
    const std::string s = std::to_string(f);

    if (!s.empty()) {
        std::cout << "fibonacci of " << i << " = " << f << " | last digit: " << s[s.size() - 1] << std::endl;
    }

    std::cin.sync(); std::cin.get();
    return 0;
}

Avoid duplicate keywords using using.

Also consider switching from intto longor long longwhen your numbers increase. Since Fibonacci numbers are positive, use as well unsigned.

0
source

Source: https://habr.com/ru/post/1664954/


All Articles