簡體   English   中英

C++ 中堆棧實現的主要因素

[英]Prime factors with stack implementation in C++

問候堆棧溢出,最近遇到了一個問題,我的代碼沒有完全按照我的意圖去做。 我的意圖是讓用戶輸入一個數字,然后程序將檢查它的素數,當它找到一個素數時,我希望它把數字壓入堆棧。 我已經嘗試在整個程序中放置 cout 語句,以查看是否有任何特定位置不起作用,但我還沒有找到任何東西。 我相信我非常接近解決這個問題,但我不確定如何從這里前進。

這是我的代碼:

#include <iostream>
#include <cmath>
using namespace std;

class stack
{
    public:
        static const int MAX = 100;
        stack() { used = 0; } // constructor

        void push(int entry);
        int pop();
        int size() { return used; }
        bool empty() { return used == 0;}

    private:
        int data[MAX];
        int used; // how many elements are used in the stack, top element is used - 1
};

void stack::push(int entry)
{
    data[used] = entry;
    ++used;
}

int stack::pop()
{
    --used;
    return data[used];
}

void primeFactors(stack, int);

int main()
{
    stack primeValues;
    int entry = 0;

    cout << "Enter a positive integer (0 to stop): ";
        cin >> entry;

    if (entry != 0)
    {
        cout << "Prime factors: " << entry << " = ";
    }
    primeFactors(primeValues, entry);

    while(primeValues.pop() != 0) // hopefully continues to pop the prime factors until it reaches zero?
    {
        cout << primeValues.pop() << " ";
    }

    return 0;
}

void primeFactors(stack primeValues, int entry)
{
    if (entry == 0)
    {
        return; // terminate when zero is reached
    }
    if (entry == 1)
    {
        cout << entry; // if 1 is reached display one
    }

    while (entry % 2 == 0) // while there is no remainder do the following
    {
        primeValues.push(2); // push two into stack 
        entry = entry/2;
    }

    for (int i = 3; i <= sqrt(entry); i = i+2) // start looping through numbers to find more prime factors
    {
        while (entry % i == 0)
        {
            primeValues.push(i);
            entry = entry/i;
        }
    }

    if (entry > 2) // if the number is greater than two and doesnt have prime factors push the number
    {
        primeValues.push(entry);
    }
}

我嘗試了各種不同的數字,但似乎沒有任何效果。 我試過彈出幾次以查看是否有任何內容被推送,但它只顯示為零。 我在這里錯過了什么?

你犯了一個非常簡單的錯誤。 在按值將堆棧傳遞給primeFactors時,將復制堆棧並處理副本。 primeFactors完成時,該副本將被丟棄,並留下您原來的空堆棧。

利用 C++ 模板:

#include <iostream>
#include <cmath>

template <typename T, unsigned int MAX>
class stack {
    private:
    T data[MAX] = { 0 };
    unsigned int used = 0;

    public:
    stack() : used(0) {}

    void push(T entry) {
        if (used <= MAX - 1) {
            data[used] = entry;
            used += 1;
        }
    }

    T pop() {
        used -= 1;
        return data[used];
    }

    unsigned int size() const {
        return used;
    }

    bool empty() const {
        return used == 0;
    }
};

template <typename T, unsigned int MAX>
void primeFactors(stack<T, MAX>&, int);

int main() {
    stack<int, 100> primeValues;
    int entry = 0;

    std::cout << "Enter a positive integer (0 to stop): ";
    std::cin >> entry;

    if (entry == 0) {
        return 0;
    }

    primeFactors(primeValues, entry);

    while (!primeValues.empty()) {
        std::cout << primeValues.pop() << " ";
    }

    std::cout << std::endl;
}

template <typename T, unsigned int MAX>
void primeFactors(stack<T, MAX>& primeValues, int entry) {
    if (entry == 0) {
        return;
    }

    if (entry == 1) {
        std::cout << entry << std::endl;
    }

    while (entry % 2 == 0) {
        primeValues.push(2);
        entry /= 2;
    }

    for (int i = 3; i <= std::sqrt(entry); i += 2) {
        while (entry % i == 0) {
            primeValues.push(i);
            entry /= i;
        }
    }
}```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM