输入要在阵列中显示的数量

时间:2017-04-23 03:03:35

标签: c++ arrays class exception

我正在编写一个显示整数数组的程序。我设置了数组的大小,但我想知道如何向用户询问他们想要列出的数组的索引。假设 const SIZE = 10 ,用户想要查看数组中的前三个。我还想写一个异常,如果用户输入超过了数组的大小,就会捕获错误。如果您需要查看一些代码,请告诉我们。任何帮助表示赞赏!

intergerarray.h

class IntArray
{
private:
    int *aptr;                     // Pointer to the array
    int arraySize;                 // Holds the array size
    void subscriptError();         // Handles invalid subscripts
public:
    class OutOfBoundException
    {
    public:
        int index;
        OutOfBoundException(){};
        int getInde() { return index; }

    };
    IntArray(int);                 // Constructor
    IntArray(const IntArray &);    // Copy constructor
    ~IntArray();                   // Destructor

    int size() const               // Returns the array size
    {
        return arraySize;
    }

    int &operator[](const int &);  // Overloaded [] operator
};

IntergerArray.cpp

IntArray::IntArray(int s)
{
    arraySize = s;
    aptr = new int[s];
    for (int count = 0; count < arraySize; count++)
        *(aptr + count) = 0;
}


IntArray::IntArray(const IntArray &obj)
{
    arraySize = obj.arraySize;
    aptr = new int[arraySize];
    for (int count = 0; count < arraySize; count++)
        *(aptr + count) = *(obj.aptr + count);
}


IntArray::~IntArray()
{
    if (arraySize > 0)
        delete[] aptr;
}


void IntArray::subscriptError()
{
    cout << "ERROR: Subscript out of range.\n";
    exit(0);
}


int &IntArray::operator[](const int &sub)
{
    if (sub < 0 || sub >= arraySize)
        subscriptError();
    return aptr[sub];
}

driver file.cpp

    int main()
{
    int SIZE = 10;
    //int index;
    //cout << "enter an index";
    //cin >> index;
    IntArray table(SIZE);

    for (int x = 0; x < SIZE; x++)
        table[x] = x;

    for (int x = 0; x < SIZE; x++)
        cout << table[x] << " ";
    cout << endl;

    //table[SIZE + 1] = 0;

    return 0;
}

3 个答案:

答案 0 :(得分:0)

我认为您希望显示低于用户输入的索引值的所有元素:

让array []为size = 10的数组名,你可以从用户那里得到一个索引值(比如l)并在for loop中使用该值来打印索引中的所有元素低于l

 int array[size] 
void disp_in(int l)
  {
   if(l>=size) // if l greater than or equal to size (as index start at 0)
      throw l;
   else
       {

        cout<<"Elements : ";
        for(int i=0;i<=l;i++) //if we have say l=2 ,array values in indexes 0,1and 2 will be displayed
            cout<<array[i];

         }
    }
int main ()
      {
       int l;
       cout<<"Enter index : "; 
       cin>>l;  //till this index value, the array value will be displayed
       try
          {
           disp_in(l);
            }
       catch(int e)
          {
          cout<<"Index value greater than max index"; 
            }
         return 0;
         }

答案 1 :(得分:0)

这不是你想要做的吗?为什么这么简单的问题代码呢?

    const int arraySize = 10;
    int array[arraySize];

    int elementToDis;
    do
    {
        std::cout << "Number of array elements to display: ";
        std::cin >> elementToDis;
    } while (elementToDis > arraySize || elementToDis < 0); // this is your exeption

    for (int ccc(0); ccc < elementToDis; ++ccc)
        std::cout << "Index " << ccc << ": " << array[ccc] << '\n';

答案 2 :(得分:0)

您可以尝试这样的事情:

def get_powerset (string):
    perm = []
    if len(string) == 0:
        perm.append("")
        return perm
    first = string[0]
    print ("first = " + str(first))
    rem = string[1:len(string)]
    print ("rem = " + str(rem))
    words = get_powerset(rem)
    perm.extend(words)
    for word in words:
        perm.append(first+word)

    return perm