⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 编寫一個Stackf类,包括對該stack的實現
💻 CPP
字号:
#include<iostream.h>
#include<stdio.h>
#include"stack.h"
void introduction(){   //介绍本程序用途
	cout<<"This program allwos the user to enter one command"<<endl
		<<"(but only one) on each input line."<<endl
		<<"For example,if the command p is entered,then"<<endl
		<<"the program will pop the front of the stack."<<endl;
		return;
}
void help(){ //列出操作列表
	cout<<"The valid commands are:"<<endl
		<<"N-Return the number of the elements in stack."<<endl
		<<"P-Pop the front element of the stack"<<endl
		<<"T-Get and print the front element."<<endl
		<<"S-Push an element into the stack."<<endl
		<<"E-Retrun whether it is empty."<<endl
		<<"F-Return whether it is full."<<endl
		<<"C-Clear the stack."<<endl
		<<"Q-Quit"<<endl<<endl;
}
template<class T>
bool do_command(char c,Stack<T>& test_stack){
	bool continue_input=true; //用于判断是否继续进行操作
	T element;  //作top()及push()函数的参数
	switch(c){
	case 'P': //调用pop()
		if(test_stack.empty()) //若栈空
			cout<<"Stack is empty."<<endl<<endl;
		else{
			test_stack.pop();
            cout<<endl;
		}
        break;
	case 'T': //调用top()
		if(test_stack.empty()) //若栈空
			cout<<"Stack is empty."<<endl<<endl;
		else{
			test_stack.top(element);
			cout<<"The first element is:"<<element<<endl<<endl;
		}
		break;
	case 'S': //调用push()
		if(test_stack.full()) //若栈满
			cout<<"The stack is full."<<endl<<endl;
		else{
			cout<<"Enter the element:";
			cin>>element;  
			test_stack.push(element); //将element压栈
			cout<<endl;
		}
		break;
	case 'N': //调用ele_numbers()
		cout<<"The number of the elements in stack is:"<<test_stack.ele_numbers()<<endl<<endl;
		break;
	case 'E': //调用empty()
		cout<<"The stack is "<<(test_stack.empty()?"":"not ")<<"empty."<<endl<<endl;
		break;
	case 'F'://调用full();
		cout<<"The stack is "<<(test_stack.full()?"":"not ")<<"full."<<endl<<endl;
		break;
	case 'C': //调用clear()
		test_stack.clear();
		cout<<"The stack has been cleared."<<endl<<endl;
		break;
	case 'Q': //退出
		cout<<"Stack demonstration finished."<<endl<<endl;
		continue_input=false;
		break;
	}
	return continue_input;
}
char get_command(){
	char ch;
	help();
	do{
		cout<<"Enter the character you need:";
		cin>>ch;
	}while(ch!='P'&&ch!='T'&&ch!='S'&&ch!='Q'&&ch!='N'&&ch!='E'&&ch!='F'&&ch!='C'); //ch必须为'P'、'T'、'S'、'Q'、'N'、'E'、'F'、'C'之一
	return ch;
}

int main(){
	Stack<int> test_stack;
	introduction();
	while(do_command(get_command(),test_stack));
	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -