数据结构实验1——顺序表

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <fstream>
  7 #include <iomanip>
  8 #include <string>
  9 #include <wchar.h>
 10 #include <Windows.h>
 11 using namespace std;
 12 
 13 #define ok 1
 14 #define error 0
 15 #define overflow -2
 16 typedef int elemtype;
 17 const int maxn = 100;
 18 typedef int status;
 19 typedef struct{
 20     char no[20];
 21     char name[80];
 22     elemtype grade;
 23 }student;
 24 typedef struct{
 25     student *elem;
 26     int length;
 27 }sqlist;
 28 
 29 int main()
 30 {
 31     SetConsoleOutputCP(65001);
 32     sqlist l;
 33     int i, choose;
 34     char name[80];
 35 
 36     status init(sqlist &l);
 37     status creat(sqlist &l);
 38     status show(sqlist l);
 39     status locate(sqlist l, char *name);
 40     status get(sqlist l, int i);
 41     status in(sqlist &l, int i, student e);
 42     status out(sqlist &l, int i);
 43     //status length(sqlist l);
 44     status destroy(sqlist &l);
 45 
 46     cout<<"-------------"<<endl;
 47 
 48     cout<<"1.初始化链表"<<endl;
 49     cout<<"2.构建链表"<<endl;
 50     cout<<"3.显示链表长度"<<endl;
 51     cout<<"4.显示链表内容"<<endl;
 52     cout<<"5.查找学生信息"<<endl;
 53     cout<<"6.获取学生信息"<<endl;
 54     cout<<"7.插入学生信息"<<endl;
 55     cout<<"8.删除学生信息"<<endl;
 56     cout<<"9.销毁链表"<<endl;
 57     cout<<"0.退出"<<endl;
 58     cout<<"-------------"<<endl;
 59     choose = -1;
 60 
 61     while(choose != 0){
 62         cout<<"请选择:";
 63         cin>>choose;
 64         switch(choose){
 65             case 1://创建链表
 66                 {
 67                 if(init(l)) cout<<"成功建立链表\n\n";
 68                 else cout<<"链表建立失败\n\n";
 69                 break;
 70             }
 71             case 2://构建链表
 72                 {
 73                 if(creat(l)) cout<<"链表构建成功\n\n";
 74                 else cout<<"链表构建失败\n\n";
 75                 break;
 76             }
 77             case 3://链表长度
 78                 {
 79                 cout<<"链表长度为"<<l.length<<endl<<endl;
 80                 break;
 81             }
 82             case 4://显示链表内容
 83                 {
 84                 int a = show(l);
 85                 if(!a) cout<<"无内容"<<endl<<endl;
 86                 else cout<<"打印完成"<<endl<<endl;
 87                 break;
 88             }
 89             case 5://查找学生信息
 90                 {
 91                 printf("请输入需要获取信息的学生名字:\n");
 92                 gets(name);
 93                 int a = locate(l, name);
 94                 if(!a) cout<<"不存在"<<endl<<endl;
 95                 else cout<<a<<endl<<endl<<endl;
 96                 break;
 97             }
 98             case 6://获取学生信息
 99                 {
100                 printf("请输入需要获取信息的学生编号:\n");
101                 cin>>i;
102                 if(!get(l, i)) cout<<"编号不合法"<<endl<<endl;
103                 else cout<<get(l, i)<<endl<<endl;
104                 break;
105             }
106             case 7://插入学生信息
107                 {
108                 printf("请输入要插入学生的编号:\n");
109                 cin>>i;
110                 student e;
111                 printf("请输入要插入学生的姓名:\n");
112                 cin>>e.name;
113                 printf("请输入要插入学生的学号:\n");
114                 cin>>e.no;
115                 printf("请输入要插入学生的成绩:\n");
116                 cin>>e.grade;
117                 if(in(l, i, e)) cout<<"插入成功"<<endl<<endl;
118                 else cout<<"插入失败"<<endl<<endl;
119                 break;
120             }
121             case 8://删除学生信息
122                 {
123                 printf("请输入要删除学生的编号:\n");
124                 cin>>i;
125                 if(out(l, i)) cout<<"删除成功"<<endl<<endl;
126                 else cout<<"删除失败"<<endl<<endl;
127                 break;
128             }
129             case 9://销毁链表
130                 {
131                 if(destroy(l)) cout<<"成功删除链表\n\n"<<endl<<endl;
132                 else  cout<<"链表删除失败\n\n"<<endl<<endl;
133                 break;
134             }
135         }
136     }
137 }
138 
139 status creat(sqlist &l){//构建链表
140     if(!l.elem) exit(overflow);
141     if(l.length == 0) return error;
142     do{
143         //char no[20], name[80];
144         //int grade;
145         printf("输入学号,姓名,成绩:\n输入0 0 0时结束\n");
146         scanf("%s%s%d", l.elem[l.length].no, l.elem[l.length].name, &l.elem[l.length].grade);
147         l.length ++ ;
148     }while(strcmp(l.elem[l.length-1].no, "0") != 0);
149     l.length -- ;
150     return ok;
151 }
152 
153 status destroy(sqlist &l){//销毁
154     if(l.elem) delete(l.elem);
155     l.length = 0;
156     return ok;
157 }
158 
159 status init(sqlist &l){//初始化
160     l.elem = new student[100];
161     if(!l.elem) exit(overflow);
162     l.length = 0;
163     return ok;
164 }
165 
166 status show(sqlist l){//显示内容
167     if(l.length == 0) return error;
168     else {
169         for(int i = 0 ; i < l.length; i ++ ){
170             printf("姓名:%s 学号:%s 成绩:%d\n\n", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
171         }
172         return ok;
173     }
174 }
175 
176 status get(sqlist l, int i){//获取学生信息
177     if(i < 0 || i > l.length) return error;
178     else{
179         printf("姓名:%s 学号:%s 成绩:%d", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
180         return ok;
181     }
182 }
183 
184 status locate(sqlist l, char *n){//查找学生信息
185     for(int i = 0; i < l.length; i ++ ){
186         if(strcmp(l.elem[i].name, n) == 0) {
187             printf("姓名:%s 学号:%s 成绩:%d", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
188             return i + 1;
189         }
190     }
191     return error;
192 }
193 
194 status in(sqlist &l, int i, student e){//插入
195     if(i < 1 || i > (l.length + 1)) return error;
196     else if(l.length == maxn) return error;
197     for(int j = l.length - 1; j >= i - 1; j ++ ){
198         l.elem[j + 1] = l.elem[j];
199     }
200     l.elem[i - 1] = e;
201     ++ l.length;
202     return ok;
203 }
204 
205 status out(sqlist &l, int i){//删除
206     if(i < 1 || i > (l.length + 1)) return error;
207     for(int j = i; j <= l.length - 1; j ++ )
208         l.elem[j - 1] = l.elem[j];
209     -- l.length;
210     return ok;
211 }

注意中文乱码(

 订正:初始化函数中应该为new student[maxn];

Window.h 应为windows.h

wchar.h 应为 cwchar

posted @ 2019-09-18 18:13  moomight  阅读(1045)  评论(0编辑  收藏  举报