神秘博客

C++ Primer Plus [第六版]第9章编程练习题答案

1

#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 <iostream>
#include <cstring>
#include "golf.h"

void setgolf(golf &g, const char *name, int hc)
{
  strcpy_s(g.fullname, Len, name);
  g.handicap = hc;
}

int setgolf(golf &g)
{
  char temp[Len];
  std::cout << "请输入姓名: ";
  std::cin.getline(temp, Len);
  if (temp[0] == '\0')
    return 0;
  strcpy_s(g.fullname, Len, temp);
  std::cout << "请输入等级: ";
  std::cin >> g.handicap;
  std::cin.get();
  return 1;
}

void handicap(golf &g, int hc)
{
  g.handicap = hc;
}

void showgolf(const golf &g)
{
  using namespace std;
  ios_base::fmtflags temp;
  temp = cout.setf(ios_base::fixed);
  cout.setf(ios_base::left);
  cout << "姓名: ";
  cout.width(20);
  cout << g.fullname;
  cout << "    等级: ";
  cout.width(2);
  cout << g.handicap << endl;

  cout.setf(temp);
}
#include <iostream>
#include "golf.h"

int main()
{
  using namespace std;
  golf list[10];
  int count = 0;

  for (int i = 0; i < 10; i++)
  {
    cout << i + 1 << "#: \n";
    if (!setgolf(list[i]))
      break;
    count++;
  }

  for (int i = 0; i < count; i++)
  {
    if (i == 0)
      cout << "*****列表*****\n";
    showgolf(list[i]);
  }
  golf temp;
  cout << endl;
  setgolf(temp, "Ann Birdfree", 24);
  showgolf(temp);
  handicap(temp, 26);
  showgolf(temp);

  system("pause");
  return 0;
}

2

#include <iostream>
#include <string>

void strcount(const std::string * str);

int main()
{
  using namespace std;
  string input;

  cout << "Enter a line:\n";
  while (getline(cin, input) && input != "\0")
  {    
    strcount(&input);
    cout << "Enter next line (empty line to quit):\n";
  }
  cout << "Bye\n";

  system("pause");
  return 0;
}

void strcount(const std::string * str)
{
  using namespace std;
  static int total = 0;        
  int count = 0;               

  cout << "\"" << str << "\" contains ";
  count = str->size();
  total += count;
  cout << count << " characters\n";
  cout << total << " characters total\n";
}

3

#include <iostream>
#include <new>
#include <cstring>

struct chaff
{
  char dross[20];
  int slag;
};

void setchaff(chaff &p);
int main()
{
  using namespace std;
  double n[20];
  chaff *p1 = new chaff[2];
  chaff *p2 = new (n)chaff[2];
  cout << &n << endl;
  cout << p1 << " " << p2 << endl;
  for (int i = 0; i < 2; i++)
  {
    setchaff(p1[i]);
    setchaff(p2[i]);
    cout << p1[i].dross << " " << p1[i].slag << endl;
    cout << p2[i].dross << " " << p2[i].slag << endl;
  }

  delete[]p1;
  system("pause");
  return 0;
}

void setchaff(chaff &p)
{
  strcpy_s(p.dross, 20, "ps5");
  p.slag = 5;
}

4

#ifndef SALESS_H_
#define SALESS_H_

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



#endif
#include <iostream>
#include "saless.h"

namespace SALES
{
  void setSales(Sales &s, const double ar[], int n)
  {	
    double total = 0;
    s.max = 0;
    s.min = ar[0];
    for (int i = 0; i < n; i++)
    {
      s.sales[i] = ar[i];
      if (s.max < ar[i])
        s.max = ar[i];
      if (s.min > ar[i])
        s.min = ar[i];
      total += ar[i];
    }
    s.average = total / n;
  }

  void setSales(Sales &s)
  {
    using namespace std;
    double total = 0;
    for (int i = 0; i < QUARTERS; i++)
    {
      cout << "#" << i + 1 << ": ";
      cin >> s.sales[i];
      total += s.sales[i];
    }
    s.average = total / QUARTERS;
    s.max = 0;
    s.min = s.sales[0];
    for (int i = 0; i < QUARTERS; i++)
    {
      if (s.max < s.sales[i])
        s.max = s.sales[i];
      if (s.min < s.sales[i])
        s.min = s.sales[i];
    }
  }

  void showSales(const Sales &s)
  {
    using namespace std;

    for (int i = 0; i < QUARTERS; i++)
    {
      cout << "#" << i + 1 << ": ";
      cout << s.sales[i] << " ";
    }
    cout << endl;
    cout << "平均: " << s.average << endl;
    cout << "最大值: " << s.max << endl;
    cout << "最小值: " << s.min << endl;
  }
}
#include <iostream>
#include "saless.h"

int main()
{
  using namespace SALES;
  Sales p1;
  Sales p2;
  double num[QUARTERS] = { 87.5,68.4,74.2,59.8 };
  
  std::cout << "********************\n";
  setSales(p1);
  showSales(p1);
  std::cout << "********************\n";
  setSales(p2, num, QUARTERS);
  showSales(p2);

  std::cout << "完成\n";
  system("pause");
  return 0;

}

 

版权说明:
点赞
  1. 神秘说道:

    签到成功!签到时间:上午11:04:57,每日打卡,生活更精彩哦~

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏