【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;}


原创粉丝点击