程序填空题:D Fill in the blanks
Run the following program, Enter: 1, the output is: 55 34 21 13 8 5 3 2 1 1
```
#include
using namespace std;
enum ERROR{UnderFlow,OverFlow};
template
class StackTemplate {
enum { ssize = 100 };
T stack[ssize];
int top;
public:
StackTemplate() : top(0) {}
void push(const T& i) {
if (top >= ssize) @@[throw OverFlow](1);
stack[top++] = i;
}
T pop() {
if (@@[top <= 0](1)) throw UnderFlow;
return @@[stack[--top]](1);
}
int size() const
{ return top; }
};
int fibonacci(int n);
int main() {
@@[try](1) {
@@[StackTemplate](1) is;
for(int i = 0; i < 20; i++)
is.push(fibonacci(i));
for(int k = 0; k < 20; k++)
cout << is.pop() << "\t";
}
catch( ERROR e ) {
switch(@@[e](1))
{
case OverFlow:
exit;
case UnderFlow:
exit;
}
}
catch(...)
{
exit;
}
return 0;
}
int fibonacci(int n)
{
@@[const](1) int sz = 100;
int i;
static int f[sz];
if (n >= sz) @@[throw OverFlow](1);
f[0] = f[1] = 1;
for(i = 0; i < sz; i++)
if(f[i] == 0) break;
while(i <= n) {
@@[f[i]](1) = f[i-1] + f[i-2];
i++;
}
return @@[f[n]](1);
}
```
答案:
第1空:throw OverFlow
第2空:top <= 0
第3空:stack[--top]
第4空:try
第5空:StackTemplate
第6空:e
第7空:const
第8空:throw OverFlow
第9空:f[i]
第10空:f[n]
```
#include
using namespace std;
enum ERROR{UnderFlow,OverFlow};
template
class StackTemplate {
enum { ssize = 100 };
T stack[ssize];
int top;
public:
StackTemplate() : top(0) {}
void push(const T& i) {
if (top >= ssize) @@[throw OverFlow](1);
stack[top++] = i;
}
T pop() {
if (@@[top <= 0](1)) throw UnderFlow;
return @@[stack[--top]](1);
}
int size() const
{ return top; }
};
int fibonacci(int n);
int main() {
@@[try](1) {
@@[StackTemplate
for(int i = 0; i < 20; i++)
is.push(fibonacci(i));
for(int k = 0; k < 20; k++)
cout << is.pop() << "\t";
}
catch( ERROR e ) {
switch(@@[e](1))
{
case OverFlow:
exit;
case UnderFlow:
exit;
}
}
catch(...)
{
exit;
}
return 0;
}
int fibonacci(int n)
{
@@[const](1) int sz = 100;
int i;
static int f[sz];
if (n >= sz) @@[throw OverFlow](1);
f[0] = f[1] = 1;
for(i = 0; i < sz; i++)
if(f[i] == 0) break;
while(i <= n) {
@@[f[i]](1) = f[i-1] + f[i-2];
i++;
}
return @@[f[n]](1);
}
```
答案:
第1空:throw OverFlow
第2空:top <= 0
第3空:stack[--top]
第4空:try
第5空:StackTemplate
第6空:e
第7空:const
第8空:throw OverFlow
第9空:f[i]
第10空:f[n]