(一一四)第九章程式設計練習
1.下面是一個頭檔案:
// golf.h -- for pe9-1.cpp
const int len = 40;
struct golf
{
char fullname[len];
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf (golf & g, const char * name, int hc);
// interactive version:
// functionsolicits name and handicap from user
// and sets the member of g to the values enterd
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);
//function resets handicap to new value
void handicap (golf & g, int hc);
//function displays contents of golf structure
void showgolf(const golf & g);
注意到setgolf()被過載,可以使用其第一個版本:
golf ann;
setgolf(ann, "Ann Birdfree", 24);
上述函式呼叫提供了儲存在ann結構中的資訊。可以這樣使用其第二個版本;
golf andy;
setgolf(andy);
上述函式將提示使用者輸出姓名和等級,並將他們儲存在andy結構中。這個函式可以(但不一定必須)在內部使用第一個版本
根據這個標頭檔案,建立一個多檔案程式,其中一個檔名為golf.cpp,他提供了與標頭檔案中的原型相匹配的函式定義;另一個檔案應包含main(),並演示原型化函式的所有特性。例如,包含一個讓使用者輸入的迴圈,並使用輸入的資料來填充一個由golf
答:
//golf.h
#pragma once
const int len = 40;
struct golf
{
char fullname[len];
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
//意思是結構的兩個值,根據傳遞的兩個引數來賦值
void setgolf(golf & g, const char * name, int hc);
// interactive version:
// function solicits name and handicap from user
// and sets the member of g to the values enterd
// returns 1 if name is entered, 0 if name is empty string
//函式請求結構的兩個成員的資料,如果有,則返回1,如果是空的,返回0
int setgolf(golf & g);
//function resets handicap to new value
//函式更改結構的handicap成員的值為傳遞的引數
void handicap(golf & g, int hc);
//function displays contents of golf structure
//函式顯示結構成員的內容
void showgolf(const golf & g);
//1.cpp
#include<iostream>
#include"golf.h"
int main()
{
using namespace std;
golf mm[10];
cout << "請輸入姓名和一個人的等級。若輸入在姓名輸入空字串時(在輸入名字的時候直接按回車鍵),則結束輸入" << endl;
cout << "1# 姓名:";
cin.getline(mm[0].fullname, 39);
cout << "等級:";
cin >> mm[0].handicap;
int i;
cin.sync();
for (i = 1;setgolf(mm[i - 1]);i++)
{
cout << i+1<<"# 姓名:";
cin.getline(mm[i].fullname, 39);
cout << "等級:";
cin >> mm[i].handicap;
cin.sync();
}
cout << "輸入結束。" << endl << endl;
if (!setgolf(mm[0]))
{
cout << "由於你並沒有輸入資料,程式結束。" << endl;
system("pause");
return 0;
}
cout << "現在顯示輸入結果:" << endl;
for (i = 0;i < 10 && setgolf(mm[i]);i++)
{
showgolf(mm[i]);
}
cout << endl;
cout << "現在將1#的等級改為200" << endl;
handicap(mm[0], 200);
showgolf(mm[0]);
system("pause");
return 0;
}
//golf.cpp
#include<iostream>
#include"golf.h"
void setgolf(golf & g, const char * name, int hc)
{
int i;
for (i = 0;name[i] == '\0';i++)
g.fullname[i] = name[i];
g.fullname[i] = '\0';
g.handicap = hc;
}
int setgolf(golf & g)
{
if (g.fullname[0] == '\0')return 0;
else return 1;
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "名字為:" << g.fullname << ", 等級為:" << g.handicap << std::endl;
}
2.修改程式清單9.9,用string物件代替字元陣列。這樣,該程式將不再需要檢查輸入的字串是否過長,同時可以將輸入字元串同字串""進行比較,以判斷是否為空行。
答:
#include<iostream>
#include<string>
void strcount(const std::string str);
int main()
{
using namespace std;
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";
system("pause");
return 0;
}
void strcount(const std::string str)
{
using namespace std;
static int total = 0;
int count = str.length();//string類名.length()返回的是字串長度。也可以用str.size(),效果是一樣的
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
3.下面是一個結構宣告:
struct chaff
{
char dross[20];
int flag;
};
編寫一個程式,使用定位new運算子將一個包含兩個這樣的結構的陣列放在一個緩衝區中。然後,給結構的成員賦值(對於char陣列,使用函式strcpy()),並使用一個迴圈來顯示內容。一種方法是像程式清單9.10那樣將一個靜態陣列用作緩衝區;另一種方法是使用常規new運算子來分配緩衝區。
答:
#include<iostream>
struct chaff
{
char dross[20];
int flag;
};
//編寫一個程式,使用定位new運算子將一個包含兩個這樣的結構的陣列放在一個緩衝區中。然後,給結構的成員賦值(對於char陣列,使用函式strcpy()),並使用一個迴圈來顯示內容。一種方法是像程式清單9.10那樣將一個靜態陣列用作緩衝區;另一種方法是使用常規new運算子來分配緩衝區。
int main()
{
using namespace std;
chaff pp[5];
chaff*x = new(pp)chaff[2]; //定位new運算子,把兩個這樣的結構放在pp的位置
char *mm=new char[20];
for (int i = 0;i < 2;i++)
{
cout << i + 1 << "# dross: ";
cin.getline(mm, 19);
strcpy_s(x[i].dross,mm); //將輸入的複製進去
cout << "flag: ";
cin >> x[i].flag;
cin.sync();
}
for (int i = 0;i < 2;i++)
{
cout << "dross:" << x[i].dross;
cout << ", flag: " << x[i].flag << endl;
}
system("pause");
return 0;
}
4.請基於下面這個名稱空間編寫一個由3個檔案組成的程式:
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales(Sales & s);
//display all information in structures s
void showSales(const Sales & s);
}
第一個檔案是一個頭檔案,其中包含名稱空間;第二個檔案是一個原始碼檔案,它對這個名稱空間進行擴充套件,以提供這三個函式的定義;第三個檔案宣告兩個Sales物件,並使用setSales()的互動式版本為一個結構提供值,然後使用setSales() 的非互動式版本為另一個結構提供值。另外它還是用showSales()來顯示這兩個結構的內容。
答:
//1.h
#pragma once
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
//從ar數組裡複製n或者4(取較小的)個成員到s結構中的sales成員。然後計算最大最小和平均。然後把sales成員剩下的成員,設定為0
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n);
//收集四季度的銷售情況,儲存他們進s的sales成員,然後計算,並存儲到平均、最大、最小值
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales(Sales & s);
//顯示結構中的所有資訊
//display all information in structures s
void showSales(const Sales & s);
}
//1.cpp
#include<iostream>
#include"1.h"
int main()
{
using namespace std;
using namespace SALES;
Sales name[2];
double x[3] = { 1.1,5.5,3.3 };
setSales(name[0], x, 3);
setSales(name[1]);
for (int i = 0;i < 2;i++)
showSales(name[i]);
system("pause");
return 0;
}
//2.cpp
#include<iostream>
#include"1.h"
namespace SALES //把函式定義新增到名稱空間之中
{
void setSales(Sales & s, const double ar[], int n)
{
int i;
for (i = 0;i < n && i < 4;i++) //賦值
s.sales[i] = ar[i];
for (int j = i;j < 4;j++) //將未賦值的設定為0
s.sales[j] = 0;
double total = 0;
for (int j = 0;j < i;j++) //total為所有有效值的總和
total += s.sales[j];
s.average = total / i; //設定平均值
s.max = s.min = s.sales[0]; //最大最小值初始化為第一個值
for (int j = 0;j < i;j++) //設定最大最小值
{
if (s.sales[j] > s.max)s.max = s.sales[j];
if (s.sales[j] < s.min)s.min = s.sales[j];
}
}
void setSales(Sales & s)
{
for (int i = 0;i < 4;i++)
{
s.sales[i] = rand() % 20 + 1;
}
int i = 4;
double total = 0;
for (int j = 0;j < i;j++) //total為所有有效值的總和
total += s.sales[j];
s.average = total / i; //設定平均值
s.max = s.min = s.sales[0]; //最大最小值初始化為第一個值
for (int j = 0;j < i;j++) //設定最大最小值
{
if (s.sales[j] > s.max)s.max = s.sales[j];
if (s.sales[j] < s.min)s.min = s.sales[j];
}
}
void showSales(const Sales & s)
{
using namespace std;
cout << "輸出:" << endl;
for (int i = 0;i < 4 && s.sales[i] != 0;i++)
cout << "s.sales[" << i << "] = " << s.sales[i] << endl;
cout << "average = " << s.average << endl;
cout << "max = " << s.max << endl;
cout << "min = " << s.min << endl;
}
}