C++ primer plus 练习9

来源:互联网 发布:数学理科优化重组卷 编辑:程序博客网 时间:2024/06/04 18:49


9.1

 //golf.h
#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

 

//golf.cpp
#include"golf.h"
#include<iostream>
using namespace std;
#include<cstring>
void setgolf(golf & g, const char * name, int hc)
{
 strcpy_s(g.fullname, strlen(name) + 1, name);
 g.handicap = hc;
}
int setgolf(golf&g)
{

 cout << "Enter your name: \n";
 cin.getline( g.fullname, Len);
 cout << "Enter your handicap: \n";
 cin >> g.handicap;
 cin.get();
 if (g.fullname == "")  return 0;
 return 1;
}
void handicap(golf & g, int hc)
{
 g.handicap = hc;
}
void showgolf(const golf & g)
{
 cout << "Name : " << g.fullname << endl;
 cout << "Handicap : " << g.handicap << endl;
}

 

//pe9 -1.cpp
#include<iostream>
#include"golf.h"
using namespace std;
#include<cstring>
int main()
{
 golf g;
 char  name[Len];
 int han;
 int n = 4;
 for (int i = 0; i < n; i++)
 {
  cin.getline(name, Len);
  cin >> han;
  cin.get();
  if (strcmp(name,"")== 0) break;
  setgolf(g, name, han);
  showgolf(g);
 }
}

 

 

9.2

#include<iostream>
#include<cstring>
using namespace std; // std 作全局声明不然报string未定义错误 之后就无需申明std 故可将其屏蔽掉
void strcount(const string str);

int main()
{
 string input;
 cout << "Enter a line:\n";
 getline(cin, input);
 while (input != "")
 {
  strcount(input);
  cout << "Enter next line (empty line to quit): \n";
  getline(cin, input);
 }
 cout << "Bye\n";
 return 0;
}
void strcount(const string str)
{
 static int total = 0;
 int count = 0;

 cout << "\"" << str << "\"contains ";
 count = str.length();
 total += count;
 cout << count << " characters\n";
 cout << total << " characters total\n";
}

 

 9.3

#include<iostream>
using namespace std;
struct chaff
{
 char dross[20];
 int slag;
};
#include<cstring>
const int BUF = 900;
char buffer[BUF];
int main()
{
 chaff *a, *b;
 a = new chaff[2];
 b = new (buffer)chaff[2];
 int i;
 strcpy_s(a[0].dross, 20, "Merry");
 a[0].slag = 5;
 strcpy_s(a[1].dross, 20,"Christmas" );
 a[1].slag = 3;
 strcpy_s( b[0].dross, 20,"You find");
 b[0].slag = 2;
 strcpy_s(b[1].dross, 20,"the code" );
 b[1].slag = 1;
 for (i = 0; i < 2; i++)
 {
  cout << "A[" << i << "]: \ndross : " << a[i].dross
   << "\nslag : " << a[i].slag << endl;
  cout << "B[" << i << "]: \ndross : " << b[i].dross
   << "\nslag : " << b[i].slag << endl;
 }
 return 0;
}

 

9.4

//sale.h
#ifndef SALE_H_
#define SALE_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

 

//sales.cpp
#include"sale.h"
#include<iostream>
using namespace std;
namespace SALES
{
 void setSales(Sales & s, const double ar[], int n)
 {
  int i;
  double total = 0;
  s.max = -999;
  s.min = 999;
  if (n > 4) n = 4;
  for (i = 0; i < n; i++)
  {
   s.sales[i] = ar[i];
   if (s.max < s.sales[i]) s.max = s.sales[i];
   if (s.min > s.sales[i]) s.min = s.sales[i];
   total += s.sales[i];
  }
  if (n < 4)
  {
   for (; i < 4; i++)
    s.sales[i] = 0;
   if (s.min > 0) s.min = 0;
   if (s.max < 0) s.max = 0;
  }
  s.average = total / 4;
 }
 void setSales(Sales & s)
 {
  int i;
  double total = 0;
  s.max = -999;
  s.min = 999;
  cout << "Sales : \n";
  for (i = 0; i < 4; i++)
  {
   cout << i << " : ";
   cin >> s.sales[i];
   if (s.sales[i]>s.max)
    s.max = s.sales[i];
   if (s.sales[i] < s.min)
    s.min = s.sales[i];
   total += s.sales[i];
  }
  s.average = total / 4;

 }
 void showSales(const Sales & s)
 {
  for (int i = 0; i < 4; i++)
   cout << s.sales[i] << endl;
  cout << "Average: " << s.average << endl;
  cout << "Max: " << s.max << endl;
  cout << "Min: " << s.min << endl;
 }
}

//main.cpp
#include<iostream>
using namespace std;
#include"sale.h"
int main()
{
 using namespace SALES;
 Sales s;
 double ar[4] = { 3.3, 2.2, 1.1, 4.4 };
 double ar2[3] = { 2.2, 3.1, 1.1 };
 setSales(s, ar, 4);
 showSales(s);
 setSales(s, ar2, 3);
 showSales(s);
 setSales(s);
 showSales(s);
 return 0;
}

 

0 0
原创粉丝点击