1 #include <iostream> 2 #include <fstream> 3 #include <iomanip> 4 #include <string> 5 #include <cstring> 6 #define ERROR 0 7 #define OK 1 8 #define MaxSize 1000000 9 using namespace std; 10 typedef int Status; 11 typedef struct 12 { 13 string no; 14 string name; 15 int price; 16 }ElemType; 17 typedef struct 18 { 19 ElemType *elem; 20 int length; 21 }SqList; 22 SqList L; 23 24 Status InitList(SqList &L)//顺序表初始化 25 { 26 L.elem=new ElemType[MaxSize]; 27 if(!L.elem) return ERROR; 28 29 L.length=0; 30 return OK; 31 } 32 33 void Menu()//菜单选项 34 { 35 cout<<endl; 36 cout<<" 1.从文件中读入数据到顺序表"<<endl; 37 cout<<" 2.遍历顺序表"<<endl; 38 cout<<" 3.查找"<<endl; 39 cout<<" 4.插入"<<endl; 40 cout<<" 5.删除"<<endl; 41 cout<<" 6.修改"<<endl; 42 cout<<" 7.排序:对图书价格升序排列"<<endl; 43 cout<<"请选择功能选项:"; 44 } 45 46 Status ReadFromFile(SqList &L,char *filename)//1.从文件读入数据到顺序表 47 { 48 ifstream infile(filename,ios::in); 49 if(!infile) return ERROR; 50 51 while(!infile.eof()) 52 { 53 infile>>L.elem[L.length].no; 54 infile>>L.elem[L.length].name; 55 infile>>L.elem[L.length].price; 56 L.length++; 57 } 58 infile.close(); 59 return OK; 60 } 61 62 Status ListPrint(SqList &L)//2.遍历顺序表输出 63 { 64 if(!L.length) return ERROR; 65 66 cout<<left<<setw(18)<<"ISBN"<<left<<setw(24)<<"书名"<<"定价"<<endl; 67 for(int i=0;i<=L.length-1;i++) cout<<left<<setw(18)<<L.elem[i].no<<left<<setw(24)<<L.elem[i].name<<left<<setw(24)<<L.elem[i].price<<endl; 68 69 return OK; 70 } 71 72 int LocateElem(SqList &L,string s)//3.查找,返回元素位置序号 73 { 74 for(int i=0;i<=L.length-1;i++) if(L.elem[i].name==s || L.elem[i].no==s) return i+1; 75 return 0; 76 } 77 78 Status ListInsert(SqList &L,int i,ElemType e)//4.插入元素 79 { 80 if(i<1 || i>L.length+1) return ERROR; 81 if(L.length==MaxSize) return OK; 82 for(int k=0;k<=L.length-1;k++) if(L.elem[k].no==e.no && L.elem[k].name==e.name && L.elem[k].price==e.price) return ERROR; 83 84 for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j]; 85 L.elem[i-1]=e; 86 L.length++; 87 return OK; 88 } 89 90 Status ListDelete(SqList &L,string s)//5.删除元素 91 { 92 int i=LocateElem(L,s); 93 if(!i) return ERROR; 94 95 i--; 96 for(int j=i;j<=L.length-1;j++) L.elem[j]=L.elem[j+1]; 97 L.length--; 98 return OK; 99 } 100 101 Status ListChange(SqList &L,string s,ElemType e)//6.修改元素 102 { 103 int i=LocateElem(L,s); 104 if(!i) return ERROR; 105 106 i--; 107 L.elem[i]=e; 108 return OK; 109 } 110 111 int Partition(SqList &L,int left,int right)//快速排序,对图书价格升序排列 112 { 113 ElemType Temp=L.elem[left]; 114 int temp=L.elem[left].price; 115 while(left<right) 116 { 117 while(left<right && L.elem[right].price>=temp) right--; 118 L.elem[left]=L.elem[right]; 119 while(left<right && L.elem[left].price<=temp) left++; 120 L.elem[right]=L.elem[left]; 121 } 122 L.elem[left]=Temp; 123 124 return left; 125 } 126 void QSort(SqList &L,int left,int right) 127 { 128 int pivotloc=0; 129 if(left<right) 130 { 131 pivotloc=Partition(L,left,right);//移动并获取枢轴位置 132 QSort(L,left,pivotloc-1);//递归左子序列 133 QSort(L,pivotloc+1,right);//递归右子序列 134 } 135 } 136 137 int main() 138 { 139 int f=InitList(L); 140 if(!f) 141 { 142 cout<<"顺序表初始化失败!"<<endl; 143 return 0; 144 } 145 else 146 { 147 cout<<"顺序表初始化成功!"<<endl; 148 } 149 150 Menu(); 151 int choose; 152 while(cin>>choose) 153 { 154 if(choose==1) 155 { 156 int ans=ReadFromFile(L,"afile.txt"); 157 if(!ans) cout<<"读入数据失败,文件不能被打开!"<<endl; 158 else cout<<"读入数据成功!"<<endl; 159 } 160 else if(choose==2) 161 { 162 int ans=ListPrint(L); 163 if(!ans) cout<<"顺序表中没有元素!"<<endl; 164 } 165 else if(choose==3) 166 { 167 cout<<"请输入要查找的书名或其ISBN"<<endl; 168 string s; 169 cin>>s; 170 171 int ans=LocateElem(L,s); 172 if(!ans) cout<<"未找到该元素!"<<endl; 173 else cout<<"该元素位置序号为"<<ans<<endl; 174 } 175 else if(choose==4) 176 { 177 cout<<"请输入要插入的位置(1=<插入位置<=length+1)"<<endl; 178 int pos; 179 cin>>pos; 180 cout<<"请输入要插入的元素信息"<<endl; 181 ElemType e; 182 cin>>e.no>>e.name>>e.price; 183 184 int ans=ListInsert(L,pos,e); 185 if(!ans) cout<<"元素插入失败,请检查插入位置是否合法,元素是否已经存在,或空间是否已满!"<<endl; 186 else cout<<"元素插入成功!"<<endl; 187 } 188 else if(choose==5) 189 { 190 cout<<"请输入要删除书籍的ISBN"<<endl; 191 string s; 192 cin>>s; 193 194 int ans=ListDelete(L,s); 195 if(!ans) cout<<"元素删除失败,请检查该元素是否存在!"<<endl; 196 else cout<<"元素删除成功!"<<endl; 197 } 198 else if(choose==6) 199 { 200 cout<<"请输入要修改书籍的ISBN"<<endl; 201 string s; 202 cin>>s; 203 cout<<"请输入想要修改成的元素信息!"<<endl; 204 ElemType e; 205 cin>>e.no>>e.name>>e.price; 206 207 int ans=ListChange(L,s,e); 208 if(!ans) cout<<"元素修改失败,请检查该元素是否存在!"<<endl; 209 else cout<<"元素修改成功!"<<endl; 210 } 211 else if(choose==7) 212 { 213 QSort(L,0,L.length-1); 214 cout<<"元素按价格升序排序成功!"<<endl; 215 216 int ans=ListPrint(L); 217 if(!ans) cout<<"顺序表中没有元素!"<<endl; 218 } 219 else cout<<"输入错误,请重新输入!"<<endl; 220 221 Menu(); 222 } 223 224 return 0; 225 }
完。