📄 interface.cpp
字号:
#include "Interface.h"
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
template<class DataType>
Interface<DataType>::Interface(void)
{
}
template<class DataType>
Interface<DataType>::~Interface(void)
{
}
template<class DataType>
void Interface<DataType>::GetLength(const Chain<DataType> &List) const //取长度模块
{
cout<<"\nThe Length of the List is : " << List.Length() <<endl; //直接将长度输出
}
template<class DataType>
void Interface<DataType>::Output(Chain<DataType> &List) //输出模块
{
bool reselect;
cout<<"\n\n*****Output Module***** ";
cout<<"\n1.Output before sort. \n2.Output after sort. \n3.Return to mainmenu.\n4.Exit.\nselect an operation: \n"; //可以选择的操作,原线性表输出,按升序输出,返回主菜单,退出程序
char selection; //判断是否要重呈现子菜单
selection=getch(); //读取对操作的选择
while(1) //无限循环,对于不符合要求可以直接退出或者返回
{
reselect=true;
switch(selection)
{
case '1':{
Chain<DataType> temp(List); //创建一个线性表,为了防止对原线性表的修改,便于用户进行其他的操作
try
{
cout<<temp;
}
catch(Error) //如果抛出异常就捕捉,输出错误提示信息
{
cout<<"\nPlease check the data input or input the data again.\n";
break;
}
break;}
case '2':{
Chain<DataType> temp(List);
try
{
temp.Sort();
cout<<temp;
}
catch(Error)
{
cout<<"\nPlease check the data input or input the data again.\n";
break;
}
break;}
case '3':
return ;
case '4':
cout<<"\nThank you for using Stephen List System.\n"; //选择退出整个程序
system("pause");
exit(1);
default:{ //如果输入操作错误
reselect=false;
cout<<"Error Input.Please input again:\n";
selection=getch();
break;}
}
if(reselect) //正确输入后重新返回到子菜单
{
cout<<"\n\n1.Output before sort. \n2.Output after sort. \n3.Return to mainmenu.\n4.Exit.\nselect an operation: \n";
selection=getch();
}
}
}
template<class DataType>
void Interface<DataType>::Search(const Chain<DataType> &List) const //搜索模块
{
bool reselect;
char selection;
cout<<"\n\n*****Search Module***** ";
cout<<"\n1.Search by value. \n2.Search by index. \n3.return to mainmenu. \n4.Exit.\nselect an operation: \n"; //按值查找,按索引查找,返回主菜单,退出程序
selection=getch();
while(1)
{
reselect=true;
switch(selection)
{
case '1':{
cout<<"Input a value you want to search: ";
int s;
cin>>s;
int index=List.Search(s);
if(index)
cout<<"The index of the value is : "<<index;
else
cout<<"Sorry , the element you have input isn't in the list.";
break;}
case '2':{
cout<<"Input the index you want to search: ";
int i;
cin>>i;
int value;
bool check=false;
try
{
check=List.Find(i,value);
}
catch(Error)
{
cout<<"\nThe out_of_Bounds index you have input cause error.\nPlease reselect the operation.\n";
break;
}
if(check)
cout<<"The value of the index is : " << value;
else
cout<<"Index no exist." <<endl;
break;}
case '3':
return ;
case '4':
cout<<"\nThank you for using Stephen List System.\n";
system("pause");
exit(1);
default:{
reselect=false;
cout<<"Error Input.Please input again: \n";
selection=getch();
break;}
}
if(reselect)
{
cout<<"\n\n1.Search by value. \n2.Search by index. \n3.return to mainmenu. \n4.Exit.\nselect an operation: \n";
selection=getch();
}
}
}
template<class DataType>
void Interface<DataType>::Operation() //主菜单方法,是一个程序和用户的接口
{
cout<<"Welcome to Stephen's System.\n\n\n";
Chain<DataType> List;
//开始就为用户建立一个空的线性表,用户可以对线性表进行插入,删除,取长度,输出,合并等一系列的操作
cout<<"Instructions:\nAn empty List have been created.\nYou can select the operations below to munipulate the List.\n";
cout<<"\n\n**********MainMenu**********\n";
cout<<"1.Add or Delete data.\n .....you can insert a new data or delete the data from the list.\n";
cout<<"2.Search value.\n .....you can search a element by value or index.\n";
cout<<"3.Get Length.\n .....you can get the length of the list you have input.\n";
cout<<"4.Output data.\n .....you can output the data by sort or not.\n";
cout<<"5.Combination.\n .....you can combine two list and output the combination.\n";
cout<<"6.Exit.\n";
cout<<"Select an operation: ";
bool reselect;
char selection;
selection=getch();
while(1)
{
reselect=true;
switch(selection)
{
case '1':
this->List_Manipulation(List); //调用增删元素模块
break;
case '2':
this->Search(List); //调用搜索模块
break;
case '3':
this->GetLength(List); //调用取长度模块
break;
case '4':
this->Output(List); //调用输出模块
break;
case '5':
this->Combine(List); //调用合并线性表模块
break;
case '6':
cout<<"\nThank you for using Stephen List System.\n";
system("pause");
exit(1);
default:{
reselect=false;
cout<<"Input Error.Please input again: \n";
selection=getch();
break;}
}
if(reselect)
{
cout<<"\n\n**********MainMenu**********\n";
cout<<"1.Add or Delete data.\n.....you can insert a new data or delete the data from the list.\n";
cout<<"2.Search value.\n .....you can search a element by value or index.\n";
cout<<"3.Get Length.\n .....you can get the length of the list you have input.\n";
cout<<"4.Output data.\n .....you can output the data by sort or not.\n";
cout<<"5.Combination.\n .....you can combine two list and output the combination.\n";
cout<<"6.Exit.\n";
cout<<"Select an operation: ";
selection=getch();
}
}
}
template<class DataType>
void Interface<DataType>::Combine(const Chain<DataType> &List) const
{
cout<<"\n\n*****Combination Module*****";
cout<<"\nAt present ,you only have one list in the memory,you need to add anthor list :"; //需要再增加一个线性表
Chain<DataType> ComList;
char selection='y';
while(selection=='y' || selection=='Y') //判断是否再继续输入
{
int index=1;
DataType value;
cout<<"\nInput the value: ";
cin>>value;
try
{
ComList.Insert(index,value);
}
catch(Error)
{
cout<<"\nThe index you input caused error.\nPlease check the index.\n";
break;
}
cout<<"\nAfter the insertion:"<<ComList<<endl;
cout<<"\nInsert Continue?(y/n) select: ";
selection=getch();
++index;
}
cout<<"\nInput Complete!\n";
Chain<DataType> CombineList(List);
CombineList.Sort();
ComList.Sort();
Chain<DataType> Combinated_List;
Combinated_List.Combination(CombineList,ComList);
cout<<"\nAfter sorted Combination of the two list:\n";
cout<<Combinated_List;
}
template<class DataType>
void Interface<DataType>::List_Manipulation(Chain<DataType> &List) //增删元素模块
{
cout<<"\n\n*****Insert and Delete Module*****"; //插入元素,删除元素,返回主菜单,退出程序
cout<<"\n1.Insert element.\n2.Delete element.\n3.Return to mainmenu.\n4.Exit.\nSelect an operation:\n";
char selection;
bool reselect;
selection=getch();
while(1)
{
reselect=true;
switch(selection)
{
case '1':{
int index;
DataType value;
cout<<"\nInput the index :";
cin>>index;
cout<<"Input the value: ";
cin>>value;
try
{
List.Insert(index,value);
}
catch(Error)
{
cout<<"\nThe index you input caused error.\nPlease check the index.\n";
break;
}
cout<<"\nAfter the insertion:\n"<<List<<endl;
cout<<"\nInput complete!\n";
break;}
case '2':{
int index;
DataType value;
cout<<"\nInput the index : ";
cin>>index;
try
{
List.Delete(index,value);
}
catch(Error)
{
cout<<"\nThe index you input caused error.\nPlease check the index.\n";
break;
}
cout<<"You delete the data : "<<value<<endl;
cout<<"After delete:\n";
cout<<List<<endl;
break;}
case '3':
return ;
case '4':
cout<<"\nThank you for using Stephen List System.\n";
system("pause");
exit(1);
default:{
reselect=false;
cout<<"Error Input.Please input again:\n";
selection=getch();
break;}
}
if(reselect)
{
cout<<"\n1.Insert element.\n2.Delete element.\n3.Return to mainmenu.\n4.Exit.\nSelect an operation:\n";
selection=getch();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -