c++ 第十四章第一题

#ifndef WINE_H_
#define WINE_H_
#include <iostream>
#include<string>
#include<valarray>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
    
    
public:
  Wine() {
    
    };
  ~Wine() {
    
    };
  Wine(const char* l, int y, const int yr[], const int bot[]);
  Wine(const char* l, int y);
  void GetBottles();
  void Show();
  const std::string& Label();
  int sum();
 private:
  std::string name;
  PairArray year;
  int yearnumber;
};
#endif
#include<iostream>
#include"diyi.h"
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
{
    
    
 name = l;
 yearnumber = y;
 ArrayInt a(y);
 ArrayInt b(y);
 for (int i = 0; i < y; i++)
 {
    
    
  a[i] = yr[i];
  b[i] = bot[i];
 }
 year = std::make_pair(a, b);
}
ine::Wine(const char* l, int y)
{
    
    
 name = l;
 yearnumber = y;
}
void Wine::GetBottles()
{
    
    
 std::cout << "enter " << name << " data for " << yearnumber << " year(s)\n";
 year.first.resize(yearnumber);
 year.second.resize(yearnumber);
 for (int i = 0; i < yearnumber; i++)
 {
    
    
  std::cout << "enter year: ";
  std::cin >> year.first[i];
  std::cout << "enter bottles for that year: ";
  std::cin >> year.second[i];
 }
}
void Wine::Show()
{
    
    
 std::cout << "Wine: " << name << std::endl;
 std::cout << "Year   Bottles\n";
 for (int i = 0; i < yearnumber; i++)
 {
    
    
  std::cout << year.first[i] << "   " << year.second[i] << std::endl;
 }
}
const std::string& Wine::Label()
{
    
    
 return name;
}
int Wine::sum()
{
    
    
 int num=0;
 for (int i = 0; i < yearnumber; i++)
 {
    
    
  num += year.second[i];
 }
 return num;
}
#include <iostream>
#include "diyi.h"
int main(void)
{
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    cout << "Enter name of wine: ";
    char lab[50];
    cin.getline(lab, 50);
    cout << "Enter number of years: ";
    int yrs;
    cin >> yrs;
    Wine holding(lab, yrs);
    holding.GetBottles();
    holding.Show();
    const int YRS = 3;
    int y[YRS] = {
    
     1993, 1995, 1998 };
    int b[YRS] = {
    
     48, 60, 72 };
    Wine more("Gushing Grape Red", YRS, y, b);
    more.Show();
    cout << "Total bottles for " << more.Label()
        << ": " << more.sum() << endl;
    cout << "Bye\n";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wode_0828/article/details/108740838