DC顺序表——通讯录的实现

引言

顺序表是一种基本的数据结构,它使用连续的存储空间来存放具有相同类型的数据元素。在许多编程语言中,顺序表可以用数组来实现。由于其元素在内存中紧密排列,顺序表支持快速地通过索引访问任何位置上的元素。

通讯录的实现

要求:

  • 能够存放100人的信息
  • 能够保存用户的名字,年龄,性别,电话和住址
  • 可以增加,删除,修改,查找,显示联系人信息

接下来给出该项目各个文件的代码ovo

SeqList.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "contact.h"
typedef PeoInfo SLDataType;
typedef struct SeqList
{
	SLDataType* arr;//指针
	int capacity;//空间容量
	int size;//有效数据个数
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);
void SLPrint(SL* ps);
//检查容量-是否扩容函数
void SLCheckCapacity(SL*ps);
//头部插入
void SLPushFront(SL* ps,SLDataType x);
//尾部插入
void SLPushBack(SL* ps, SLDataType x);
//头部删除
void SLPopFront(SL* ps);
//尾部删除
void SLPopBack(SL* ps);
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
//指定位置删除数据
void SLErase(SL* ps, int pos);
//查找数据
int SLfind(SL* ps, SLDataType x);
//销毁顺序表
void SLDestroy(SL* ps);

SeqList.c

#include "SeqList.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}//初始化

void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}//打印元素

//扩容
void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4: 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			return 1;
		}
		//扩容成功
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}
//头部插入
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//检查是否需要扩容
	for (int i = ps->size; i>0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}//旧数据往后面挪动一位
	ps->arr[0] = x;
	ps->size++;
}

//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}

//头部删除
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);//确保顺序表不为空
	for (int i = 0; i < ps->size - 1; i--)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//尾部删除
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);//确保顺序表不为空
	ps->size--;//使得尾部数据无效即可
}

//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i =ps->size; i>pos; i--)//pos位置及以后的数据往后挪
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

//指定位置删除数据
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos>=0 &&pos<ps->size);//确保顺序表不为空
	for (int i = pos; i <ps->size-1 ; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//查找数据
//int SLfind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			return i;//找到了返回下标位置
//		}
//	}
//	return -1;
//}

//销毁
void SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);//不为空才释放
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

contact.h

#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100

//前置声明
typedef struct SeqList contact;//把SeqList重命名为contact ,顺序表变通讯录
//用户数据
typedef struct PersonInfo
{
	char name[100];
	int age;
	char gender[10];
	char tel[20];
	char addr[20];
}PeoInfo;

//初始化通讯录
void InitContact(contact* con);

//添加通讯录数据

void AddContact(contact* con);

//删除通讯录数据

void DelContact(contact* con);

//展示通讯录数据

void ShowContact(contact* con);

//查找通讯录数据

void FindContact(contact* con);

//修改通讯录数据

void ModifyContact(contact* con);

//销毁通讯录数据

void DestroyContact(contact* con);

contact.c

//实现通讯录功能
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"


void LoadContact(contact* con)
{
	FILE* pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen error\n");
		return;
	}

	//循环读取数据文件
	PeoInfo info;
	while (fread(&info, sizeof(PeoInfo), 1, pf))
	{
		SLPushBack(con, info);
	}
	printf("历史数据导入通讯录成功");
}

void InitContact(contact* con)
{
	SLInit(con);
	LoadContact(con);
}

//添加到通讯录
void AddContact(contact* con)
{
	PeoInfo info;
	printf("请输入姓名:\n");
	scanf("%s", &info.name);
	printf("请输入年龄:\n");
	scanf("%d", &info.age);
	printf("请输入性别:\n");
	scanf("%s", &info.gender);
	printf("请输入电话:\n");
	scanf("%s", &info.tel);
	printf("请输入地址:\n");
	scanf("%s", &info.addr);

	SLPushBack(con, info);
	printf("插入成功\n");
}

//展示通讯录

void ShowContact(contact* con)
{
	printf("%s  %s  %s  %s  %s\n", "姓名", "年龄", "性别", "电话", "地址");
	for (int i = 0; i < con->size;i++)
	{
		printf("%s  %d  %s  %s  %s\n",
			con->arr[i].name,
			con->arr[i].age,
			con->arr[i].gender,
		    con->arr[i].tel,
			con->arr[i].addr);
	}
}

//由于在删除 修改之前都要先看看这个数据是否存在,以姓名为查找依据

int FindByName(contact* con, char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].name, name))
		{
			return i;//找到了则返回下标
		}
	}
	return -1;//找不到返回-1
}

//删除通讯录中某一元素
void DelContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要删除的用户的姓名:\n");
	scanf("%s", name);
	
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要删除的用户不存在,删除失败\n");
		return;
	}
	SLErase(con, pos);
	printf("删除成功\n");
}

//添加某一元素到通讯录
void ModifyContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的用户的姓名:\n");
	scanf("%s", name);

	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要修改的用户不存在,修改失败\n");
		return;
	}

	PeoInfo info;
	printf("请输入姓名:\n");
	scanf("%s", &con->arr[pos].name);
	printf("请输入年龄:\n");
	scanf("%d", &con->arr[pos].age);
	printf("请输入性别:\n");
	scanf("%s", &con->arr[pos].gender);
	printf("请输入电话:\n");
	scanf("%s", &con->arr[pos].tel);
	printf("请输入地址:\n");
	scanf("%s", &con->arr[pos].addr);

	printf("修改成功\n");

}

//查找某一元素

void FindContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找的用户的姓名:\n");
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要查找的用户不存在,查找失败\n");
		return;
	}
	printf("查找成功\n");
	printf("%s  %d  %s  %s  %s\n",
		con->arr[pos].name,
		con->arr[pos].age,
		con->arr[pos].gender,
		con->arr[pos].tel,
		con->arr[pos].addr);
}

//在要销毁通讯录时保存数据
void SaveContact(contact* con)
{
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen error\n");
		return;
	}
	//将通讯录数据写入文件
	for (int i = 0; i < con->size; i++)
	{
		fwrite(con->arr + i, sizeof(PeoInfo), 1, pf);
	}
	printf("通讯录数据保存成功\n");
}

//销毁通讯录
void DestroyContact(contact* con)
{
	SaveContact(con);
	SLDestroy(con);
}

test.c

#include "SeqList.h"
#define _CRT_SECURE_NO_WARNINGS 1
void menu()
{
	//通讯录初始化
	contact con;
	InitContact(&con);
	int op = -1;
	do
	{
		printf("**************通讯录*************\n");
		printf("*****1.添加用户   2.删除用户*****\n");
		printf("*****3.查找用户   4.修改用户*****\n");
		printf("*****5.展示用户   0.退出*********\n");
		printf("*********************************\n");
		printf("请选择你的操作:\n");
		scanf_s("%d", &op);
		switch (op)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
			DelContact(&con);
			break;
		case 3:
			FindContact(&con);
			break; 
		case 4:
			ModifyContact(&con);
			break; 
		case 5:
			ShowContact(&con);
			break; 
		case 0:
			return;
		default:
			printf("非法输入,请重新输入\n");
			break;
		}
	} while (op != 0);
	//退出以后 销毁通讯录
	DestroyContact(&con);
}

int main()
{
	menu();
	return 0;
}

简单的通讯录小项目就完成啦!感兴趣的小伙伴快去试试看吧~~

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Linux下使用DC进行ASIC设计,一般需要以下几个步骤: 1. 安装DC:首先需要从Synopsys官网下载DC的安装包,然后按照安装指南进行安装。安装完成后需要设置环境变量,将DC的bin目录加入到系统的PATH中。 2. 创建设计库:使用DC需要先创建设计库,可以使用以下命令创建: ``` > dc_shell DC> create_library <library_name> -technology <tech_name> -vendor <vendor_name> ``` 其中,`<library_name>`为设计库名称,`<tech_name>`和`<vendor_name>`为芯片工艺和芯片厂商名称,可以根据需要进行修改。 3. 导入设计:将设计文件导入到设计库中,可以使用以下命令: ``` DC> read_file <verilog_file> ``` 其中,`<verilog_file>`为Verilog格式的设计文件路径。 4. 进行逻辑综合:使用以下命令进行逻辑综合: ``` DC> compile <top_module> ``` 其中,`<top_module>`为顶层模块的名称。 5. 进行后端物理设计:逻辑综合完成后,需要进行后端物理设计,包括布局、布线和时序优化等。可以使用DC的后端工具完成这些任务,例如Floorplan、Place-and-Route和PrimeTime等。 6. 生成最终版图:后端物理设计完成后,生成最终版图,可以使用以下命令: ``` DC> write -format verilog -hierarchy -output <output_file> ``` 其中,`<output_file>`为输出版图文件的路径。 以上是使用DC进行ASIC设计的基本步骤,具体操作需要根据实际情况进行调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值