当前位置:   article > 正文

用线性表实现的通讯录管理 C++代码_线性表通讯录

线性表通讯录
  1. /****************************************/
  2. /*主控菜单处理测试程序main2.c************/
  3. /***************************************/
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. #define LIST_INIT_SIZE 100
  8. #define LISTINCREMENT 10
  9. int OK=1;
  10. int OVERFLOW=0;
  11. int ERROR=0;
  12. typedef struct
  13. { //通讯录结点类型
  14. char num[10]; //编号
  15. char name[20]; //姓名
  16. char sex[6]; //性别
  17. char phone[13]; //电话
  18. char addr[31]; //地址
  19. } DataType;
  20. typedef struct
  21. {
  22. DataType *elem;
  23. int length;//当前长度
  24. int listsize;//线性表的长度
  25. }SqList;
  26. /*******************/
  27. /* 菜单选择函数程序 */
  28. /***************************/
  29. int menu_select( )
  30. {
  31. int choice;
  32. cout<<" 通讯录管理系统 /n";
  33. cout<<"======================================================================/n";
  34. cout<<"1.建立/t"<<"2.插入/t"<<"3.查询/t"<<"4.删除/t"<<"5.输出/t"<<"0.退出/n";
  35. cout<<"======================================================================/n";
  36. cout<<"请 选 择 0-5: ";
  37. for( ; ; )
  38. {
  39. cin>>choice;
  40. if (choice<0||choice>5)
  41. cout<<"输入错误,重选0-5:";
  42. else
  43. break;
  44. }
  45. cout<<endl;
  46. return choice;
  47. }
  48. void InitList(SqList &L)
  49. {
  50. L.elem=(DataType *)malloc(LIST_INIT_SIZE*sizeof(DataType));
  51. if(!L.elem)
  52. exit(OVERFLOW);
  53. L.length=0;
  54. L.listsize=LIST_INIT_SIZE;//以上步骤为初始化
  55. }
  56. /**************************/
  57. /*建立通讯录线性表函数 */
  58. /**************************/
  59. void CreateList(SqList &L)
  60. {
  61. L.elem=(DataType *)malloc(LIST_INIT_SIZE*sizeof(DataType));
  62. if(!L.elem)
  63. exit(OVERFLOW);
  64. L.length=0;
  65. L.listsize=LIST_INIT_SIZE;//因为建立一个通讯录,即重新开始建立一个,所以要初始化
  66. //使不和以前的通讯录相混淆。
  67. int i=0;
  68. int flag=1;
  69. while(flag==1)
  70. {
  71. cout<<"/n编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
  72. cin>>L.elem[i].num>>L.elem[i].name>>L.elem[i].sex>>L.elem[i].phone>>L.elem[i].addr;
  73. i++;
  74. L.length++;
  75. cout<<"/n是否还要输入?(0 or 1):";
  76. cin>>flag;
  77. cout<<endl;
  78. }//输入信息
  79. }
  80. /******************************/
  81. /*在通讯录线性表中插入元素 */
  82. /******************************/
  83. int InsertNode(SqList &L,int i,DataType x)
  84. {
  85. /*特殊情况的处理*/
  86. if(i<1||i>L.length+1)//这里L.length1的目的是,用户可能会在最后边接上元素,并不插,
  87. //只是顺序接到最后面
  88. return ERROR;
  89. DataType * newbase;
  90. DataType * p,* q;
  91. if(L.length>=L.listsize)
  92. {
  93. newbase=(DataType *)realloc(L.elem, (L.listsize+LISTINCREMENT)*sizeof(DataType));
  94. if(!newbase)
  95. exit(OVERFLOW);
  96. L.elem=newbase;
  97. L.listsize+=LISTINCREMENT;
  98. }
  99. /*插入操作*/
  100. q=&(L.elem[i-1]);//q为第i个元素的位置
  101. for(p=&(L.elem[L.length-1]); p>=q; p--)
  102. {
  103. strcpy((p+1)->num,p->num);
  104. strcpy((p+1)->name,p->name);
  105. strcpy((p+1)->sex,p->sex);
  106. strcpy((p+1)->phone,p->phone);
  107. strcpy((p+1)->addr,p->addr);//i-1之后的元素依次后移一位
  108. }
  109. strcpy(q->num,x.num);
  110. strcpy(q->name,x.name);
  111. strcpy(q->sex,x.sex);
  112. strcpy(q->phone,x.phone);
  113. strcpy(q->addr,x.addr);
  114. //----------------------------------------------------------------------------------------
  115. //“->”和“.”的区别:“->”是用于指针的,而“.”是用于对象调用的。
  116. //----------------------------------------------------------------------------------------
  117. L.length++;
  118. return OK;
  119. }
  120. /******************************************/
  121. /* 有序通讯录线性表的查找 */
  122. /******************************************/
  123. int ListFind(SqList &L)
  124. {// 有序通讯录线性表上的查找
  125. int i=1;
  126. int xz;
  127. DataType *p;
  128. p=L.elem;
  129. char SNum[5];
  130. char SName[9];
  131. do
  132. {
  133. cout<<"1 按编号查询 2 按姓名查询:";
  134. cin>>xz;
  135. cout<<endl;
  136. if(xz!=1&&xz!=2)
  137. cout<<"输入错误!/n/n";
  138. }while(xz!=1&&xz!=2);
  139. if(xz==1)
  140. {
  141. int j=0;
  142. cout<<"输入编号:";
  143. cin>>SNum;
  144. cout<<endl;
  145. while(i<=L.length && strcmp(p[i-1].num,SNum)!=0)
  146. i++;
  147. if(i>L.length)
  148. return 0;
  149. else
  150. return i;
  151. }
  152. else
  153. {
  154. int j;
  155. cout<<"输入姓名:";
  156. cin>>SName;
  157. cout<<endl;
  158. while(i<=L.length && strcmp(p[i-1].name,SName)!=0)
  159. i++;
  160. if(i>L.length)
  161. return 0;
  162. else
  163. return i;
  164. }
  165. }
  166. /*******************************/
  167. /* 通讯录线性表上的结点删除 */
  168. /*********************************/
  169. int DelNode(SqList &L)
  170. {
  171. DataType * p;
  172. DataType * q;
  173. int i;
  174. cout<<"要删除哪个位置上的元素?:";
  175. cin>>i;
  176. cout<<endl;
  177. if(i<1||i>L.length)
  178. return ERROR;//特殊情况的处理
  179. p=&(L.elem[i-1]);
  180. q=L.elem+L.length-1;
  181. for(p;p<q;p++)
  182. {
  183. strcpy(p->num,(p+1)->num);//i-1之后的元素依次后移一位
  184. strcpy(p->name,(p+1)->name);
  185. strcpy(p->sex,(p+1)->sex);
  186. strcpy(p->phone,(p+1)->phone);
  187. strcpy(p->addr,(p+1)->addr);
  188. }//i之后的元素依次左移
  189. L.length--;
  190. return OK;
  191. }
  192. /**********************************/
  193. /* 通讯录线性表的输出函数 */
  194. /**********************************/
  195. void PrintList(SqList &L)
  196. {
  197. int i;
  198. for(i=0;i<L.length;i++)
  199. {
  200. cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "
  201. <<L.elem[i].phone<<" "<<L.elem[i].addr<<endl;
  202. }
  203. if(L.length==0)
  204. cout<<"通讯录中没有元素!/n";
  205. cout<<endl;
  206. }
  207. //主函数
  208. void main()
  209. {
  210. SqList L;
  211. InitList(L);
  212. for( ; ; )
  213. {
  214. switch(menu_select( ) )
  215. {
  216. case 1:
  217. cout<<"**********************************/n";
  218. cout<<"* 通 讯 录 线 性 表 的 建 立 */n";
  219. cout<<"**********************************/n";
  220. CreateList(L);
  221. break;
  222. case 2:
  223. cout<<"**********************************/n";
  224. cout<<"* 通 讯 者 信 息 的 添 加 */n";
  225. cout<<"**********************************/n";
  226. cout<<endl;
  227. cout<<"编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
  228. DataType p; //申请新结点
  229. cin>>p.num>>p.name>>p.sex>>p.phone>>p.addr;
  230. int i;
  231. int m;
  232. cout<<"想插到哪个位置上? ";
  233. cin>>i;
  234. cout<<endl;
  235. m=InsertNode(L,i,p);
  236. if(m==ERROR)
  237. cout<<"你输入的元素位置超过了界限!/n/n";
  238. else
  239. cout<<"已经插入了该元素!/n/n";
  240. break;
  241. case 3:
  242. int a;
  243. cout<<"**********************************/n";
  244. cout<<"* 通 讯 者 信 息 的 查 询 */n";
  245. cout<<"**********************************/n";
  246. a=ListFind(L);
  247. if(a!=0)
  248. {
  249. cout<<"编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
  250. cout<<L.elem[a-1].num<<" "<<L.elem[a-1].name<<" "<<L.elem[a-1].sex<<" "
  251. <<L.elem[a-1].phone<<" "<<L.elem[a-1].addr<<endl;
  252. cout<<endl;
  253. }
  254. else
  255. cout<<"没有查到要查询的通讯者!/n/n";
  256. break;
  257. case 4:
  258. int b;
  259. cout<<"**********************************/n";
  260. cout<<"* 通 讯 者 信 息 的 删 除 */n";
  261. cout<<"**********************************/n";
  262. b=DelNode(L); //删除结点
  263. if(b==0)
  264. cout<<"你输入的元素位置超过界限!/n/n";
  265. else
  266. cout<<"已经成功删除了该元素!/n/n";
  267. break;
  268. case 5:
  269. cout<<"**********************************/n";
  270. cout<<"* 通 讯 者 信 息 的 输 出 */n";
  271. cout<<"**********************************/n";
  272. PrintList(L);
  273. break;
  274. case 0:
  275. cout<<"/t 再 见! /n";
  276. return;
  277. }
  278. }
  279. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/559457
推荐阅读
相关标签
  

闽ICP备14008679号