CG作业参考
带注释
ADT声明
# include<iostream>
using namespace std;
template <typename E> class List{
private:
//用于保护赋值对象和复制对象
void operator =(const List&) {}
List(const List&){}
public:
List(){}
//虚析构函数
virtual ~List(){}
//添加元素
virtual void add(const E& it) = 0;
//获取第i个数据信息
virtual E get(int i) = 0;
//删除第i个元素
virtual void remove(int i) = 0;
};
ADT实现
#include "DefineADT.h"
#include <iostream>
using namespace std;
template <typename E>
class AList : public List<E>
{
private:
//顺序表总长度空间
int maxSize;
//数组指针
E *listArray;
//顺序表当前数据长度
int listSize;
public:
AList(int size)
{
maxSize = size;
listSize = 0;
listArray = new E[maxSize];
}
E get(int i)
{
//判断i是否合法
if (i < maxSize)
return listArray[i];
else return 0;
}
//添加元素
void add(const E& it){
//如果空间足够
if(listSize<maxSize){
listArray[listSize] = it;
listSize++;
}
else
cout << "添加失败,空间不足"<<endl;
}
void remove(int i){
if(i<listSize){
for (int j = i ; j < listSize-1; j++)
{
listArray[j] = listArray[j + 1];
}
listSize--;
}
else
cout << "删除失败,超出数据大小"<<endl;
}
int size(){
return listSize;
}
};
主函数
#include "ConductADT.h"
#include <iostream>
#include <ctype.h>
#include<string>
using namespace std;
int main(){
string str;
getline(cin, str);
//创建线性表大小
AList<char> L(str.length());
//向线性表中添加元素
for (int i = 0; i < str.size(); i++)
{
L.add(str[i]);
}
int abc=0;
int num=0;
int other=0;
//for循环遍历计数
for (int i = 0; i < str.length(); i++)
{
char c = L.get(i);
if(isalpha(c))
abc++;
else if(isdigit(c))
num++;
else
other++;
}
//输出
cout << abc << " " << num << " " << other << endl;
//遍历删除数字
int i = 0;
while(i<L.size())
{
char c = L.get(i);
if(isdigit(c))
L.remove(i);
else i++;
}
//输出
for (int i = 0; i < L.size(); i++)
{
cout << L.get(i);
}
}