第九章 編程練習

編程練習1

// golf.cpp
#include "golf.h"
#include <cstring>
#include <iostream>

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

int setgolf(golf & g)
{   
    cout << "Please enter the name: ";  
    if (cin.get(g.fullname, Len))
    {
        cin.get();
        cout << "Please enter the handicap: ";      
        cin >> g.handicap;
        cin.get();
        return 1;
    }
    else
        return 0;
}

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

void showgolf(const golf & g)
{
    cout << g.fullname << " take " << g.handicap <<" handicap" << endl;
}
// main.cpp
#include <iostream>
#include "golf.h"

const int ArrSize = 10;
using namespace std;

int main()
{
    golf golfarr[ArrSize];
    setgolf(golfarr[0], "xyz abc", 5);
    int set = 1;
    int num = 1;
    while (num < ArrSize)
    {
        set = setgolf(golfarr[num]);
        if (set ==1)
            num++;
        else
            break;
    }

    cout << "show all golfer" << endl;
    for (int i =0; i < num; i++)
    {
        showgolf(golfarr[i]);
    }
    cout << "revalue handicap and show all golfer" << endl;
    for (int i =0; i < num; i++)
    {
        handicap(golfarr[i], i+1);
        showgolf(golfarr[i]);
    }

    system("pause");
    return 0;
}

編程練習2

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

void strcount(string str);

int main()
{
    string input;
    cout << "Enter a line: " << endl;
    getline(cin,input);
    while(input !="")
    {
        strcount(input);
        cout << "Enter next line (empty line to quite)" << endl;
        getline(cin,input);
    }
    cout << "Bye" << endl;
    system("pause");
    return 0;
}

void strcount(string str)
{
    static int total = 0;
    int count =0;

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

編程練習3

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

struct chaff{  
    char dross[20];  
    int slag;  
};  
const int Size=512;  
const int ArSize=2;  
char Buf[Size];  

int main()  
{  
    chaff *p;  
    p=new (Buf) chaff[ArSize];
    for(int i = 0; i < ArSize; i++)  
    {  
        strcpy(p[i].dross,"xyz");  
        p[i].slag = (i+1)*10;  
    }    
    for(int i = 0; i < ArSize; i++)  
    {  
        cout << "p[" << i << "] adreess is "<< &p[i] << " and valus is " << p[i].dross << endl;  
        cout << p[i].slag << endl;  
    }  
    system("pause");  
    return 0;  
} 

編程練習4

//sales.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);
}
//sales.cpp
#include "SALES.h"  
#include <iostream> 

static int count=0;
using namespace std; 
namespace SALES  
{  

    void setSales(Sales &s,const double ar[],int n)
    {
        int num = 0;
        s.max = 0;
        s.min = 0;
        double total = 0;
        while(num < n && num < QUARTERS)
        {
            s.sales[num] = ar[num];

            if (num == 0)
            { 
                s.max = s.sales[num];
                s.min = s.sales[num];
            }
            else {
                if (s.max < s.sales[num])
                    s.max = s.sales[num];
                if (s.min > s.sales[num])
                    s.min = s.sales[num];
            }
            total += ar[num];
            num++;  
        }
        s.average = total/num;
    }

    void setSales(Sales & s)
    {
        double total = 0;
        for (int i = 0; i < QUARTERS; i++)
        {
            cout << "Please enter the sales for Quater " << i+1 << " : ";
            cin >> s.sales[i];
            if (i == 0)
            { 
                s.max = s.sales[i];
                s.min = s.sales[i];
            }
            else {
                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];
        }
        s.average = total/QUARTERS;
    }
    void showSales(const Sales &s)
    {
        for (int i = 0; i < QUARTERS; i++)
        {
            if (s.sales[i] >0)
            cout << "The sales for Quater " << i+1 << " is  " << s.sales[i] << endl;
        }
        cout << "The max sales is  " << s.max<< endl;
        cout << "The min sales is  " << s.min<< endl;
        cout << "The average sales is  " << s.average<< endl;
    }

}
//main.cpp
#include "sales.h"
#include <iostream>

int main()
{
    using namespace SALES;
    Sales s1, s2;

    double ar[3] = {1000, 2000, 3000};
    setSales(s1, ar, 3);
    setSales(s2);

    showSales(s1);
    showSales(s2);
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章