Subtraction
| ||
题目描述
某小朋友已经四岁了,她开始学习数字减法,但她还不理解负数的概念,如果被减数和减数中出现负数,或者被减数小于减数,她都不会算。 这里某人已设计函数calc做减法计算前进行了检查。
int calc(int a, int b) throw(logic_error) { if (a<0) throw out_of_range("Out of range exeception"); else if (b<0) throw out_of_range("Out of range exeception"); else if (a<b) throw logic_error("Minuend smaller than subtrahend"); return a-b; }
请写一个test函数可以正确调用函数calc,test函数定义如下: void test(int, int);
主函数也已给出如下: #include <iostream> #include <stdexcept> using namespace std;
int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; test(a, b); } return 0; }
样例输入
4 3 1 -3 1 1 -3 1 3
样例输出
2 Out of range exception Out of range exception Minuend smaller than subtrahend
提示
请只提交test函数,不要提交calc函数。 | ||
void test(int a , int b)
{
try
{
int ans = calc(a , b) ;
cout << ans << endl ;
}
catch(out_of_range &orange)
{
cout << orange.what() << endl;
}
catch(logic_error &loer)
{
cout << loer.what() << endl ;
}
}