c ++动态数组浮点异常

时间:2015-04-23 01:23:55

标签: c++ dynamic-arrays floating-point-exceptions

对于我的作业,我不得不在c ++中设计一个arraylist,只使用1d数组和指针使数组动态化。我已经做了充分的测试,我的功能正常工作,但是当我使用老师提供给我的主要内容时,我得到了这个浮点错误。这个功课的目的是创建一个适用于教师主要课程的课程,而无需更改主要

中的任何代码

这里是主要的:

#include "ArrayList.h"
#include <iostream>
using namespace std;

int main(int argc,char *argv[])
{
    ArrayList arr;

    for (int i=1;i<=50;i++)
    {
        arr.push_back(i);
    }

    cout << "Should contain numbers 1..50, is ";

    cout << arr.toString() << endl;

    for (int i=arr.size()-1;i>=1;i--)
    {
        arr.erase(arr[i]);
    }   

    cout << "Should contain only 1, is ";
    cout << arr.toString() << endl;

    arr.erase(arr[0]);

    for (int i=1;i<=50;i++)
    {
        if (i<=2)
            arr.push_back(i);
        else
        {
            int j=1;
            while ((j<arr.size()) && (i%arr[j]!=0))
                j++;

            if (j==arr.size())
            {
                arr.push_back(i);
            }
        }
    }

    cout << "Prime numbers between 1 and 50 are: " << arr.toString() << endl;

}

这是我的cpp:

#include<iostream>
#include<string>
#include<sstream>
#include "ArrayList.h"

using namespace std;
void ArrayList:: intialArr(int arr[])
{
    for(int i = 0; i < length; i++)
    {
        arr[i] = 0;
    }
}

string ArrayList:: toString()
{
    std::ostringstream ss;
    for(int i = 0; i < capacity; i++)
    {
        if(arr[i]>0 || arr[i] <0)
        {
            ss << arr[i] << " ";
        }
    }
    return ss.str();
}

ArrayList::ArrayList()
{
    length = 1;
    capacity=0;
    arr = new int[length];
    intialArr(arr);
}

int& ArrayList:: operator[] (unsigned int i)
{
    return arr[i];
}

void ArrayList:: push_back(int m)
{
    if(capacity>=length)
    {   
       int oldlength = length;

       length = length*2;
       int* curArr = new int[length];
       intialArr(curArr);
       for (int i = 0; i < oldlength; i++)
       {
          curArr[i] = arr[i];
       }
       delete [] arr;
       arr = curArr;
    }

    arr[capacity] = m;
    capacity++;
}   


void ArrayList:: erase(int m)
{
        if(capacity == length/2)
        {   
            length = length/2;
            int* curArr = new int[length];
            intialArr(curArr);      
        for (int i = 0; i<capacity; i++)
        {
                curArr[i] = arr[i];

        }
        delete [] arr;
        arr = curArr;
        }

        for(int i = 0; i < capacity; i++)
        {
            if(arr[i]==m)
            {
                for(int j = i; j<length; j++)
                {
                    arr[j] = arr[j+1];
                }
                capacity--;
                break;
            }
    }
    cout << "length = " << length << " capacity = " << capacity << " capacity/length = " << capacity*2 << endl;
}

从我所读到的,当你尝试除以零或无限值出现时,通常会抛出在线浮点异常,但我不明白我是如何得到这些问题的。

我的代码通过添加和删除数字1-50的主要部分,但是一旦我设置数组以保存素数(在主要的arr.erase(arr [0]之后),我得到错误)

我只是在主要设置了几个标签,以找到我的号码进入while ((j<arr.size()) && (i%arr[j]!=0))的样子,我发现在崩溃前我的号码是

j = 1 and arr[j] = 2
i = 5 and arr.size() = 4

0 个答案:

没有答案