首页 > 代码库 > c++ primer 6.2.1节练习答案

c++ primer 6.2.1节练习答案

练习6.10

源文件

 1 int main()
 2 {
 3     try                                 //尝试用下try catch
 4     {
 5         while (cin >> num1 >> num2)
 6         {
 7             if (num1 == 999)
 8                 throw runtime_error("this number may be error!");
 9             exchange_num(&num1, &num2);
10             cout << num1 << " " << num2 << endl;
11         }
12     }
13     catch (runtime_error err)
14     {
15         cout << err.what();
16     }
17     system("pause");
18     return 0;
19 }

 

头文件

1 #ifndef FIRSTHEAD_H
2 #define FIRSTHEAD_H
3 void exchange_num(int *p, int *q);
4 int num1, num2;
5 #endif // !FIRSTHEAD_H

 

函数定义

1 void exchange_num(int *p, int *q)
2 {
3     int temp;
4     temp = *q;
5     *q = *p;
6     *p = temp;
7     return;
8 }

 

c++ primer 6.2.1节练习答案