C++ Primer Plus第五版 第九章 编程练习答案
来源:互联网 发布:迅雷网络 股票代码 编辑:程序博客网 时间:2024/06/05 02:13
/******************************************************************************************************************* Author : Cui mingyang Blog : cx_12586 Time : 2017/10/30 From : C++ Primer Plus第五版第9章编程练习 第1题 *******************************************************************************************************************/ #include <iostream> #include "golf.h" using namespace std; int main() { golf Ann; setgolf(Ann); showgolf(Ann); setgolf(Ann, "Ann Birdfree", 24); cout << "The recorded name and handicap are: \n"; showgolf(Ann); handicap(Ann, 66); cout << "Your amended name and handicap are: \n"; showgolf(Ann); system("pause"); return 0; } #ifndef GOLF_H_ #define GOLF_H_ const int Len = 40; struct golf { char fullname[Len]; int handicap; }; void setgolf(golf &g, const char *name, int hc); int setgolf(golf &g); void handicap(golf &g, int hc); void showgolf(const golf &g); #endif #include "golf.h" #include<iostream> #include<cstring> using namespace std; void setgolf(golf &Ann, const char *name, int hc) { strcpy(Ann.fullname, name); Ann.handicap = hc; } int setgolf(golf &Ann) { cout << "Please enter your name: "; cin.getline(Ann.fullname, Len); cout << "Please enter your handicap: "; cin >> Ann.handicap; return Ann.fullname == NULL ? 0 : 1; } void handicap(golf &Ann, int hc) { Ann.handicap = hc; } void showgolf(const golf &Ann) { cout << Ann.fullname << endl; cout << Ann.handicap << endl; }
/******************************************************************************************************************* Author : Cui mingyang Blog : cx_12586 Time : 2017/10/30 From : C++ Primer Plus第五版第9章编程练习 第2题 *******************************************************************************************************************/ #include <iostream> #include <string> void strcount(const std::string &s); int main() { std::string s; while(getline(std::cin, s)) strcount(s); system("pause"); return 0; } void strcount(const std::string &s) { static int total = 0; int len = 0; for(int i = 0 ; i < (int)s.length() ; ++i) len += s[i] == ' ' ? 0 : 1; std::cout << len << std::endl; total += len; std::cout << "Total: " << total << std::endl; }
/******************************************************************************************************************* Author : Cui mingyang Blog : cx_12586 Time : 2017/10/30 From : C++ Primer Plus第五版第9章编程练习 第3题 *******************************************************************************************************************/ // newplace.cpp -- using placement new #include <iostream> #include <new> // for placement new struct chaff { char dross[20]; int slag; }; const int BUF = 512; const int N = 5; char Buffer[BUF]; // chunk of memory int main() { using namespace std; cout << "Calling new and placement new:\n"; chaff *p = new (Buffer) chaff[2]; for(int i = 0 ; i < 2 ; ++i) { std::cin.getline(p[i].dross, 20); std::cin >> p[i].slag; std::cin.get(); //吸收'\n' } for(int i = 0 ; i < 2 ; ++i) std::cout << "#" << i + 1 << std::endl << p[i].dross << std::endl << p[i].slag << std::endl; delete p; system("pause"); return 0; }
/******************************************************************************************************************* Author : Cui mingyang Blog : cx_12586 Time : 2017/10/30 From : C++ Primer Plus第五版第9章编程练习 第4题 *******************************************************************************************************************/ #include <iostream> #include "sale.h" int main() { double a[3] = {1.1, 2.2, 3.3}; SALES::Sales s; SALES::setSales(s, a, 3); SALES::showSales(s); SALES::setSales(s); SALES::showSales(s); system("pause"); return 0; } namespace SALES { const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; void setSales(Sales &s, const double ar[], int n); void setSales(Sales &s); void showSales(const Sales &s); } #include <iostream> #include "sale.h" namespace SALES { void setSales(Sales &s, const double ar[], int n) { double max = -1, min = 999999, average = 0; if(n < 4) { for(int i = 0 ; i < 4 ; ++i) { if(i < n) { s.sales[i] = ar[i]; max = max < s.sales[i] ? s.sales[i] : max; min = min > s.sales[i] ? s.sales[i] : min; average += s.sales[i]; } else s.sales[i] = 0; } s.max = max; s.min = min; s.average = average / n; } else { for(int i = 0 ; i < 4 ; ++i) { s.sales[i] = ar[i]; max = max < s.sales[i] ? s.sales[i] : max; min = min > s.sales[i] ? s.sales[i] : min; average += s.sales[i]; } s.max = max; s.min = min; s.average = average / 4; } } void setSales(Sales &s) { double max = -1, min = 999999, average = 0; for(int i = 0 ; i < 4 ; ++i) { std::cin >> s.sales[i]; max = max < s.sales[i] ? s.sales[i] : max; min = min > s.sales[i] ? s.sales[i] : min; average += s.sales[i]; } s.max = max; s.min = min; s.average = average / 4; } void showSales(const Sales &s) { for(int i = 0 ; i < 4 ; ++i) std::cout << s.sales[i] << std:: endl; std::cout << "MAX: " << s.max << std::endl << "MIN: " << s.min << std:: endl << "AVE: " << s.average << std::endl; }}
阅读全文
0 0
- c++.primer.plus第五版第九章编程练习答案
- C++ Primer Plus第五版 第九章 编程练习答案
- C++.Primer.Plus第五版第五章编程练习答案
- C Primer Plus (第五版) 第五章 编程练习 答案
- C ++ Primer Plus 第六版 第九章编程练习答案
- C primer plus(第五版)编程练习第九章
- c++.primer.plus第五版第六章编程练习答案
- c++.primer.plus第五版第七章编程练习答案
- c++.primer.plus第五版第八章编程练习答案
- c++.primer.plus第五版第十章编程练习答案
- C Primer Plus (第五版) 第六章 编程练习 答案
- C++ Primer Plus第五版 第五章 编程练习答案
- C primer plus 第五章 编程练习个人答案
- 记录——《C Primer Plus (第五版)》第九章编程练习第九题
- C++ Primer Plus第六版 第九章 编程练习答案
- C++ Primer Plus第六版 第五章 编程练习答案
- C++ Primer Plus第五版 第四章 编程练习答案
- C++ Primer Plus第五版 第三章 编程练习答案
- C++ 命名空间
- QTableView表头填满控件,表头对齐
- c++ 关键字
- ubuntu 中wget (下载)命令用法
- Python中sorted函数的用法
- C++ Primer Plus第五版 第九章 编程练习答案
- 通过邻接表实现广度优先搜索(迷宫问题)
- [svc]彻底明白centos set&env bash&source&exec执行程序本质区别
- Linux下安装jdk7
- Java IO流
- jdbcTemplate的理解及使用
- 压缩打包
- Codeforces 884D Boxes and Balls(赫夫曼编码,优先队列)
- lvm