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

socket编程-UDP(2)-设计翻译系统

socket编程-UDP(2)-设计翻译系统

实现一个简单的英译汉的网络字典

dictionary.txt

apple: 苹果
banana: 香蕉
cat: 猫
dog: 狗
book: 书
pen: 笔
happy: 快乐的
sad: 悲伤的
hello: 
: 你好run: 跑
jump: 跳
teacher: 老师
student: 学生
car: 汽车
bus: 公交车
love: 爱
hate: 恨
hello: 你好
goodbye: 再见
summer: 夏天
winter: 冬天

Dict.hpp

#pragma once#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "Log.hpp"
#include "InetAddr.hpp"const std::string defaultdict = "./dictionary.txt";
const std::string sep = ": ";using namespace LogModule;class Dict
{
public:Dict(const std::string &path = defaultdict) : _dict_path(path){}bool LoadDict(){std::ifstream in(_dict_path);if (!in.is_open()){LOG(LogLevel::DEBUG) << "打开字典: " << _dict_path << " 错误";return false;}std::string line;while (std::getline(in, line)){// "apple: 苹果"auto pos = line.find(sep);if (pos == std::string::npos){LOG(LogLevel::WARNING) << "解析: " << line << " 失败";continue;}std::string english = line.substr(0, pos);std::string chinese = line.substr(pos + sep.size());if (english.empty() || chinese.empty()){LOG(LogLevel::WARNING) << "没有有效内容: " << line;continue;}_dict.insert(std::make_pair(english, chinese));LOG(LogLevel::DEBUG) << "加载: " << line;}in.close();return true;}std::string Translate(const std::string &word, InetAddr &client){auto iter = _dict.find(word);if (iter == _dict.end()){LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->None";return "None";}LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->" << iter->second;return iter->second;}~Dict(){}private:std::string _dict_path; // 路径+文件名std::unordered_map<std::string, std::string> _dict;
};

InetAddr.hpp

#pragma once
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// 网络地址和主机地址之间进行转换的类class InetAddr
{
public:InetAddr(struct sockaddr_in &addr) : _addr(addr){_port = ntohs(_addr.sin_port);           // 从网络中拿到的!网络序列_ip = inet_ntoa(_addr.sin_addr); // 4字节网络风格的IP -> 点分十进制的字符串风格的IP}uint16_t Port() {return _port;}std::string Ip() {return _ip;}~InetAddr(){}private:struct sockaddr_in _addr;std::string _ip;uint16_t _port;
};
http://www.dtcms.com/a/310821.html

相关文章:

  • 基于线性规划的储能充放电仿真系统
  • 读取数据集及数据集划分
  • 7.苹果ios逆向-目录结构
  • 【vue】Vue 项目创建工具对比:vue create 与 create-vue 的核心区别
  • 安卓开发--LinearLayout(线性布局)
  • 华锐矩阵世界平台与海外客户洽谈合作
  • 将 YOLOv11 的 .pt 模型转换为 YOLOv8 格式需要特定的处理流程 机器学习 计算机视觉cv
  • Spotlight on MySQL 300安装教程(附使用指南):实时监控MySQL性能的工具
  • 好未来披露2026财年Q1财报:净利润3128万美元,同比大增174%
  • 解决IDEA中MAVEN项目总是将LANGUAGE LEVEL重置的问题
  • InteriorGS 数据集
  • 力扣-字母异位词
  • gpu 优化
  • 从删库到跑路?MyBatis3逻辑删除实战:优雅规避数据灾难
  • 一致连续性背后的直觉是什么?
  • 高速信号设计之 DDR5 篇
  • 【unity实战】简易的车辆控制系统
  • 从零开始:Kaggle 竞赛实战入门指南
  • 鸿蒙系统PC安装指南
  • 【RH124 问答题】第 9 章 控制服务和守护进程
  • 测试分类:详解各类测试方式与方法
  • 告别“AI味”图像!最新开源AI模型FLUX.1-Krea实现真实光影生成
  • 【n8n】如何跟着AI学习n8n【05】:Merge节点和子流程调用
  • Prim算法
  • 交叉编译简介
  • 【JAVA面试】基础篇
  • 广东省省考备考(第六十三天8.1)——资料分析、数量(强化训练)
  • 【AI应用】 能源保供战:AI大模型如何守护万家灯火?
  • Day37| 完全背包、518.零钱兑换II、377. 组合总和 Ⅳ、70. 爬楼梯 (进阶)
  • 流式编程学习思路