抛出异常

时间:2021-04-03 03:00:18

标签: c++

程序要求 try/catch 必须放在 main.cpp 中,如下所示:

    cout << "printing the array element by element using: int getElement(int);" << endl;
   cout << "(going one too far to test out of range)" << endl;
   for(int i=0; i<=LISTSIZE; i++){
      try{
         elementResult = mylist.getElement(i);
         cout << elementResult << endl;
         } catch(int e){
         cout << "Error: Index out of range." << endl;
      }
   }
   cout << endl;

当它访问方法时:

int MyList::getElement(int passedIndex){
    if((passedIndex < 0) || (passedIndex > length -1)){
        throw 0;
    }
    return array[passedIndex];
}

我使用哪种投掷变体似乎无关紧要,之后我的数组就会被破坏。如果它保持在边界内,它就可以正常工作,或者我工作它不会从方法中抛出(在其他地方进行错误检查),但是要求声明它必须是那样的,所以我必须遗漏一些东西。完整代码如下:

main.h:

#ifndef MAIN_H
#define MAIN_H

/***********************************
 * DO NOT MODIFY THIS FILE OTHER THAN
 * TO ADD YOUR COMMENT HEADER
 ***********************************/

#include <iostream>     /* cout, endl */
#include "mylist.h"
#include <stdexcept>

#define LISTSIZE 10

using std::cout;
using std::endl;

int elementResult;

#endif /* MAIN_H */

main.cpp:

#include "main.h"

int main(int argc, char** argv) {
    /***********************************
     * DO NOT MODIFY THIS FILE OTHER THAN
     * TO ADD YOUR COMMENT HEADER AND
     * UNCOMMENT THINGS AS YOU COMPLETE
     * THE FUNCTIONALITY OF YOUR LIST OBJECT
     ***********************************/

    /* This will create a "list" of size LISTSIZE
     * and initialize it to all zeros */
    cout << "create and initialize mylist" << endl;
    MyList mylist(LISTSIZE);
    mylist.printArray();
    cout << endl;

    /* This will set the list to all 50 */
    cout << "set mylist to all 50" << endl;
    mylist.setArray(50);
    mylist.printArray();
    cout << endl;

    /* This will fail and set the array to the
     * default random 1-10 values */
    cout << "attempt to set to random numbers -2 to 4" << endl;
    mylist.setRandom(-2,4);
    mylist.printArray();
    cout << endl;

    /* This will fail and set the array to the
     * default random 1-10 values */
    cout << "attempt to set to random numbers 4 to 4" << endl;
    mylist.setRandom(4,4);
    mylist.printArray();
    cout << endl;

    /* This will succeed and set the array to the
     * random 1-100 values */
    cout << "attempt to set to random numbers 1 to 100" << endl;
    mylist.setRandom(1,100);
    mylist.printArray();
    cout << endl;

    /* This will succeed and set the array to the
     * random 500-1000 values */
    cout << "attempt to set to random numbers 500 to 1000" << endl;
    mylist.setRandom(1000,500);
    mylist.printArray();
    cout << endl;

    /* These next two sets will succeed and set the 1st and last
     * elements to 1000 and 2000 respectively */
    if(mylist.setElement(1000, 0)){
       cout << "Element Set" << endl;
    } else {
       cout << "Element NOT Set" << endl;
    }
    if(mylist.setElement(2000, LISTSIZE-1)){
       cout << "Element Set" << endl;
    } else {
       cout << "Element NOT Set" << endl;
    }
    mylist.printArray();
    cout << endl;

    /* These next two sets will fail and leave the array unmodified */
    if(mylist.setElement(9999, -1)){
       cout << "Element Set" << endl;
    } else {
       cout << "Element NOT Set" << endl;
    }
    if(mylist.setElement(9999, LISTSIZE)){
       cout << "Element Set" << endl;
    } else {
       cout << "Element NOT Set" << endl;
    }
    mylist.printArray();
    cout << endl;

    cout << "Testing new and/or modified code..." << endl << endl;

   cout << "printing the array element by element using: int getElement(int);" << endl;
   cout << "(going one too far to test out of range)" << endl;
   for(int i=0; i<=LISTSIZE; i++){
      try{
         elementResult = mylist.getElement(i);
         cout << elementResult << endl;
         } catch(int e){
         cout << "Error: Index out of range." << endl;
      }
   }
   cout << endl;
   mylist.printArray();

   cout << "attempting to get element 4000 using: int getElement(int);" << endl;
      try{
         cout << mylist.getElement(4000) << endl;
      } catch(int e){
         cout << "Error: Index out of range." << endl;
      }
   cout << endl;

   cout << "printing the array element by element using: int getElement(int,int*);" << endl;
   cout << "(going one too far to test out of range)" << endl;
   for(int i=0; i<=LISTSIZE; i++){
      if(mylist.getElement(i, &elementResult)){
         cout << elementResult << endl;
      } else {
         cout << "Error: Index out of range." << endl;
      }
   }
   cout << endl;

   cout << "attempting to get element 4000 using: int getElement(int,int*);" << endl;
   if(mylist.getElement(4000, &elementResult)){
      cout << elementResult << endl;
   } else {
      cout << "Error: Index out of range." << endl;
   }

    return 0;
}

mylist.h:

#ifndef MYLIST_H
#define MYLIST_H

#include <iostream>     /* cout, endl */
#include <stdlib.h>     /* srand, rand, atoi */
#include <time.h>       /* time */
#include <stdexcept>
// you can add libraries if you need them, but you shouldn't

// DO NOT MODIFY THESE DEFINES
#define RMIN 1
#define RMAX 10
#define DEFAULT_SIZE 10

using std::cout;
using std::endl;

class MyList {
public:
    // DO NOT MODIFY THESES NEXT TWO
    MyList(int); // constructor
    ~MyList(); // destructor

    int getElement(int);
    void setArray(int); 
    bool setElement(int, int); 
    void setRandom(int, int);
    void printArray();
    bool getElement(int, int*);

private:
    // these are the only attributes allowed
    // DO NOT ADD OR MODIFY THEM
    int length;
    int *array;
};

#endif //MYLIST_H

mylist.cpp:

#include "mylist.h"

// constructor
MyList::MyList(int size) {
    srand(time(NULL));  // call only once!
    if(size < 1){
        size = DEFAULT_SIZE;
    }
    MyList::length = size;
    MyList::array = new int(size);
    setArray(0);

    }

// destructor
MyList::~MyList() {
    //delete[] MyList::array;
}

void MyList::printArray() {
    cout << "[";
    for (int i = 0; i < length; i++){
        if (i == length - 1){
            cout << array[i];
        }else{
            cout << array[i] << " ";
        }
        
    }
    cout << "]" << endl;
}

void MyList::setArray(int setArrayTo){
    for (int i = 0; i < length; i++){
        MyList::array[i] = setArrayTo;

    }
}

void MyList::setRandom(int numOne, int numTwo){
    bool isValidRandom = true;
    int randMin, randMax;
    if((numOne < RMIN) || (numTwo < RMIN) || (numOne == numTwo)){ isValidRandom = false; }
    if(isValidRandom == true){
        if(numTwo < numOne){
            randMin = numTwo;
            randMax = numOne;
        } else {
            randMin = numOne;
            randMax = numTwo;
        }
    } else {
        randMin = RMIN;
        randMax = RMAX;
    }
    for(int i = 0;i < length; i++){
        MyList::array[i] = rand() % randMax + randMin; 
    }

}

bool MyList::setElement(int passedValue, int arrayIndex){
    bool isInRange = true;
    if ((arrayIndex < 0)||(arrayIndex > length - 1)){
        isInRange = false;
    }
    if (isInRange == true){
        MyList::array[arrayIndex] = passedValue;
    }
    return isInRange;
}

int MyList::getElement(int passedIndex){
    if((passedIndex < 0) || (passedIndex > length -1)){
        throw 0;
    }
    return array[passedIndex];
}

bool MyList::getElement(int passedIndex, int *iPtr){
    bool isItValid = true;
    if((passedIndex >= 0) && (passedIndex < length)){
        *iPtr = MyList::array[passedIndex];
    } else {
        isItValid = false;
    }
    return isItValid;
}

Output

0 个答案:

没有答案
相关问题