想法和构思:
顺序表说白了底层是数组配套方法,通讯录说白了就是顺序表配套增删查改.
今后在设计复杂的数据结构时,一定不要先上手,先把基本的复用结构设计好,这是最重要的
代码部分:包含对上一篇文章顺序表方法结构的测试+通讯录方法与结构的设计与测试
A部分--对顺序表的测试:
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void text1()
{
SeqList S1;
SeqListInit(&S1);
SeqListPushBack(&S1, 1);
SeqListPushBack(&S1, 2);
SeqListPushBack(&S1, 3);
SeqListPushFront(&S1, 99);
int find1 = SeqListFind(&S1, 100);
find1 = SeqListFind(&S1, 99);
//SeqListPopBack(&S1);
//SeqListPopFront(&S1);
//SeqListPrint(&S1);
SeqListInsert(&S1, 1, 100);
SeqListErase(&S1, 2);
/*printf("%d", S1.size);*/
SeqListPrint(&S1);
SeqListDestory(&S1);
}
int main()
{
text1();
return 0;
}
B部分--contacts头文件
#pragma once
#define TEL_MAX 20
#define ADDR_MAX 20
#define GENDER_MAX 20
#define NAME_MAX 10
typedef struct personInfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}peoInfo;
typedef struct SeqList Contact;
void ContactsInit(Contact* con);
void ContactsDestroy(Contact* con);
void ContactsAdd(Contact* con);
void ContactsDel(Contact* con);
void ContactsModify(Contact* con);
void ContactsFind(Contact* con);
void ContactsShow(Contact* con);
C部分--contacts方法实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "contacts.h"
#include "SeqList.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
void ContactsInit(Contact* con)
{
SeqListInit(con);
}
void ContactsDestroy(Contact* con)
{
SeqListDestory(con);
}
void ContactsAdd(Contact* con)
{
CheckCapacity(con);
peoInfo p1;
printf("请输入新添加的名字\n");
scanf("%s", p1.name);
printf("请输入新添加的性别\n");
scanf("%s", p1.gender);
printf("请输入新添加的年龄\n");
scanf("%d", &p1.age);
printf("请输入新添加的电话\n");
scanf("%s", p1.tel);
printf("请输入新添加的地址\n");
scanf("%s", p1.addr);
SeqListPushBack(con,p1);
}
int FindbyName(Contact* con,char* s1)
{
int i = 0;
for (i = 0; i < con->size; i++)
{
if (0 == strcmp(s1, con->a[i].name))
{
return i;
}
}
return -1;
}
void ContactsDel(Contact* con)
{
char s1[NAME_MAX];
while (1)
{
printf("请输入你想删除的联系人姓名:\n");
scanf("%s", s1);
int find = FindbyName(con, s1);
if (find == -1)
{
printf("没找到请重新输入\n");
continue;
}
SeqListErase(con, find);
printf("删除成功\n");
break;
}
}
void ContactsFind(Contact* con)
{
char s1[NAME_MAX];
printf("请输入想要查找的联系人信息的名字:\n");
scanf("%s", &s1);
int find = FindbyName(con, s1);
if (find == -1)
{
printf("没找到不存在\n");
return;
}
printf("找到了兄弟\n");
int i = find;
printf("%s %s %s %s %s\n", "名字", "性别", "年龄", "电话", "住址");
printf("%5s", con->a[i].name);
printf("%5s", con->a[i].gender);
printf("%5d", con->a[i].age);
printf("%5s", con->a[i].tel);
printf("%5s", con->a[i].addr);
}
void ContactsModify(Contact* con)
{
char s1[NAME_MAX];
printf("请输入要修改的联系人姓名:");
scanf("%s", s1);
int find = FindbyName(con, s1);
if (find == -1)
{
printf("没找到不存在\n");
return;
}
else
{
printf("请输入新修改的名字\n");
scanf("%s", con->a[find].name);
printf("请输入新修改的性别\n");
scanf("%s", con->a[find].gender);
printf("请输入新修改的年龄\n");
scanf("%d", &con->a[find].age);
printf("请输入新修改的电话\n");
scanf("%s", con->a[find].tel);
printf("请输入新修改的地址\n");
scanf("%s", con->a[find].addr);
printf("修改完成,修改后的通讯录信息如下:\n");
ContactsShow(con);
}
}
void ContactsShow(Contact* con)
{
printf("%s %s %s %s %s\n", "名字", "性别", "年龄", "电话", "住址");
int i = 0;
while (con->a && i < con->size )
{
printf("%5s", con->a[i].name);
printf("%5s", con->a[i].gender);
printf("%5d", con->a[i].age);
printf("%5s", con->a[i].tel);
printf("%5s", con->a[i].addr);
i++;
printf("\n");
}
}
D部分--contacts代码测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
#include "contacts.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main()
{
Contact c1;
ContactsInit(&c1);
ContactsAdd(&c1);
ContactsAdd(&c1);
ContactsShow(&c1);
//ContactsDel(&c1);
//ContactsShow(&c1);
//ContactsModify(&c1);
ContactsFind(&c1);
ContactsDestroy(&c1);
return 0;
}
注意事项:
1.头文件无法相互包含,此时对一些引用不到的部分应该使用前置声明
这个时候可能有朋友会问啊主播主播这前置声明有啥用啊有不把定义的结构体复制过来,我说兄弟,头文件它不实现啊,具体实现在.c啊,你想怎么声明不行.c文件才是包含两个头文件啊,以后会玩了么.
2.关于printf函数格式化的一些总结,看博主的另一篇文章链接:
3.VS2022使用的快捷键:执行,调试逐语句,调试逐过程,跳到定义,Ctrl + Shift + V 查看剪贴板,打开文件:Ctrl + O,Ctrl + Shift + Tab,跳转到链接主播还没写好
4.文件操作,跳转到链接:主播还没写好
5.固定数据结构:增删查改,初始化销毁展示
6.关于字符匹配函数strcmp使用.链接跳转.

被折叠的 条评论
为什么被折叠?



