/*
https://www.hackerrank.com/challenges/30-more-exceptions/problem
*/
//Write your code here
struct MyException: public exception
{
const char* what () const throw() //const throw() means that this function won't throw any exception.
{
return "n and p should be non-negative";
}
};
class Calculator
{
public:
int power(int n, int p){
if((n<0)||(p<0)) throw MyException();
int result=1; //result = pow(n,p) works as well.
for(int i=0;i<p;i++){
result *= n;
}
return result;
}
};
댓글 없음:
댓글 쓰기