1. 程式人生 > >c++ primer plus第九章程式設計答案

c++ primer plus第九章程式設計答案

//程式設計練習1(多個檔案)
//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);

//golf.cpp

#include <iostream>
#include <cstring>//字元陣列多用裡面的函式,陣列之間是不允許賦值的
#include "golf.h"
using namespace std;

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

    cout << "Enter fullname: ";
    cin.get(g.fullname,Len);
      if (strlen(g.fullname)==0)//利用strlen來表示陣列是否為空陣列
      {
          return 0;
      }
    cout << "Enter handicap: ";
    cin >> g.handicap;

    return 1;
}
void handicap(golf & g, int hc)
{
    g.handicap = hc;
}
void showgolf(const golf & g)
{
       cout << "\nfullname : " << g.fullname << endl;
      cout << "handicapL : " << g.handicap << endl;
}


//main.cpp


#include <iostream>
#include "golf.h"

using namespace std;

int main()
{
    golf g[10];
    int i=0;
    while ( i<10 && setgolf(g[i]) )
    {
        i++;
        cout << "Next:\n";
        cin.get();
    }
    cout <<"\ninput end!\n\n";
    handicap(g[0],0);
    setgolf(g[2],"girl",3);
    for(int j=0;j<i;j++)//顯示的個數要注意
            showgolf(g[j]);
    cout << "END!\n\n";

    return 0;
}
//程式設計練習2
#include <iostream>
#include <string>

using namespace std;

void strcount(const string str);

int main()
{
    string input;
    cout << "Enter a line:\n";
    getline(cin, input);
    while (cin)
    {
        strcount(input);
        cout << "Enter next line (empty line to quit):\n";
        getline(cin, input);
        if(input == "")//空行
        {
            break;
        }
    }
    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";
}
//程式設計練習3(定位new運算子)
#include<iostream>
#include <cstring>
#include<new>//包含new標頭檔案
struct chaff
{
    char dross[20];
    int slag;
};

using namespace std;
void getchaff(chaff &);
void showchaff(const chaff &);

int main()
{
    chaff cs[2];
    chaff *ps = new (cs) chaff[2];//定位new運算子
     chaff *pb = new chaff[2];
    cout << "cs[0]'s address : " << (void *)cs << endl;
    cout << "ps address: " << ps <<endl;
    cout << "pb address : " << pb << endl;
    for (int i = 0; i < 2 ;i++)
    {
        getchaff(cs[i]);
        cin.get();
    }
     for (int j = 0; j < 2 ;j++)
    {
        showchaff(cs[j]);
    }

    for (int  i = 0; i < 2 ;i++)
    {
        getchaff(*(pb+i));//引數是結構
        cin.get();
    }
     for ( int j = 0; j < 2 ;j++)
    {
        showchaff(*(pb+j));
    }

        delete [] ps;
        delete [] pb;


    return 0 ;
}
void getchaff(chaff & cs)
{
    char input[20];
    cout << "Enter dross : ";
    cin.getline(input,20);
        strcpy(cs.dross,input);
        cout << "Enter slag : ";
        cin >> cs.slag;

}
void showchaff(const chaff &cs)
{
    cout << "dross : " << cs.dross <<endl;
    cout << "salg : " << cs.slag << endl;
}
//程式設計練習4(名稱空間)
//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);
}

//main.cpp
#include <iostream>
#include "sale.h"

int main()
{
        double arr[3] = {34,56.4,456.5};
        using SALES::Sales;
        using SALES::setSales;
        using SALES::showSales;
        Sales st1,st2;
        setSales(st1,arr,3);
        std::cout << "ONE: \n";
        showSales(st1);
        setSales(st2);
        std::cout << "TWO: \n";
        showSales(st2);
    return 0 ;
}


//sales.cpp
#include <iostream>
#include <cstring>
#include "sale.h"

namespace SALES
{
    void setSales(Sales & s,const double ar[],int n )
    {
        int min_num = 4;
        if(min_num > n)
        {
            min_num = n;
            std::cout <<"array not full!\n";
            for (int i = n;i < 4 ;i++)
            {
                s.sales[i] = 0.0;
            }
        }
        double sum = 0.0;
        s.max = ar[0];
         s.min = ar[0];
        for (int i = 0; i < min_num;i++)
        {
            s.sales[i] = ar[i];
            sum += s.sales[i];
            if( s.max < s.sales[i])
            {
                s.max = s.sales[i];
            }
            if(s.min > s.sales[i])
            {
                s.min = s.sales[i];
            }
        }
        s.average = sum / min_num ;
    }

    void setSales(Sales & s)
    {
        std::cout << "Enter number into sales: ";
        int i = 0 ;
        double sum = 0.0;
        while( i<4 && (std::cin >> s.sales[i] ))
        {
            sum += s.sales[i];
            i++;
            if(i!=4)
            std::cout << "next: ";
        }

        s.max = s.sales[0];
        s.min = s.sales[0];
        for (int j = 0;j<4;j++)
        {
            if(s.sales[j]>s.max)
               {
                  s.max =s.sales[j];
               }
              else  if (s.sales[j]<s.min)
                {
                    s.min = s.sales[j];
                }
        }
         s.average = sum / 4 ;
    }

    void showSales(const Sales & s)
    {
        std::cout << "sales[QUARTERS]: ";
        for(int i = 0;i<QUARTERS;i++)
        {
            std::cout << "sales[" << i << "]: ";
        std::cout << s.sales[i] << "  ";
        }
        std::cout << "\naverage: " << s.average << std::endl;
        std::cout << "max: " << s.max << std::endl;
        std::cout << "min: " << s.min << std::endl;

    }
}