C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1
1 TEMPLATE 1 Template 描述 使用模板函数求最大值 使用如下 main 函数对程序进行测试 int main() { double a, b; cin >> a >> b; cout << Max(a, b) << endl; int c, d; cin >> c >> d; cout << Max(c, d) << endl; } return 0; 输入 两个小数连个整数 输出 最大小数最大整数 样例输入 10.2 9.3 9 10 样例输出 10.2 10 2
1 TEMPLATE 1 #inclu de <iostream> 2 using namespace std ; 3 4 template<c l a s s T> 5 T Max( const T &a, const T &b) 6 { 7 i f ( a>b) return a ; 8 return b ; 9 } 10 11 i n t main ( ) 12 { 13 double a, b ; 14 cin >> a >> b ; 15 cout << Max( a, b) << endl ; 16 17 i n t c, d ; 18 cin >> c >> d ; 19 cout << Max( c, d) << endl ; 20 21 return 0 ; 22 } 3
2 TEMPLATE 2 Template 描述 使用模板函数排序 使用如下 main 函数对程序进行测试 int main() { double a, b, c; cin >> a >> b >> c; Sort(a, b); cout << a << "\t" << b << endl; Sort(a, b, c); cout << a << "\t" << b << "\t" << c << endl; int d, e, f; cin >> d >> e >> f; Sort(d, e); cout << d << "\t" << e << endl; Sort(d, e, f); cout << d << "\t" << e << "\t" << f << endl; } return 0; 输入 三个小数三个整数 输出 两个排好序的小数三个排好序的小数两个排好序的整数三个排好序的整数 样例输入 -23.14 22.11-44.44 3 22 2 样例输出 -23.14 22.11-44.44-23.14 22.11 3 22 2 3 22 4
2 TEMPLATE 1 #inclu de <iostream> 2 using namespace std ; 3 4 template<c l a s s T> 5 void Sort (T &a, T &b) 6 { 7 i f ( a<b) return ; 8 T temp = a ; 9 a = b ; 10 b = temp ; 11 } 12 13 template<c l a s s T> 14 void Sort (T &a, T &b, T &c ) 15 { 16 Sort ( a, b) ; 17 Sort (b, c ) ; 18 Sort ( a, b) ; 19 } 20 21 i n t main ( ) 22 { 23 double a, b, c ; 24 cin >> a >> b >> c ; 25 Sort ( a, b) ; 26 cout << a << "\t" << b << endl ; 27 Sort ( a, b, c ) ; 28 cout << a << "\t" << b << "\t" << c << endl ; 29 30 i n t d, e, f ; 31 cin >> d >> e >> f ; 32 Sort (d, e ) ; 33 cout << d << "\t" << e << endl ; 34 Sort (d, e, f ) ; 35 cout << d << "\t" << e << "\t" << f << endl ; 36 37 return 0 ; 38 } 5
3 PERIMETER 3 Perimeter 描述 编写一个园形类 Circle, 实现半径的输入 周长的计算和输出 使用如下 main 函数对程序进行测试 ( 其中 PI=acos(-1.0)) int main() { double r; cin >> r; Circle ci(a); cout << ci.perimeter(); return 0; } 输入 半径 输出 周长 ( 小数点后两位 ) 样例输入 3.21484 样例输出 20.20 圆周率的取值需要比较精确, 以保证计算结果的精度控制精度可能需要以下代码 #include<iostream> #include<iomanip> cout<<setiosflags(ios::fixed)<<setprecision(2); 6
3 PERIMETER 1 #inclu de <iostream> 2 #inclu de <iostream> 3 #inclu de <iomanip> 4 #i n c l u d e <cmath> 5 using namespace std ; 6 7 double PI = acos ( 1.0) ; 8 c l a s s C i r c l e 9 { 10 p r i v a t e : 11 double r ; 12 public : 13 C i r c l e ( double r =0.0){ this >r=r ; } ; 14 double Perimeter ( ) { return 2*PI* r ; } 15 } ; 16 17 i n t main ( ) 18 { 19 double r ; 20 cin >> r ; 21 C i r c l e c i ( r ) ; 22 cout<<s e t i o s f l a g s ( i o s : : f i x e d )<<s e t p r e c i s i o n (2) ; 23 cout << c i. Perimeter ( ) ; 24 return 0 ; 25 } 7
4 SQUARE 4 Square 描述 编写一个正方形类 Square, 实现边长的输入 面积和周长的计算和输出 使用如下 main 函数对程序进行测试 int main() { double a; cin >> a; Square sq(a); } cout << "Area: "<< sq.area() << "\nperimeter: "<< sq.perimeter(); return 0; 输入 边长 输出 面积周长 样例输入 10.2 样例输出 Area: 104.04 Perimeter: 40.8 8
4 SQUARE 1 #inclu de <iostream> 2 using namespace std ; 3 4 c l a s s Square 5 { 6 p r i v a t e : 7 double a ; 8 public : 9 Square ( ) {a = 0. 0 ; } ; 10 Square ( double a ) { this >a=a ; } ; 11 double Area ( ) { return a*a ; } 12 double Perimeter ( ) { return 4*a ; } 13 } ; 14 15 i n t main ( ) 16 { 17 double a ; 18 cin >> a ; 19 Square sq ( a ) ; 20 21 cout << " Area: "<< sq. Area ( ) << "\ nperimeter: "<< sq. Perimeter ( ) ; 22 return 0 ; 23 } 9
5 BOOK 5 Book 题目描述 完成图书类 Book( 包含书名, 作者, 销量 ), 并用以下 main 函数测试 Book 类 int main() { Book book1; book1.print(); string name, author; int sale; getline(cin, name); getline(cin, author); cin >> sale; } Book book2(name, author, sale); book2.print(); return 0; 输入 第一行书名第二行作者名第三行销量 输出 Name: 书名, Author: 作者, Sale: 销量 Name: 书名, Author: 作者, Sale: 销量 输入样例 The Art of Computer Programming Donald Ervin Knuth 1000 输出样例 Name: no, Author: no, Sale: 0 Name: The Art of Computer Programming, Author: Donald Ervin Knuth, Sale: 1000 10
5 BOOK 1 #inclu de <iostream> 2 #include<s t r i n g > 3 using namespace std ; 4 5 c l a s s Book 6 { 7 p r i v a t e : 8 s t r i n g name ; // 9 s t r i n g author ; // 10 i n t s a l e ; // - 11 public : 12 Book ( ) 13 { 14 name = "no" ; 15 author = "no" ; 16 s a l e = 0 ; 17 } 18 Book( const s t r i n g &n, const s t r i n g &a, i n t s ) 19 { 20 name = n ; 21 author = a ; 22 s a l e = s ; 23 } 24 void Print ( ) 25 { 26 cout<<" Name: "<<name<<", " ; 27 cout<<" Author: "<<author<<", " ; 28 cout<<" Sale: "<<s a l e <<endl ; 29 } 30 ~Book ( ) {} 31 } ; 32 33 i n t main ( ) 34 { 35 Book book1 ; 36 book1. Print ( ) ; 37 38 s t r i n g name, author ; 39 i n t s a l e ; 40 g e t l i n e ( cin, name) ; 41 g e t l i n e ( cin, author ) ; 42 cin >> s a l e ; 43 44 Book book2 (name, author, s a l e ) ; 45 book2. Print ( ) ; 46 return 0 ; 47 } 11