[C++ Primer Plus] 第9章、內存模型和名稱空間(二)課後習題

1、複習題

    

 2.using聲明和using編譯指令的區別

using聲明:  using std::cin;  using std::cout;  using std::endl;ios

using編譯指令:using namespace std;編程

2、編程練習

    

   

 頭文件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<string>
#include "golf.h"
//using std::cin;
//using std::cout;
//using std::endl;
using namespace std;

void setgolf(golf &g, const char * name, int hc) {
    strcpy_s(g.fullname, name);
    g.handicap = hc;
}
int setgolf(golf &g) {
    cout << "Enter name:";
    cin >> g.fullname;
    if (g.fullname[0] == '\0')
        return 0;
    cout << "Enter handicap value: ";
    while (!(cin >> g.handicap))    //若是輸入錯誤
    {
        cin.clear();
        cout << "請輸入整數:";
    }
    while (cin.get() != '\n')
        continue;
    return 1;
}
void handicap(golf &g, int hc) {
    g.handicap = hc;
}
void showgolf(const golf &g) {
    cout << "Golfer: " << g.fullname <<endl;
    cout << "Handicap: " << g.handicap <<endl;
}

main.cppui

#include<iostream>
#include<string>
#include "golf.h"
using namespace std;

const int Mems = 5;
void main()
{
    golf team[Mems];
    cout << "輸入 " << Mems << " 球隊成員:\n";
    int i;
    for (i = 0; i<Mems; i++)
        if (setgolf(team[i]) == 0)
            break;
    cout << endl;
    for (int j = 0; j<i; j++)
        showgolf(team[j]);
    setgolf(team[0], "Fred Norman", 5);
    showgolf(team[0]);
    handicap(team[0], 4);
    showgolf(team[0]);
    system("pause");
}

 二、修改程序清單9.9:用string對象代替字符數組.這樣,該程序將再也不須要檢查輸入的字符串是否過長,同時能夠將輸入字符串同字符串""進行比較,比判斷是否爲空行spa

 修改前3d

#include<iostream>
using namespace std;

const int Size=10;
void strcount(const char *str){//const表示str指針不能修改指向的內容(不過能夠指向另一塊內容)
    static int total=0;//static靜態變量,首次初始化後,其值一直存在(即第二次調用strcount函數時,total的值不會再次初始化)
    int count=0;
    cout<<"\""<<str<<"\" contains ";
    while (*str++)//先判斷*str是否爲NULL,而後再str++
        count++;
    total+=count;
    cout<<count<<" characters\n";
    cout<<total<<" characters total!\n";
}

void main() {
    char in[Size];
    char next;
    cout<<"Enter a line:"<<endl;
    cin.get(in,Size);//最多接收Size-1個字符+1個'\0'
    while (cin)    // ==while(!cin.fail()),即讀入流成功 
    {
        cin.get(next);
        while(next!='\n')    //若next不是換行符
            cin.get(next);
        strcount(in);
        cout<<"Enter next line (empty line to quit):\n";
        cin.get(in,Size);
    }
    cout<<"Bye!"<<endl;
    system("pause");
}

修改後指針

#include<iostream>
#include<string>
using namespace std;

void strcount(const string &str){
    static int total=0;//static靜態變量,首次初始化後,其值一直存在(即第二次調用strcount函數時,total的值不會再次初始化)
    int count=str.length();
    cout<<"\""<<str<<"\" contains ";
    total+=count;
    cout<<count<<" characters\n";
    cout<<total<<" characters total!\n";
}

void main() {
    string input;
    cout<<"Enter a line:"<<endl;
    getline(cin,input);
    while (""!=input)    
    {
        strcount(input);
        cout<<"Enter next line (empty line to quit):\n";
        getline(cin, input);  
    }
    cout<<"Bye!"<<endl;
    system("pause");
}

3.下面是一個結構聲明code

#include<iostream>  
#include<cstring>  
using namespace std;

const int BUF = 512;  
const int N = 2;  
char buffer[BUF];  
struct chaff   
{  
    char dross[20];  
    int slag;  
};  
  
void main()   
{  
    //使用靜態數組做爲緩衝區  
    chaff *cf = new(buffer)chaff[N];  //定位new運算符:將數組cf放在了數組buffer中
    for (int i = 0; i < N; i ++)  
    {  
        cout << "Please enter dross: ";  
        char dross[20];  
        cin.getline(dross,20);  
        strcpy_s(cf[i].dross, dross);  
        cout << "Please enter slag:";  
        cin >> cf[i].slag;  
        cin.get();  
    }  
    for (int i = 0; i < N; i++)  
        cout << cf[i].dross << " : " << cf[i].slag << endl;  
      
    //使用動態數組做爲緩衝區  
    char* buffer2 = new char[BUF];  
    chaff* cf2 = new(buffer2)chaff[N];  
    for (int i = 0; i < N; i++)  
    {  
        cout << "Please enter dross: ";  
        char dross[20];  
        cin.getline(dross, 20);  
        strcpy_s(cf2[i].dross, dross);  
        cout << "Please enter slag:";  
        cin >> cf2[i].slag;  
        cin.get();  
    }  
    for (int i = 0; i < N; i++)  
        cout << cf2[i].dross << " : " << cf2[i].slag << endl;  
    cf2 = NULL;//把這個置爲空指針  
    delete[] buffer2;//把緩衝區刪除了  

    system("pause");
}   

 sale.h頭文件orm

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

sale.cpp函數定義

#include <iostream>  
#include "sale.h"  
using namespace std;

void SALES::setSales(Sales & s, const double ar[], int n)//使用命名空間SALES後就可沒必要添加SALES::
{
    double total = 0;
    for (int i = 0; i < QUARTERS; i++)
    {
        if (i >= n)
            s.sales[i] = 0;
        else
            s.sales[i] = ar[i];
        if (i == 0)
        {
            s.max = s.sales[i];
            s.min = s.sales[i];
        }
        else
        {
            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 / QUARTERS;
}

void SALES::setSales(Sales & s)
{
    double d[QUARTERS];
    for (int i = 0; i < QUARTERS; i++)
    {
        cout << "Enter the sales:";
        cin >> d[i];
    }
    setSales(s, d, QUARTERS);
}

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

main.cpp主函數

#include<iostream>
#include "sale.h"
using namespace std;

void main() {
    double d[4] = { 123.3, 323, 342.333, 8933 };
    SALES::Sales s1, s2;
    setSales(s1, d, 4);
    setSales(s2);
    showSales(s1);
    showSales(s2);
    system("pause");
}

相關文章
相關標籤/搜索