SOJ.Subtraction

Subtraction
 
   
   

   
 
时间限制:1秒    内存限制:256兆
题目描述

某小朋友已经四岁了,她开始学习数字减法,但她还不理解负数的概念,如果被减数和减数中出现负数,或者被减数小于减数,她都不会算。

这里某人已设计函数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 ;
    }
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值