Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么赢了';这个堆栈不能正确显示吗?_C++ - Fatal编程技术网

C++ 为什么赢了';这个堆栈不能正确显示吗?

C++ 为什么赢了';这个堆栈不能正确显示吗?,c++,C++,我必须写一个程序来求一个数的素因式分解。例如,如果数字为3960,则基本因子为11 5 3 3 2 2。当我运行程序时,这不是您得到的输出。我的代码怎么了?谢谢 #include <iostream> #include <stdio.h> using namespace std; #include "Stack.h" int main() { int i; int n; int d; Stack s; cout << "Stack created.

我必须写一个程序来求一个数的素因式分解。例如,如果数字为3960,则基本因子为11 5 3 3 2 2。当我运行程序时,这不是您得到的输出。我的代码怎么了?谢谢

#include <iostream>
#include <stdio.h>
using namespace std;

#include "Stack.h"

int main()
{
int i;
int n;
int d;
Stack s;
   cout << "Stack created.  Empty? " << boolalpha << s.empty() << endl;



int num;
int div;
    cout<<"Enter a number to know its prime factor: "<<endl;
    cin >> num;

    cout << "\nThe prime factors of "<<num<<" are: \n\n" << endl;

    div = 2;

    while(num!=0){
        if(num%div!=0)
            div = div + 1;
        else {
            num = num / div;
            s.push(div);
            if(num==1)
              break;
        }

s.display(cout);

    }
}
#包括
#包括
使用名称空间std;
#包括“Stack.h”
int main()
{
int i;
int n;
int d;
堆栈s;

cout您正在计算循环中打印堆栈。对
s.display(cout)
的调用需要放在大括号的一个级别之外

您的代码如下所示:

    while(num!=0){
        if(num%div!=0)
            div = div + 1;
        else {
            num = num / div;
            s.push(div);
            if(num==1)
              break;
        }

s.display(cout);

    }
Yuch!正确格式化它,它是:

while(num!=0){
    if(num%div!=0)
        div = div + 1;
    else {
        num = num / div;
        s.push(div);
        if(num==1)
          break;
    }
    s.display(cout);
}
这显然不是你想要的。你想要:

while(num!=0){
    if(num%div!=0)
        div = div + 1;
    else {
        num = num / div;
        s.push(div);
        if(num==1)
          break;
    }
}
s.display(cout);
这有望教会您正确缩进代码

我们不知道您的
堆栈
类是什么,但是这段代码确实正确地提取了基本因子,并且只打印了一次

我也不确定您为什么在这里使用自己的堆栈类。您完全可以使用
std::vector
。如下所示:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> stack;
    int num;
    int div;

    std::cout << "Enter a number to know its prime factor: ";
    std::cin >> num;
    std::cout << "\nThe prime factors of " << num << " are: \n\n" << std::endl;
    div = 2;
    while (num > 1){
        if (num%div != 0)
            div++;
        else {
            num /= div;
            stack.push_back(div);
        }
    }

    for (std::vector<int>::const_iterator iter = stack.begin(); iter != stack.end(); ++iter)
        std::cout << *iter << ' ';
    std::cout << std::endl;
}
#包括
#包括
int main()
{
std::向量堆栈;
int-num;
国际部;
std::cout>num;

std::你得到了什么输出?你的缩进到处都是,你为什么要混合
printf
cout
?你得到了什么输出?你需要发布
堆栈的代码,也许我们可以帮你you@Karl很抱歉,我回滚了您的编辑,因为错误的格式是问题的根本原因非常感谢。它很有效。我为代码的草率表示歉意。正确地进行缩进是值得的。即使在最好的时候编程也很难!!