程序博客网 > java web开发 pdf
来源:互联网 发布:java web开发 pdf 编辑:程序博客网 时间:2024/04/28 05:13
#include <iostream>#include <string>#include <cctype>#include <cstdlib>using namespace std;class Tools{public: static bool Is_int(string StrCheckUp) { for(int i = 0; i < int(StrCheckUp.length()); i++) //判断字符串是否全是数字 if(!isdigit(StrCheckUp[i])) return false; return true; }};class List{public: string *data; int length; int size; List() { size = 128; data = new string[size]; length = 0; ClearList(); } ~List() { delete []data; } void Insert(int pos, string e) { if(length + 1 == size - 1) { size = size + 100; string *temp = new string[size]; for(int n = 0; n <= length; n++) { temp[n] = data[n]; } delete []data; data = temp; } if(pos > length + 1 || pos < 1) cout << "erorr" << endl; else { for(int i = length + 1; i >= pos; i--) data[i + 1] = data[i]; data[pos] = e; length++; } } void Delete(int pos) { if(pos > length || pos < 1) cout << "erorr" << endl; else { for(int i = pos; i < length; i++) data[i] = data[i + 1]; data[length] = ""; length--; } } string GetData(int pos) { if(pos > length || pos < 1) { cout << "erorr" << endl; return ""; } else { return data[pos]; } } int Find(string e) { for(int i = 1; i <= length; i++) { if(data[i] == e) return i; } return 0; } string GetPriorData(string e) { if(Find(e) == 0 || Find(e) == 1) { return ""; } else { return data[Find(e) - 1]; } } string GetNextData(string e) { if(Find(e) == length || Find(e) == 0) { return ""; } else { return data[Find(e) + 1]; } } void ClearList() { for(int i = 0; i < size; i++) data[i] = ""; length = 0; } void ListDisplay() { cout << "当前线性表:"; for(int i = 1; i <= length; i++) { cout << '[' << data[i] << ']'; } cout << endl; } void Run() { string num; string s; bool GoOn = true; cout << "1.建立线性表" << endl << "2.插入元素" << endl << "3.删除元素" << endl << "4.查找特定元素的位置" << endl << "5.查找特定位置的元素" << endl << "6.输出指定元素的前驱" << endl << "7.输出指定元素的后继" << endl << "8.清空线性表" << endl << "0.退出" << endl; char ctrl; do{ cin >> ctrl; switch(ctrl) { //建立线性表 case '1': { cout << "请输入线性表长度:" << endl; cin >> num; fflush( stdin ); if(Tools::Is_int(num)) { cout << "请依次输入各元素" << endl; for(int i = 0; i < atoi(num.c_str()); i++) { getline(cin, s); fflush( stdin ); Insert(i + 1, s); } } else { cout << "无效输入" << endl; } break; } //插入元素 case '2': { cout << "请输入位置" << endl; cin >> num; fflush( stdin ); if(Tools::Is_int(num)) { cout << "输入元素" << endl; getline(cin, s); fflush( stdin ); Insert(atoi(num.c_str()), s); } else { cout << "无效输入" << endl; } break; } //删除元素 case '3': { cout << "请输入位置" << endl; cin >> num; fflush( stdin ); if(Tools::Is_int(num)) { Delete(atoi(num.c_str())); } else { cout << "无效输入" << endl; } break; } //查找特定元素的位置 case '4': { fflush( stdin ); cout << "请输入元素" << endl; getline(cin, s); int n = Find(s); if(n == 0) { cout << "找不到该元素" << endl; } else { cout << "位置:" << n << endl; } break; } //查找特定位置的元素 case '5': { fflush( stdin ); cout << "请输入位置" << endl; cin >> num; if(Tools::Is_int(num)) { cout << "该位置元素是:" << GetData(atoi(num.c_str())) << endl; } else { cout << "无效输入" << endl; } break; } //输出指定元素的前驱 case '6': { fflush( stdin ); cout << "请输入元素" << endl; getline(cin, s); if(GetPriorData(s) == "") { cout << "不存在该元素 或 没有前驱" << endl; } else { cout << "前驱:" << GetPriorData(s) << endl; } break; } //输出指定元素的后继 case '7': { fflush( stdin ); cout << "请输入元素" << endl; getline(cin, s); if(GetNextData(s) == "") { cout << "不存在该元素 或 没有后继" << endl; } else { cout << "后继:" << GetNextData(s) << endl; } break; } case '8': { ClearList(); break; } case '0': { GoOn = false; } default : { cout << "无效输入" << endl; } } ListDisplay(); fflush( stdin ); }while(GoOn); }};void main(){ List list; list.Run();}
元素统一用string
方便输出看结构