动态数组类不会打印

时间:2016-04-29 09:08:10

标签: c arrays pointers dynamic-arrays

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cstddef>

class ArrayList
{
    public:
        ArrayList();
        void expand();
        void store(std::string x); 
        void display_size();
        friend std::ostream& operator<<(std::ostream& os, const ArrayList &arr);
        void clean();
        ArrayList(const ArrayList &arr); 
        ~ArrayList();
        void operator=(const ArrayList& arr);
    private:
        int size; //total size of your dynamic array
        int max; //double the size of the array.
        int free_space; //total size of unused space in your dynamic array
        char *array; //a pointer used to create a dynamic array
};


ArrayList::ArrayList()
{
    size = 2;
    free_space = 1;
    array = new char[2]();
    array[0] = ' ';
    array[1] = '\0';
    max = size;

}

void ArrayList::expand()
{

    max = size + size;
    char *temp = new char[max];
    for( int i = 0; i < max; i++ )
    {
        array[i] = temp[i];
    }  
    free_space = free_space + size;
    delete [] array;
    array = temp;   
    size = max;
}

void ArrayList::store(std::string x) 
{
    int taken = max - free_space;
    int y = x.size();
    free_space = free_space - y;
    for(int i = 0; i < y; i++)
    {
        if(y >= size)
        {
            while(y >= size)
            {
                expand();
            }
        }
        else
        {
            array[i + taken] = x[i];
        }               
    }
}

std::ostream& operator<<(std::ostream& os, const ArrayList &arr)
{
    os << arr.array;
    return os;
}

void ArrayList::display_size()
{
    size = max;
    std::cout << "Array Content: ";
    std::cout << array;
    std::cout << std::endl;
    std::cout << "Remaining size: ";
    std::cout << free_space; 
    std::cout << std::endl;
}

void ArrayList::clean()
{
    int x = 0;
    for(int i = 0; i < size; i++)
    {
        if(array[i] == ' ')
        {
            x++;
        }
    }
    size = x;
}

ArrayList::ArrayList(const ArrayList &arr) 
{
    array = new char[size + 1];
    strcpy(array, arr.array);
}

ArrayList::~ArrayList()
{
    delete [] array;
}

void ArrayList::operator=(const ArrayList& arr)
{
    int new_length = strlen(arr.array);
    if(new_length > max)
    {
        delete [] array;
        max = new_length;
        array = new char[max + 1];
    }
    for(int i = 0; i < new_length; i++)
    {
        array[i] = arr.array[i];
    }
    array[new_length] = '\0';
}


int main()
{
    ArrayList x;
    std::string y;
    char ans;
    x.display_size();
    std::cout << "Please enter your string: ";
    std::cin >> y;
    x.store(y);
    x.display_size();// << std::endl;
    do
    {
        std::cout << "Please enter your string: ";
        std::cin >> y;
        x.store(y);
        x.display_size();

        std::cout << "Do you want to enter another string? (y/n) ";
        std::cin >> ans;
    }while(ans != 'n');
    return 0;
}

我的问题是关于C ++动态数组。我创建了一个创建动态数组的类。

我发布了我的整个代码,它应该是可以运行的。

问题源于使用storeexpand函数。

store接受一个字符串并将每个字符放入数组中,如果没有足够的空间则调用expand

expand将数组的大小加倍。

Array Content:  
Remaining size: 1
Please enter your string: h
Array Content:  h
Remaining size: 0
Please enter your string: ello
Array Content: 
Remaining size: 2
Do you want to enter another string? (y/n) n

理论上,上面的输出应该已经返回&#34;你好&#34;然而,尽管退回了“&#39; h”,但它还没有归还任何东西。早。我完全没有关于如何解决这个问题的想法。

编辑:

我根据给我的建议改变了功能:

void ArrayList::expand()
{
    max = size + size;
    char *temp = new char[max];
    for( int i = 0; i < max; i++ )
    {
        temp[i] = array[i];
    }  
    free_space = free_space + size;
    delete [] array;
    array = temp;   
    size = max;
}

void ArrayList::store(std::string x) 
{
    int taken = max - free_space;

    int y = x.size();
    free_space = free_space - y;
    for(int i = 0; i < y; i++)
    {
        if(free_space <= 0)
        {
            while(free_space <= 0)
            {
                expand();
            }
        }
        else
        {
            array[i+taken] = x[i]; //I'm cetain this didn't do anything
        }               
    }
}

我已经解决了评论中详述的负数问题。现在唯一的问题是打印号码。

1 个答案:

答案 0 :(得分:2)

这&#34; for&#34;循环进入&#34;展开&#34;方法:

for( int i = 0; i < max; i++ )
{
    array[i] = temp[i];
}  

应替换为:

for( int i = 0; i < size; i++ )
{
    temp[i] = array[i];
}