3

我在课堂上做的一个程序有问题,甚至老师也找不到问题。我们正在做一个程序,要求用户输入 double 然后当他停止时,它会扫描数组并将正负分开以将它们放在不同的数组中。

我们注意到,当我们使用 float 时,程序可以处理更多的数字,但如果我们输入太多,仍然会出错,如果我们在只输入几个数字后使用 double it 就会出错。我的意思是,程序运行良好,但是当它显示结果时,数组中有一些奇怪的数字。这是使用双精度的代码:

#include <iostream>
using namespace std;

void filling(double *, int &);
void sortPositiveNegative(double *, double *, double *, int, int &, int &);
void display(const double *, int);

int main () {
    double * vecteur = new double;
    double * positive = new double;
    double * negative = new double;
    int counter = 0, counterPos = 0, counterNeg = 0;

    cout << "Filling of the real number vector " << endl;
    filling(vecteur, counter);

    cout << endl << "Display of the real number vector " << endl;
    display(vecteur, counter);

    cout << endl << "Sort of the positive and negative in the real number vector: " << endl;
    sortPositiveNegative(vecteur, positive, negative, counter, counterPos, counterNeg);

    cout << endl << "Display of the positive real number : " << endl;
    display(positive, counterPos);

    cout << endl << "Display of the negative real number : " << endl;
    display(negative, counterNeg);

    system("PAUSE");
    return 0;
}

void filling (double *vecteur, int &counter)
{
    bool validation;
    char choice = 'Y';
    do
    {
        do
        {
            validation = true;
            cout << "Please enter the value of case " << counter+1 << ": ";
            cin >> vecteur[counter];
            if(cin.fail())
            {
                cerr << "The number entered is not valid." << endl;
                cin.clear();
                validation = false;
            }
            while(cin.get() != '\n'){}
        }while(!validation);
        counter++;
        do
        {
            validation = true;
            cout <<"Do you wish to continue? (Y/N): ";
            cin >> choice;
            if(toupper(choice) != 'Y' && toupper(choice) != 'N')
            {
                cerr << "We don't understand your choice, please try again." << endl;
                cin.clear();
                validation = false;
            }
            while(cin.get() != '\n'){}
        }while(!validation);
    }
    while(toupper(choice)=='Y');
}

void sortPositiveNegative(double *vecteur, double *positive, double *negative, int counter, int &counterPos, int &counterNeg)
{
    int i = 0;
    for(i; i<counter;i++)
    {
        if(vecteur[i] >= 0)
            positive[counterPos++] = vecteur[i];
        else
            negative[counterNeg++] = vecteur[i];
    }
}

void display (const double *vecteur, int counter)
{
    for(int i = 0; i<counter;i++)
        cout << vecteur[i]<<endl;
    cout << endl;
}

我的老师认为这可能是记忆问题,但我们不知道为什么会这样。

提前致谢。

4

3 回答 3

7

肯定存在内存问题,我不知道 usingfloat将如何解决它。例如,下面的代码行只分配了一个双精度而不是一个双精度数组:

double * vecteur = new double;

然后,您可以将其vecteur用作 N 个元素的数组。这会触发未定义的行为。

要修复它,您必须根据需要分配一个包含尽可能多的值的数组。例如,假设您需要 10,然后您分配 10,如下所示:

double * vecteur = new double[10];

但是,由于您事先不知道元素的数量,因此每次要添加元素时都需要扩展数组。如果你是用 C 写的,我会推荐你​​使用realloc(). 但鉴于你使用 C++,只要坚持使用std::vector<double>,它就会自动管理内存。例如:

#include <vector>

int main()
{
    std::vector<double> vecteur; // Use vector to store array of doubles.

    // Add as many elements as you want.
    // Vector will resize itself if/when needed.
    vecteur.push_back(.1);
    vecteur.push_back(.2);
    vecteur.push_back(.3);
    vecteur.push_back(.4);
    vecteur.push_back(.5);
}

希望能帮助到你。祝你好运!

于 2013-04-26T15:49:27.017 回答
2
double * vecteur = new double;

你为一个双倍分配空间

filling(vecteur, counter);

传给filling

    cin >> vecteur[counter];

并填充,直到用户按下 Y 并使用您已为其分配内存的一个元素离开数组。
doublevsfloat无所谓。float只是更小,因此会更慢地破坏内存。但它仍然会破坏内存,从vecteur[1]
我建议你使用std::vector<dobule>而不是普通指针开始,并用push_back

于 2013-04-26T15:49:56.853 回答
2
double * vecteur = new double;
double * positive = new double;
double * negative = new double;

在这里,您每次只分配一个双倍。您存储在“数组”中的第一个项目很好,但之后的任何其他内容都是未定义的行为。

解决方法是根据需要实际分配尽可能多的项目:

double * vecteur = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * positive = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * negative = new double[MAXIMUM8NUMBER_OF_ITEMS];

或者更好的是,使用标准容器,例如std::vector.

于 2013-04-26T15:50:47.317 回答