首页 > 代码库 > 数据结构课作业系列
数据结构课作业系列
数据结构课作业-----顺序表
1 /**************************** 2 3 程序名称:简单电话号码查询系统 V.0.1 4 主要功能:简单联系人录入,查找,排序..... 5 主要结构:静态数组类链表 6 7 **************************/ 8 9 #include <cstdio> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 using namespace std; 14 #define N 1005 15 16 //主要结构 17 struct Person 18 { 19 char name[25]; 20 char telp[15]; 21 }; 22 FILE * fp; 23 24 //主要类 25 class Phone 26 { 27 public: 28 //自定义构造函数 29 Phone ():mySize(10) 30 { 31 memset (people, 0, sizeof people); 32 } 33 //创建函数 34 void Creat (int n) 35 { 36 mySize = n; 37 memset (people, 0, sizeof people); 38 } 39 //按姓名查找 or 按电话号查找 or 按位置查找 40 void Find(int flag) const; 41 //按位置插入信息 42 void Insert (Person p, int pos); 43 //按位置修改信息 44 void Revamp (Person p, int pos); 45 //全部显示 46 void Display (bool flag) const; 47 //按位置删除 48 void Delete (int pos); 49 //按姓名排序 or 按电话号码排序 50 void Sort (int flag); 51 private: 52 int mySize; 53 Person people[N]; 54 }; 55 56 bool cmp (const Person &a, const Person &b) 57 { 58 return strcmp (a.telp, b.telp) < 0 ? 1 : 0; 59 } 60 bool cmp1 (const Person &a, const Person &b) 61 { 62 return strcmp (a.telp, b.telp) > 0 ? 1 : 0; 63 } 64 65 void Phone::Delete (int pos) 66 { 67 if (mySize==0 || pos>mySize || pos<1) 68 { 69 cout << "电话本为空 or 位置不存在,无法删除\n"; 70 return; 71 } 72 for (int i=pos; i<mySize; i++) 73 people[i] = people[i+1]; 74 } 75 76 void Phone::Sort (int flag) 77 { 78 if (flag == 1) 79 sort (people, people+mySize, cmp1); 80 else 81 sort (people, people+mySize, cmp); 82 } 83 84 void Phone::Revamp (Person p, int pos) 85 { 86 if (pos<1 || pos>mySize) 87 { 88 cout << "位置不存在\n"; 89 return; 90 } 91 if (mySize == 0) 92 { 93 cout << "电话本为空\n"; 94 return; 95 } 96 if (strlen(people[pos].name)==0 || strlen (people[pos].telp)==0) 97 { 98 cout << "该处没有信息科修改\n"; 99 return;100 }101 people[pos] = p;102 }103 104 void Phone::Find (int flag) const105 {106 char tmp[25];107 int pos;108 cout << "输入要查找的信息: "; 109 if (flag < 3)110 cin >> tmp;111 else112 cin >> pos;113 114 bool tag = true;115 int i;116 for (i=0; i<mySize; i++)117 {118 if (flag == 1 && strcmp (tmp, this->people[i].name)==0)119 {120 tag = false;break;121 }122 else if (flag == 2 && strcmp (tmp, this->people[i].telp)==0)123 {124 tag = false;break;125 }126 else if (flag == 3 && (pos>1 && pos<mySize && mySize))127 {128 tag = false; i = pos; break;129 }130 }131 if (tag)132 cout << "Sorry,你要查找的信息不存在\n";133 else134 {135 cout << " 你要查找的信息如下:\n";136 cout << " 姓名: " << this->people[i].name << endl;137 cout << " 号码: " << this->people[i].telp << endl;138 }139 }140 141 void Phone::Display (bool flag) const142 {143 if (mySize == 0)144 {145 cout << " 电话号码为空\n";146 return;147 }148 149 cout << " 电话本大小: " << mySize<<endl;150 cout << " 电话本存有如下信息:\n";151 for (int i=1; i<=mySize; i++)152 {153 if (strlen(people[i].name) && !flag) 154 cout << "姓名: " << people[i].name << " 号码: " <<people[i].telp << endl;155 else if (strlen(people[i].name) && flag)156 {157 fp = fopen ("phone.txt","w");158 fprintf (fp,"姓名: %s 号码: %s\n",people[i].name,people[i].telp);159 }160 }161 cout << "\n";162 }163 164 void Phone::Insert (Person p, int pos)165 {166 if (mySize == N)167 {168 cout << "内存已满,无法插入\n";169 return;170 }171 if (pos<1 || pos>mySize)172 {173 cout << "试图插入的位置非法\n";174 return;175 }176 177 for (int i=mySize; i>pos; i--)178 people[i] = people[i-1];179 people[pos] = p;180 mySize++;181 }182 183 //菜单函数184 void Meue ()185 {186 cout << "\n\n"187 << " ************************************\n"188 << " 欢迎使用简单电话查询系统 \n"189 << " 1.创建一个 n 容量的电话本 \n"190 << " 2.添加联系人 \n"191 << " 3.修改联系人 \n"192 << " 4.查找联系人 \n"193 << " 5.显示联系人 \n"194 << " 6.排序联系人 \n"195 << " 7.写入到文件 \n"196 << " 8.删除联系人 \n"197 << " 0.退出 \n";198 cout << " ************************************\n";199 }200 201 void FuncCreat (Phone &p)202 {203 int n;204 cout << "输入电话本的大小( n 最大为1000): "; while (cin>>n, n>1000)cout << "** 重输 **\n";205 p.Creat (n);206 }207 208 void FuncAdd (Phone &p)209 {210 int n, pos;211 Person tmp;212 cout << "输入要添加的个数:"; cin >> n;213 for (int i=1; i<=n; i++)214 {215 cout << "输入第 " <<i<<" 个联系人的插入位置,姓名和电话:"; cin>>pos>>tmp.name>>tmp.telp;216 p.Insert(tmp, pos);217 }218 }219 220 void FuncRevamp (Phone &p)221 {222 int pos, n;223 Person tmp;224 cout << "输入要修改的个数: ";cin>>n;225 for (int i=1; i<=n; i++)226 {227 cout << "输入要修改的位置: ";cin>>pos;228 cout << "输入要修改的信息(姓名+号码): ";cin>>tmp.name>>tmp.telp;229 p.Revamp (tmp, pos);230 }231 }232 233 void FuncFind (Phone &p)234 {235 int n;236 cout << " ******************* \n"237 << " 1.按姓名查找 \n"238 << " 2.按号码查找 \n"239 << " 3.按位置查找 \n";240 cout << " ******************* \n";241 242 cout << " 输入查找方式: "; while (cin>>n, n!=1&&n!=2&&n!=3)cout<<"**重输**\n";243 244 p.Find (n);245 }246 247 void FuncSort (Phone &p)248 {249 int n;250 cout << " ***************** \n"251 << " 1.按号码增序排 \n"252 << " 2.按号码降序排 \n";253 cout << " ****************** \n";254 255 cout << "输入排序方式: "; while (cin>>n, n!=1&&n!=2)cout<<"**重输**\n";256 257 p.Sort (n);258 }259 260 void FuncDelete (Phone &p)261 {262 int pos;263 cout << "输入要删除的位置:";cin>>pos;264 p.Delete (pos);265 }266 //操作函数267 void Operator (Phone &p)268 {269 int op;270 while (1)271 {272 cout << "输入要进行的操作:\n";273 cin >> op;274 if (op == 1) FuncCreat(p);275 else if (op == 2) FuncAdd (p);276 else if (op == 3) FuncRevamp (p);277 else if (op == 4) FuncFind (p);278 else if (op == 5) p.Display (0);279 else if (op == 6) FuncSort (p);280 else if (op == 7) p.Display (1);281 else if (op == 8) FuncDelete (p);282 if (op==0) break;283 }284 }285 286 /************ 测试函数 *************/287 288 int main ()289 {290 291 Phone p;292 293 Meue ();294 295 Operator (p);296 297 return 0;298 }
数据结构课祖业------括号匹配检查
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 using namespace std; 6 7 #define Max 1005 8 9 typedef char StackItem;10 11 class Stack12 {13 public:14 Stack ():myTop(-1) {}15 bool empty () const { return myTop == -1; }16 void push (StackItem item);17 void pop ();18 StackItem top () const;19 private:20 StackItem myArray[Max];21 int myTop;22 };23 24 StackItem Stack::top () const25 {26 if (myTop == -1) return -1;27 return myArray[myTop];28 }29 30 void Stack::push (StackItem item)31 {32 if (myTop == Max)33 {34 cout << "栈已满,请删除后再入栈!\n";35 return;36 }37 myArray[++myTop] = item;38 }39 40 void Stack::pop ()41 {42 if (myTop==-1)43 return;44 myTop--;45 }46 47 int main ()48 {49 Stack opt;50 while (1)51 {52 cout << " ***请输入要检查的括号: ";53 StackItem braket[Max];54 bool error = false;55 gets (braket);56 for (int i=0; braket[i]; i++)57 {58 if (error) continue;59 60 if (braket[i]==‘(‘ || braket[i]==‘[‘ || braket[i]==‘{‘)61 opt.push (braket[i]);62 else63 {64 StackItem tmp = opt.top ();65 if (tmp==-1) {error = true; continue;}66 if (tmp==‘(‘ && braket[i]==‘)‘)67 {68 opt.pop ();69 continue;70 }71 if (tmp==‘[‘ && braket[i]==‘]‘)72 {73 opt.pop ();74 continue;75 }76 if (tmp==‘{‘ && braket[i]==‘}‘)77 {78 opt.pop ();79 continue;80 }81 error = true;82 }83 }84 if (error)85 cout << " --括号不匹配--\n";86 else87 cout << " --括号匹配正确--\n";88 }89 return 0;90 }
数据结构课作业------运算表达式求值
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <string> 5 using namespace std; 6 7 #define Max 1005 8 9 template <typename T> 10 class Stack 11 { 12 public: 13 Stack ():myTop(-1) { memset (myArray, 0, sizeof myArray); } 14 bool empty () const { return myTop == -1; } 15 void push (T item); 16 void pop (); 17 T top () const; 18 private: 19 T myArray[Max]; 20 int myTop; 21 }; 22 char tmp[25]; 23 24 template <typename T> void Stack <T>::push (T item) 25 { 26 if (myTop==Max) return; 27 myArray[++myTop] = item; 28 } 29 30 template <typename T> void Stack <T>::pop () 31 { 32 if (myTop == -1) return; 33 myTop--; 34 } 35 36 template <typename T> T Stack <T>::top () const 37 { 38 if (myTop == -1) return -1; 39 return myArray[myTop]; 40 } 41 42 char OptrRelation[7][7] = 43 { 44 {‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘}, 45 {‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘}, 46 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘}, 47 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘}, 48 {‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘=‘, ‘ ‘}, 49 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘ ‘, ‘>‘, ‘>‘}, 50 {‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘ ‘, ‘=‘} 51 }; 52 53 int Postion_Optr (char optr) 54 { 55 switch (optr) 56 { 57 case ‘+‘:return 0; 58 case ‘-‘:return 1; 59 case ‘*‘:return 2; 60 case ‘/‘:return 3; 61 case ‘(‘:return 4; 62 case ‘)‘:return 5; 63 case ‘#‘:return 6; 64 } 65 } 66 67 char Precede (char s1, char s2) 68 { 69 int coord_1 = Postion_Optr(s1); 70 int coord_2 = Postion_Optr(s2); 71 72 return OptrRelation[coord_1][coord_2]; 73 } 74 75 int Opera (int a, char op, int b) 76 { 77 if (op==‘-‘) return a-b; 78 if (op==‘+‘) return a+b; 79 if (op==‘*‘) return a*b; 80 if (op==‘/‘) return a/b; 81 } 82 83 int Expression (string exp) 84 { 85 Stack <char> Operator; 86 Stack <int> Number; 87 88 int i=0; 89 exp += "#"; 90 Operator.push (‘#‘); 91 92 char ch = exp[i++]; 93 while (ch!=‘#‘ || Operator.top()!=‘#‘) 94 { 95 if (ch>=‘0‘ && ch<=‘9‘) 96 { 97 int k=0, a=0; 98 while (ch<=‘9‘ && ch>=‘0‘) 99 {100 tmp[k++] = ch;101 ch = exp[i++];102 }103 tmp[k] = ‘\0‘;104 sscanf (tmp, "%d", &a);105 Number.push (a);106 }107 else108 {109 switch (Precede (Operator.top(), ch))110 {111 case ‘<‘:112 Operator.push (ch);113 ch = exp[i++];114 break;115 case ‘=‘:116 Operator.pop ();117 ch = exp[i++];118 break;119 case ‘>‘:120 char opt = Operator.top ();121 Operator.pop();122 int A = Number.top ();123 Number.pop();124 int B = Number.top ();125 Number.pop();126 Number.push (Opera (B, opt, A));127 break;128 }129 }130 131 }132 return Number.top();133 }134 135 int main()136 {137 string exp;138 while (1)139 {140 cout << "*** 请输入一个表达式 ***\n"141 "注意:以‘#‘结尾且中间不得有多余的空格 :\n";cin >> exp;142 143 int ans = Expression (exp);144 145 printf ("%d\n",ans);146 }147 return 0;148 }
数据结构课作业-----链式表。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 struct Node 7 { 8 char name[30]; 9 char telp[30]; 10 Node * next; 11 }; 12 13 class Stulist 14 { 15 public : 16 Stulist (); 17 ~Stulist (); 18 19 void Creat (int n); //建立n个元素的链式表 20 21 void Insert (Node item, int pos);//按位置插入元素item 22 23 void Find (int pos) const; //按位置查找 24 25 void Delete (int pos); //按位置删除 26 27 void Print () const; //打印所有数据 28 29 void Clear (); //清空链式表 30 private : 31 Node * first; 32 }; 33 34 void Stulist :: Clear () 35 { 36 Node * Pre = first; 37 while (Pre) 38 { 39 memset (Pre->name, 0, sizeof Pre->name); 40 memset (Pre->telp, 0, sizeof Pre->telp); 41 Pre = Pre->next; 42 } 43 } 44 45 void Stulist :: Print () const 46 { 47 Node * Pre = first->next; 48 int cnt=1; 49 cout << "******************************\n"; 50 while (Pre) 51 { 52 cout <<" "<< cnt <<". "<< Pre->name << " " << Pre->telp << endl; 53 Pre = Pre->next; 54 cnt++; 55 } 56 cout << "******************************\n"; 57 } 58 59 void Stulist :: Delete (int pos) 60 { 61 Node * Pre = first; 62 int cnt=0; 63 cout << "请输入要删除的位置: "; cin >> pos; 64 while (Pre && cnt<pos-1) 65 { 66 Pre = Pre->next; 67 cnt++; 68 } 69 if (Pre==NULL) 70 { 71 cout << "试图删除非法位置!\n"; 72 return; 73 } 74 Pre->next = Pre->next->next; 75 cout << "以成功删除第" << pos << " 个数据.\n"; 76 } 77 78 void Stulist :: Find (int pos) const 79 { 80 Node * Pre = first; 81 int cnt=0; 82 cout << "请输入要查找的位置: "; cin >> pos; 83 while (Pre && cnt<pos) 84 { 85 Pre = Pre->next; 86 cnt++; 87 } 88 if (Pre==NULL) 89 { 90 cout << "你要查找的数据不存在,请核对后再查找!\n"; 91 return; 92 } 93 cout << "你要查找的信息详细如下:\n"; 94 cout << "************************\n"; 95 cout << " 姓名:"<< Pre->name <<endl; 96 cout << " 号码:"<< Pre->telp <<endl; 97 cout << "************************\n"; 98 } 99 100 void Stulist::Insert (Node item, int pos)101 {102 Node * Pre=first, * Cur;103 cout << "请输入要插入的位置: "; cin >> pos;104 int cnt=0;105 while (Pre && cnt < pos-1)106 {107 Pre = Pre->next;108 cnt++;109 }110 if (Pre==NULL)111 {112 cout << "试图插入非法位置!\n";113 return;114 }115 cout << "请出入要插入的姓名:"; cin >> item.name;116 cout << "请出入要插入的号码:"; cin >> item.telp;117 Cur = new Node;118 strcpy (Cur->name, item.name);119 strcpy (Cur->telp, item.telp);120 Cur->next = Pre->next;121 Pre->next = Cur;122 cout << "插入成功!\n";123 }124 125 Stulist::Stulist ()126 {127 first = new Node;128 first->next = NULL;129 }130 131 Stulist :: ~Stulist ()132 {133 Node * pre = first;134 while (first)135 {136 pre = first;137 first = pre->next;138 delete pre;139 }140 }141 void Stulist::Creat (int n)142 {143 Node *Pre, *Cur;144 first = new Node;145 Pre = first;146 cout << "请输入要建立表的大小: "; cin >> n;147 for (int i=0; i<n; i++)148 {149 Cur = new Node;150 cout << "请输入第 " << i+1 << " 个数据:"; cin >> Cur->name >> Cur->telp;151 Pre->next = Cur;152 Pre = Cur;153 }154 Pre->next = NULL;155 }156 157 void Meue ()158 {159 cout<<" \n ~~~~~~~~~~~~~~请选择您要执行的操作:~~~~~~~~~~~~~~\n"; 160 cout<<endl<<endl; 161 cout<<" `````````````````````````````````````````````\n"; 162 cout<<" ` 1、清空顺序表 `\n"; 163 cout<<" ` 2、创建顺序表 `\n"; 164 cout<<" ` 3、插入信息 `\n"; 165 cout<<" ` 4、删除信息 `\n"; 166 cout<<" ` 5、查找信息 `\n"; 167 cout<<" ` 6、显示当前信息 `\n"; 168 cout<<" ` 0、退出系统 `\n"; 169 cout<<" `````````````````````````````````````````````\n"; 170 cout<<"\n请从序号0--6中选择:"; 171 }172 173 void Operater (Stulist &Link)174 {175 Meue ();176 int op, pos, n;177 Node item;178 while (1)179 {180 while (cin >> op, (op<0||op>6)) cout << "操作不合法: \n";181 switch (op)182 {183 case 1:184 Link.Clear (); break;185 case 2:186 Link.Creat (n); break;187 case 3:188 Link.Insert (item, pos); break;189 case 4:190 Link.Delete (pos); break;191 case 5:192 Link.Find (pos); break;193 case 6:194 Link.Print (); break;195 default:break;196 }197 }198 cout << "~~~~~~~~~欢迎下次使用~~~~~~~~~\n";199 }200 201 202 //*************** 主程序 **************203 int main ()204 {205 Stulist Link;206 207 Operater (Link);208 209 return 0;210 }
数据结构课作业系列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。