【14.1】c++ primer plus 课后编程答案
来源:互联网 发布:王健林天津 知乎 编辑:程序博客网 时间:2024/06/07 03:14
C++ PRIMER PLUS 课后答案
使用IDE为window7系统下的VS2010
/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>#include <valarray>#include <iostream>using std::ostream;using std::istream;using std::string;template <typename T1, typename T2>class Pair{private:T1 year;T2 Bottoles;public:T1 & yearss() {return year;}T2 & Bottoless(){return Bottoles;}T1 yearss() const {return year;}T2 Bottoless() const {return Bottoles;}Pair(const T1 & AVL1, const T2 & AVL2) : year(AVL1), Bottoles(AVL2){}Pair(){}};class Wine{private:typedef std::valarray<int> ArryInt;typedef Pair<ArryInt, ArryInt> PairArry;string name;int wineYear;PairArry temp;public:Wine() : name("None"),wineYear(0),temp(ArryInt(0), ArryInt(0)){}Wine(const char * l, int y, const int yr[], const int bot[]);Wine(const char * l, const ArryInt & yr, const ArryInt & bot);Wine(const char * l, const PairArry & yr_bot);Wine(const char * l, int y);~Wine(){};void GetBotts();void show() const;const string & Name() {return name;}int sum() {return temp.Bottoless().sum();}};#endif
/*userfucntion.cpp*/#include "usersh.h"#include <iostream>#include <cstring>using std::cout;using std::cin;using std::endl;using std::string;using std::ostream;using std::istream;Wine::Wine(const char * l, int y, const int yr[], const int bot[]): name(l), wineYear(y),temp(ArryInt(yr, y), ArryInt(bot, y)){}Wine::Wine(const char * l, const ArryInt & yr, const ArryInt & bot): name(l), wineYear(yr.size()), temp(ArryInt(yr), ArryInt(bot)){if (yr.size() != bot.size()){cout << "bottle data mismach, array set to 0:\n";wineYear = 0;temp = PairArry(ArryInt(), ArryInt());}else{temp.yearss() = yr;temp.Bottoless() = bot;}}Wine::Wine(const char * l, const PairArry & yr_bot): name(l), wineYear(yr_bot.yearss().size()), temp(yr_bot){}Wine::Wine(const char * l, int y): name(l), wineYear(y), temp(ArryInt(0, y), ArryInt(0, y)){}void Wine::GetBotts(){if (wineYear < 1){cout << "No space to save. \n";return;}cout << "enter " << name << "data for "<< wineYear << " years. \n";for(int i = 0; i < wineYear; i++){cout << "Enter years:";cin >> temp.yearss()[i];cout << "Enter bottles for that years: ";cin >> temp.Bottoless()[i];}}void Wine::show() const{cout << "Wine: " << name <<endl;cout << "\tyears\tbottles\n";for (int i = 0; i < wineYear; i++){cout << '\t' << temp.yearss()[i] << '\t' << temp.Bottoless()[i] << endl;}}
/*main*/#include <iostream>#include <Windows.h>#include <cstring>#include "usersh.h"using std::cout;using std::cin;using std::endl;const int NUM = 10;int main(){ cout << "Enter name of wine: ";char lab[50];cin.getline(lab, 50);cout << "Enter num of years: ";int yrs;cin >> yrs;Wine holding(lab, yrs);holding.GetBotts();holding.show();const int YRS = 3;int y[YRS] = {1993, 1995, 1998};int b[YRS] = {40, 60, 80};Wine more("Red wine.", YRS, y, b);more.show();cout << "total bottle for " << more.Name() << ": " << more.sum() << endl;cout << "Bye!\n";system("pause");return 0;}
阅读全文
0 0
- C++primer plus第六版课后编程题答案14.1
- C++primer plus第六版课后编程练习答案14.1
- 【14.1】c++ primer plus 课后编程答案
- C++PRIMER PLUS第六版课后编程答案 4.1-4.5
- C++PRIMER PLUS第六版课后编程答案 4.6-4.10
- C++PRIMER PLUS第六版课后编程答案 5.1-5.5
- C++PRIMER PLUS第六版课后编程答案 5.6-510
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- 分库分表组件TDDL
- LTE相关资料整理
- 什么是真正的产品经理
- 这20年我做了些什么
- windows下python安装、gensim安装
- 【14.1】c++ primer plus 课后编程答案
- svn trunk branch 分支
- 【原创】运算放大器常用参数翻译
- 系统扩展方式 scale up和scale out
- IC设计基础系列之CDC篇3:揭秘《跨时钟域处理》三大方法
- Linux基础——操作系统框架
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- Redux总结1:Redux基础
- Spring Boot对Ajax跨域访问的支持实现