11题:
#include<iostream>
using namespace std;
template<class T>
int count(T *arr, int n, const T &value)
{
//抛出异常
if (n < 1)
throw "The second parameter should be >= 1";
int num = 0;
for (int i = 0; i < n; ++i)
{
if (arr[i] == value)
++num;
}
return num;
}
void main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
const int value = 0;
int n = 0;
//检测异常,并报错
try{ cout << count(arr, n, value) << endl; }
catch (char* e)
{
cout << "The second parameters to count was " << n << endl;
cout << "An exception has been thrown\n";
cout << e << endl;
}
system("pause");
}
12题:将列长改为用列数组元素指定即可
void make2dArray(T ** &x, int numbersOfRows, int * &rowSize)
{//创建一个二维数组
//创建行指针
lens = sizeof(rowSize) / sizeof(rowSize[0]);
x = new T* [numbersOfRows];
for (int i = 0; i < lens; i++)
x[i] = new T[rowSize[i]];
}
13题:
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]; // 新数组
int number = min(oldLength, newLength); // 要拷贝的元素数量
for (int i = 0; i < number; i++)
temp[i] = a[i];
delete[] a; // 释放原空间
a = temp;
}
14题:二维数组可看作两个一维数组的嵌套
#include<algorithm>
#include<iostream>
using namespace std;
template<class Ta>
void changeLength1D(Ta *& 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
for (int i = 0; i < number; i++)
temp[i] = a[i];
delete[] a; // deallocate old memory
a = temp;
}
template<class Tb>
void changeLength2D(Tb **& b, int old_numbers_of_cows, int old_numbers_of_columns, int new_numbers_of_cows, int new_numbers_of_columns)
{
changeLength1D(b, old_numbers_of_cows, new_numbers_of_cows);
int num = min(old_numbers_of_cows, new_numbers_of_cows);
for (int i = 0; i < num;i++)
changeLength1D(b[i], old_numbers_of_columns, new_numbers_of_columns);
}
15题:
1)因为题设无符号长整形、无符号整形都占用4字节,那么,对美元来说,最大值为2^32-1,而美分最大只能为99。因此,表示金额的最大值为:2^32-1美元99美分,最小值为:最大值的负数。
2)整形的表示范围为(-2^31, 2^31-1),因此能表示的最大金额为:2^31-1美元99美分,最小金额为:-(2^31美元99美分)。
3)为了确保不发生错误,a1, a2最大可能值不能超过2^31-1。
16题:subtract(x)函数,返回了调用对象的引用,以作演示
#include<iostream>
using namespace std;
class currency
{
public:
enum signType{ plus, minus };
//构造函数
currency(signType theSign=plus,unsigned long theDollars=0, unsigned int theCents=0)
{
sign = theSign;
dollars = theDollars;
cents = theCents;
}
//析构函数
~currency(){}
void setValue(signType, unsigned long, unsigned int);
void setValue(double theAmount); //转换函数,从单个值分解
double amount(); //转换函数,将分解值化为单个值
void show(); //输出函数
void input(); //手动输入参数
currency &subtract(currency &x); //从调用对象中减去参数对象x的值
currency percent(double x); //返回调用对象的x%
currency multiply(double x); //返回调用对象的x倍
currency divide(double x); //返回调用对象的1/x倍
private:
signType sign;
unsigned long dollars;
unsigned int cents;
};
void currency::setValue(double theAmount)
{
if (theAmount < 0){ sign = minus; theAmount = -theAmount; }
else sign = plus;
dollars = (unsigned long)theAmount; //提取整数部分
cents = (unsigned int)((theAmount + 0.001 - dollars) * 100); //提取两位小数(防止编译器小数表示问题)
}
double currency::amount()
{
double theAmount = dollars + double(cents)/100;
if (sign == minus){ theAmount = -theAmount; }
return theAmount;
}
void currency::show()
{
if (sign == minus) { cout << "-"; }
cout << dollars << ".";
if (cents < 10) { cout << "0"; }
cout << cents << endl;
}
void currency::input() //提示输入
{
double theAmount;
cout << "Please input your money: ";
cin >> theAmount;
setValue(theAmount);
}
currency ¤cy::subtract(currency &x) //从调用对象中减去参数对象x的值
{
setValue(this->amount() - x.amount());
return *this;
}
currency currency::percent(double x) //返回调用对象的x%
{
currency newCurrency;
newCurrency.setValue(this->amount()*x / 100);
return newCurrency;
}
currency currency::multiply(double x) //返回调用对象的x倍
{
currency newCurrency;
newCurrency.setValue(this->amount() * x);
return newCurrency;
}
currency currency::divide(double x) //返回调用对象的1/x倍
{
currency newCurrency;
newCurrency.setValue(this->amount() / x);
return newCurrency;
}
void main()
{
double x=10;
cout << "初始a和b为: " << endl;
currency a, b;
a.show();
a.input();
cout << "手动输入参数后a变为:" << endl;
a.show();
cout << "a的10%为:" << endl;
b = a.percent(x);
b.show();
cout << "a的10倍为:" << endl;
b = a.multiply(x);
b.show();
cout << "a除以10为:" << endl;
b = a.divide(x);
b.show();
cout << "\n此时的a为:" << endl;
a.show();
cout << "此时的b为:" << endl;
b.show();
cout << "从a里减去b,a变为:" << endl;
a.subtract(b);
a.show();
system("pause");
}
17题:
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(double x)
{// Return x percent of *this.
currency result;
result.amount = (long)(amount * x / 100);
return result;
}
currency multiply(double x)
{// Return this * x.
currency result;
result.amount = (long)(amount * x);
return result;
}
currency divide(double x)
{// Return this / x.
currency result;
result.amount = (long)(amount / x);
return result;
}
18题:1):
#include<iostream>
using namespace std;
class currency
{
friend istream &operator>>(istream &, currency &);
public:
enum signType{ plus, minus };
//构造函数
currency(signType theSign = plus, unsigned long theDollars = 0, unsigned int theCents = 0)
{
amount = theDollars * 100 + theCents;
if (amount < 0) amount = -amount;
}
//析构函数
~currency(){}
void setValue(double theAmount); //设置值
currency operator-(const currency &x);
currency operator%(double x);
currency operator*(double x);
currency operator/(double x);
private:
long amount;
};
void currency::setValue(double theAmount)
{//考虑到机器表示小数的规则问题,此时我们取前两位小数
if (theAmount < 0)
amount = (long)((theAmount - 0.001) * 100);
else
amount = (long)((theAmount + 0.001) * 100);
}
istream &operator>>(istream &is,currency &x)
{//输入
is >> x.amount;
return is;
}
currency currency::operator-(const currency &x)
{//返回调用对象与参数对象的差值
currency result;
result.amount = amount - x.amount;
return result;
}
currency currency::operator%(double x)
{//返回对象的x%
currency result;
result.amount = amount * x / 100;
return result;
}
currency currency::operator*(double x)
{//返回对象的x倍
currency result;
result.amount = amount * x;
return result;
}
currency currency::operator/(double x)
{//返回对象的1/x倍
currency result;
result.amount = amount / x;
return result;
}
18题:2):将setValue(signType theSign, unsigned long theDollars, unsigned int theCents)函数换成operator=(int x)
currency ¤cy::operator=(int x)
{
amount = x;
return *this;
}
将setValue(double x)函数换成operator=(double x)
currency ¤cy::operator=(double x)
{
if (x < 0)
amount = (long)((x - 0.001) * 100);
else
amount = (long)((x + 0.001) * 100);
return *this;
}
19题:这个比较简单,用最普通的方法就好
int factorial (int n)
{// Return n!.
if (n <= 1)
return 1;
int fact = 2;
for (int i = 3; i <= n; i++)
fact *= i;
return fact;
}
20题:1):
int Fibonacci(int n)
{
if (n <= 0)
return 0;
if (n == 1)
return 1;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
2):考虑计算次数
#include<iostream>
using namespace std;
static int num = 0;
int Fibonacci(int n)
{
num++;
if (n <= 0)
return 0;
if (n == 1)
return 1;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
void main()
{ //检查函数值和调用次数
cout << Fibonacci(3) << ends;
cout << num << endl;
system("pause");
}
3)本题考虑了循环,也可以用斐波那契数列的通项公式(如果能记住的话):
int Fibonacci(int n)
{
int c, a = 1, b = 0;
if (n <= 0)
return b;
if (n == 1)
return a;
else
for (int i = 2; i <= n;i++)
{
c = a; //保存分支a的值
a += b; //计算当前的a,b分支之和
b = c; //更新分支b
}
return a;
}