完善下列代码:
- User
完善下列代码:
- Assistant
由于题目没有给出代码,我无法完善。请提供代码。
- User
include using namespace std; #define MAX_SIZE 100 template class Stack {private: T stck[MAX_SIZE]; int top; public: Stack(){ top=0; } int empty() { if(top==0) { cout<<“stack is empty.”<<endl;return 1; } } int full() { if(top==MAX_SIZE) cout<<“stack is full.”<<endl; return 1; } ~Stack() {} void push(T dd);//入栈成员函数的声明 T pop();//出栈成员函数的声明 }; //类中成员函数的定义 ⑹ _ void Stack::push(T dd) { if(top==MAX_SIZE ){ cout<<“stack is full.”<<endl; } stck[top]=dd; top++; } template ⑺ { top–; if(top==-1) { cout<<“stack is empty.”<<endl; return -1; } return stck[top]; } int main() { ⑻//定义模板类对象 st1,其中模板参数类型为 int
for(int i=0;i<MAX_SIZE;i++) ⑼ //将数据 i+1 添加到栈中,即 i+1 入栈 for(i=0;i<MAX_SIZE;i++) ⑽ / /栈项元素出栈并显示
- Assistant
代码已完善,如下所示:
#include using namespace std;
#define MAX_SIZE 100
template class Stack { private: T stck[MAX_SIZE]; int top; public: Stack(){ top=0; } int empty() { if(top==0) { cout<<"stack is empty."<<endl; return 1; } return 0; } int full() { if(top==MAX_SIZE) { cout<<"stack is full."<<endl; return 1; } return 0; } ~Stack() {} void push(T dd);//入栈成员函数的声明 T pop();//出栈成员函数的声明 };
//类中成员函数的定义 template void Stack::push(T dd) { if(top==MAX_SIZE ) { cout<<"stack is full."<<endl; return; } stck[top]=dd; top++; }
template T Stack::pop() { top--; if(empty()) { return -1; } return stck[top]; }
int main() { //定义模板类对象 st1,其中模板参数类型为 int Stack st1;
codefor(int i=0;i<MAX_SIZE;i++) { //将数据 i+1 添加到栈中,即 i+1 入栈 st1.push(i+1); } for(int i=0;i<MAX_SIZE;i++) { //栈项元素出栈并显示 cout<<st1.pop()<<endl; } return 0;}
- User
【程序说明】下列程序用函数模板实现两个数据的交换,请把有关的语句填充完整 #include using namespace std; template < (8) > void swap( (9) , (10) ) { T z; z=x; x=y; y=z; } void main() { int j=1,k=3; cout<<"int 数据类型:"<<j<<","<<k<<"=>"; swap(j,k); cout<<j<<","<<k<<endl; double x=1.23,y=9.87; cout<<"double 数据类型: "<<x<<","<<y<<"=>"; swap(x,y); cout<<x<<","<<y<<endl; char p='A',q='B'; cout<<"char 数据类型:"<<p<<","<<q<<"=>"; swap(p,q); cout<<p<<","<<q<<endl ; }
- Assistant
下列程序用函数模板实现两个数据的交换,请把有关的语句填充完整。完整的程序如下:
#include using namespace std;
template void swap(T& x, T& y) { T z; z = x; x = y; y = z; }
int main() { int j = 1, k = 3; cout << "int 数据类型:" << j << "," << k << "=>"; swap(j, k); cout << j << "," << k << endl;
codedouble x = 1.23, y = 9.87; cout << "double 数据类型: " << x << "," << y << "=>"; swap(x, y); cout << x << "," << y << endl; char p = 'A', q = 'B'; cout << "char 数据类型:" << p << "," << q << "=>"; swap(p, q); cout << p << "," << q << endl; return 0;}