当前位置: 首页 > news >正文

嵌入式编程——数据结构与linux编程

根据dict.txt文件中的内容,用户输入单词获得单词含义
        

#include "public.h"

int main(void)
{
	struct list_head *parray = NULL;
	FILE *fp = NULL;
	char *nret = NULL;
	char *pword = NULL;
	char *pparaphrase = NULL;
	int num = 0;
	char str[1024] = {0};
	char word[256] = {0};


	fp = fopen("./dict.txt", "r");
	if(fp == NULL)
	{
		ERR_MSG("打开失败\n");
		return -1;
	}

	parray = create_hashtable();
	

	while(1)
	{
		nret = fgets(str, sizeof(str), fp);
		{
			if(nret == NULL)
			{
				break;
			}
		}

		pword = str;
		pparaphrase = str;
		while(1)//定位单词
		{
			if(*pparaphrase == ' ')
			{
				*pparaphrase = '\0';
				break;
			}
			pparaphrase++;
		}

		
		while(1)//定位释义
		{
			pparaphrase++;
			if(*pparaphrase != ' ')
			{
				break;
			}
		}

		insert_hashtable(parray, pword, pparaphrase);
	}


	//show_hashtable(parray);
	while(1)
	{
		printf("输入1查询单词 任意键数字退出\n");
		scanf("%d", &num);
		getchar();

		if(num == 1)
		{
			printf("请输入要查询的单词\n");
			gets(word);
			
			if(find_hashtable(parray, word) != NULL)
			{
				printf("%s \n", find_hashtable(parray, word)->paraphrase);
			}
			else
			{
				printf("未找到\n");
			}
		}
		else
		{
			break;
		}
	}

	destroy_hashtable(parray);
	parray = NULL;
	fclose(fp);

	return 0;
}

#ifndef __PUBLIC_H__
#define __PUBLIC_H__

#include "list.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>

//结构体
typedef struct hashnode 
{
    struct list_head node;
    char word[128];
    char paraphrase[1024];
}hashnode_t;

//打印出错信息
#define ERR_MSG(message) do { \
    printf("[%s%7d]%s:%s\n", __FILE__, __LINE__, message, strerror(errno)); \
}while (0)

//更新开始计时时间
#define GET_START_TIME      do { \
    gettimeofday(&starttime, NULL); \
} while (0)

//更新结束计时时间
#define GET_END_TIME      do { \
    gettimeofday(&endtime, NULL); \
} while (0)

//获得耗时时间
#define GET_CONST_TIME  (((endtime.tv_sec * 1000000 + endtime.tv_usec) - (starttime.tv_sec * 1000000 + starttime.tv_usec)) / 1000.0)

//哈希结构体参数
#define MAXNUM      52

//声明

extern struct list_head *create_hashtable(void);
extern int insert_hashtable(struct list_head *pheadlist, char *pstr, char *pstd);
extern int show_hashtable(struct list_head *pheadlist);
extern hashnode_t *find_hashtable(struct list_head *pheadlist, char *pstr);
extern int delete_hashtable(struct list_head *parray, char *pstr);
extern int destroy_hashtable(struct list_head *parray);


struct timeval starttime;
struct timeval endtime;

#endif

相关文章:

  • 测试常见问题汇总-检查表(持续完善)
  • 【C++游戏开发-五子棋】
  • 【SQL】SQL约束
  • 大模型开发框架对比:LangChain、LlamaIndex 及其他
  • golang如何将结构体和函数进行绑定?
  • AI 时代:探索大语言模型与核心技术
  • vue的data为什么是object
  • Unreal5从入门到精通之如何在 C++ 中创建 UserWidget
  • 贪吃蛇解析
  • 前端JS接口加密攻防实操
  • 观察者模式说明(C语言版本)
  • 达梦:dmserver占用io高排查
  • Kubernetes知识点总结(十)
  • C#之上位机开发---------C#通信库及WPF的简单实践
  • 初学PADS使用技巧笔记(也许会继续更新)
  • IDEA单元测试插件 SquareTest 延长试用期权限
  • 【python】向Jira推送自动化用例执行成功
  • 2025年-G4--lc75--Best Time to Buy and Sell Stock(java版)
  • 人工智能神经网络
  • Ollama+DeepSeek+Open-WebUi
  • 新冠阳性率升高,专家:新冠变异株致病力没太多变化
  • 美国前总统拜登确诊前列腺癌
  • 多图|多款先进预警机亮相雷达展,专家:中国预警机已达世界先进水平
  • 玛丽亚·凯莉虹口连唱两夜,舞台绽放唤醒三代人青春记忆
  • 《风林火山》千呼万唤始出来,戛纳首映后口碑崩盘?
  • 政企共同发力:多地密集部署外贸企业抢抓90天政策窗口期