c++ 回顾
1.函数交换不能交换实际参数的值,因为x和y是值参数。调用交换(a, b)导致实际参数a和b的值分别复制到形式参数x和y中。然后,在swap的主体中交换x和y的值,函数终止。在终止时,x和y的新值不会复制回实际参数中。因此,实际参数的值不受函数的影响。为了使函数正常工作,我们应该改为
void swap(int&, int&y)
{
int temp = x;
x= y;
y=temp;
}
2.编写一个函数模板count 返回值为数组a[0:n-1] 中value出现的次数。
template <class T>
int count(T a[], const T& value)
{// Return number of occurrences of value in a[0:n-1].
int theCount = 0;
for (int i = 0; i < n; i++)
if (a[i] == value)
theCount++;
return theCount;
}
3.编写一个函数模板fill 给数组a[start:end-1] 赋值value
template <class T>
void fill(T* a, int start, int end, const T& value)
{// Set a[start:end-1].
for (int i = start; i < end; i++)
a[i] = value;
}
5.编写一个函数模板iota 使a[i]=value+i,0<=i<n;
template <class T>
void iota(T* a, int n, const T& value)
{// Set a[i] = a[i] + value, 0 <= i < n.
for (int i = 0; i < n; i++)
a[i] += value;
}
7.编写一个函数模板mismatch, 返回值使不等式a[i] 不等于b[i]成了的最小索引i, 0<=i<n;
template <class T>
int mismatch(T* a, T* b, int n)
{// Return smallest i such that a[i] != b[i].
// Return n if no such i.
for (int i = 0; i < n; i++)
if (a[i] != b[i])
return i;
// no mismatch
return n;
}
9.下面语句调用分别调用哪个abc函数?那一句会出现错误?为什么?
1.count<<abc(1, 2, 3)<<endln
函数调用的签名与程序1.1中abc函数的签名一致。因此,调用程序1.1的abc函数
2.count<<abc(1.0F, 2.0F, 3.0F)<<endln
函数调用的签名与程序1.2的abc函数的签名一致。因此,调用程序1.2的abc功能
3.count<<abc(1, 2, 3.0F)<<endln
函数调用的签名与abc函数的签名不一致。由于可以实现从float到int和从int到float的类型转换,c++无法解决使用哪个函数,并给出编译时错误。
4.count<<abc(1.0, 2.0, 3.0)<<endln
实际参数的类型是double。c++无法解决使用哪个函数,并给出一个编译时错误,因为可以将double类型转换为int和float类型
11. 从做练习2 当n<1 时抛出char* 异常。
template <class T>
int count(T a[], int n, const T& value)
{// Return number of occurrences of value in a[0:n-1].
if (n < 1)
throw "n must be >= 1";
int theCount = 0;
for (int i = 0; i < n; i++)
if (a[i] == value)
theCount++;
return theCount;
}
13编写一个函数模板 changeLength1D 它将原始一维数组由原有长度变为新长度 ,把原数组的千 min(oldLength, newLength)长度的数据复制到新数组中,释放原数组的内存。
template<class T>
void changeLength1D(T*& a, int oldLength, int newLength)
{
if (newLength < 0)
throw illegalParameterValue("new length must be >= 0");
T* temp = new T[newLength]; // new array
int number = min(oldLength, newLength); // number to copy
copy(a, a + number, temp);
delete [] a; // deallocate old memory
a = temp;
}
15
1.数据成员元的类型为long。赋值给无符号long类型的变量的最大值是2^{32}- 1。美分的最大允许值是99美分。
因此,当使用程序1.13的表示法时,允许的最大货币值是2^{32}- 1美元99美分。
因为我们是分开存储符号的,所以最小的货币值就是最大值的负数。
2.赋值给int类型的变量的最大值是2^{31}- 1。美分的最大允许值是99美分。
当美元和美分的数据类型改为int时,允许的最大货币值是2^{31}- 1美元和99美分。
因为我们是分开存储符号的,所以最小的货币值就是最大值的负数。
3.为了避免在转换中出现错误,结果(a1和a2)不应该超过MAX_VALUE = 2^{32}-1。
因此,美元金额不应该超过MAX_VALUE / 100。即使美元数量可能不超过MAX_VALUE / 100,
该方法也可能不会给出正确的结果。为了确保结果正确,金额的总和不应该超过MAX_VALUE / 100。
17. 使用程序 1-19 的代码完成练习 16.
void input()
{
// input the amount as a double
cout << "Enter the currency amount as a real number" << endl;
double theValue;
cin >> theValue;
// set the value
setValue(theValue);
}
currency subtract(const currency& x)
{// Return *this - x.
currency result;
result.amount = amount - x.amount;
return result;
}
currency percent(float x)
{// Return x percent of *this.
currency result;
result.amount = (long) (amount * x / 100);
return result;
}
currency multiply(float x)
{// Return this * x.
currency result;
result.amount = (long) (amount * x);
return result;
}
currency divide(float x)
{// Return this / x.
currency result;
result.amount = (long) (amount / x);
return result;
}