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

C++修改文件后缀名;链表循环删除乘积为10的元素

1. 文件名修改

在一个文件目录下,存在相同扩展名 ".stp"的多个文件,对这样的文件名,请修改文件名称,在文件 名称后增加排序标识 "-01" "-02" "-03"...
#include <iostream>
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;// Input: 目录名称
// Output:文件列表清单class Findupdate {
public:void replaceFileNameList();//遍历文件列表替换文件名bool checkFile(string path);//查找路径下所有文件,不存在子文件夹bool checkFile_circ(string path, vector<string>& m_vec_file_path_name);//查找路径下所有文件,存在子文件夹~Findupdate(){m_vec_file_path_name.clear();}vector<string> m_vec_file_path_name; //文件名路径列表
private:bool replaceFileName(string oldFileName, string newFileName);//替换文件名 string m_modif_before = ".stp";             //指定修改前的字段
};bool Findupdate::replaceFileName(string oldFileName, string newFileName)
{fstream fs;fs.open(oldFileName.c_str());if (fs.fail()){cout << "文件打开失败!" << endl;fs.close();return false;}else{fs.close();if (rename(oldFileName.c_str(), newFileName.c_str()) == -1)   //文件重命名{cout << "文件名修改失败!" << endl;return false;}return true;}return true;
}void Findupdate::replaceFileNameList()
{int num = 1;string ModifyAfterFilds = "";                  //指定修改后的字段for (auto name : m_vec_file_path_name){//临时文件名用于字符替换string newFileName = name;int findIndex = name.find(m_modif_before, 1);if (findIndex != -1 && findIndex == (name.size()-4)){ModifyAfterFilds = ".stp-0" + std::to_string(num); num++;newFileName = newFileName.replace(findIndex, m_modif_before.size(), ModifyAfterFilds);cout << name << endl;cout << newFileName << endl;}else{continue;}        int ret = replaceFileName(name, newFileName);}
}bool Findupdate::checkFile(string path)
{string EachPath = path;      //临时路径,用于字符拼接intptr_t FileHandle;             //文件句柄struct _finddata_t FileInfo;     //文件信息if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1){cout << "未找到文件! " << endl;return false;}else{while (_findnext(FileHandle, &FileInfo) == 0){if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0){m_vec_file_path_name.push_back(FileInfo.name);cout << "FileName : " << FileInfo.name << endl;}}_findclose(FileHandle);}return true;
}bool Findupdate::checkFile_circ(string path, vector<string>& m_vec_file_path_name)
{string EachPath = path;      //临时路径,用于字符拼接intptr_t FileHandle;             //文件句柄struct _finddata_t FileInfo;     //文件信息//使用_findfirst查找文件,获取文件信息if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1){cout << "failed find file! " << endl;return false;}else{do{   // 比较文件类型是否是文件夹if ((FileInfo.attrib &  _A_SUBDIR)){if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0){EachPath = path;   //每次从根路径拼接//递归调用checkFile_circ(EachPath.append("\\").append(FileInfo.name), m_vec_file_path_name);}}else{EachPath = path;m_vec_file_path_name.push_back(EachPath.append("\\").append(FileInfo.name));cout << "PATH/FileName : " << EachPath << endl;}} while (_findnext(FileHandle, &FileInfo) == 0);_findclose(FileHandle);}return true;
}int main()
{string Path = "E:\\xx\\xx";      //自定义路径Findupdate f_upate;//  f_upate.checkFile(Path); //路径下没有子文件夹f_upate.checkFile_circ(Path, f_upate.m_vec_file_path_name);f_upate.replaceFileNameList();system("pause");return 0;
}


 

2.链表删除

链表 head 反复 删去链表中由 总乘积 值为 10 的连续节点组成的序列, 直到不存在这样的序列为止,需 考虑各种特殊情况。
#include<iostream>
using namespace std;struct ListNode {  int val;  ListNode *next;  ListNode(int x) : val(x), next(NULL) {}  
};  class Solution
{
public:// 辅助函数:打印链表  void printList(ListNode* head) {  while (head) {  std::cout << head->val << " ";  head = head->next;  }  std::cout << std::endl;  }  // 辅助函数:创建链表  ListNode* createList(int arr[], int n) {  if (n == 0) return nullptr;  ListNode* head = new ListNode(arr[0]);  ListNode* curr = head;  for (int i = 1; i < n; ++i) {  curr->next = new ListNode(arr[i]);  curr = curr->next;  }  return head;  }  // 辅助函数:释放链表内存  void deleteList(ListNode* head) {  while (head) {  ListNode* temp = head;  head = head->next;  delete temp;  }  }  ListNode* removeSublists(ListNode* head){// 创建一个哑节点作为头节点的前驱  ListNode dummy(0);  dummy.next = head;  ListNode* prev = &dummy;  ListNode* curr = head;  while (curr && curr->next) {  // 检查当前节点和下一个节点的乘积是否为10  while (curr && curr->next && curr->val * curr->next->val == 10) {  // 删除这两个节点  prev->next = curr->next->next;  // 移动到下一个要检查的节点(注意这里不是curr = prev->next,因为prev->next可能已经被改变了)  curr = curr->next;}  curr = prev->next;  // 如果没有找到乘积为10的节点对,则移动到下一个要检查的节点对  if (curr) {  prev = curr;  curr = curr->next;  }  }  // 返回哑节点的下一个节点作为新的头节点  return dummy.next; }
};int main() {  int arr[] = {1, 2, 5, 2, 1, 2, 5}; // 示例链表:1 -> 2 -> 5 -> 2 -> 1 -> 2 -> 5  Solution sol_list;ListNode* head = sol_list.createList(arr, sizeof(arr) / sizeof(arr[0]));std::cout << "Original List: ";  sol_list.printList(head);head = sol_list.removeSublists(head);  std::cout << "List after removing product-ten nodes: ";  sol_list.printList(head);  sol_list.deleteList(head); // 释放链表内存  return 0;  
}

3. VS Code 配置 gitee

在VScode上配置Git - 知乎

Gitee 源码

相关文章:

  • 手摸手教你uniapp原生插件开发
  • 【ai】livekit:Agents 3 : pythonsdk和livekit-agent的可编辑模式下的安装
  • 朋友圈定时发送设置
  • 华发股份:加强业务协同 新政下项目热销
  • Go语言 gRPC 简述
  • 2018 年山东省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
  • 如何从异步调用中返回响应
  • new CCDIKSolver( OOI.kira, iks ); // 创建逆运动学求解器
  • internvl-chat部署
  • 效果炸裂!使用 GPT-4o 快速实现LLM OS
  • Linux源码编译安装MySQL + Qt连接MySQL
  • 告别虚拟机,在Windows10启动Linux子系统
  • C#面:DataReader与Dataset有什么区别
  • AI Agent智能体概述及原理
  • Android Dialog软键盘弹出问题完美解决办法
  • ES6指北【2】—— 箭头函数
  • [nginx文档翻译系列] 控制nginx
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • Android优雅地处理按钮重复点击
  • CentOS7简单部署NFS
  • classpath对获取配置文件的影响
  • express + mock 让前后台并行开发
  • java8-模拟hadoop
  • js继承的实现方法
  • Linux链接文件
  • MySQL的数据类型
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • React中的“虫洞”——Context
  • Redis的resp协议
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • swift基础之_对象 实例方法 对象方法。
  • Vue--数据传输
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 分类模型——Logistics Regression
  • 关于字符编码你应该知道的事情
  • 理清楚Vue的结构
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 微服务框架lagom
  • 项目管理碎碎念系列之一:干系人管理
  • 学习笔记TF060:图像语音结合,看图说话
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 译自由幺半群
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 在weex里面使用chart图表
  • 终端用户监控:真实用户监控还是模拟监控?
  • 如何正确理解,内页权重高于首页?
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​VRRP 虚拟路由冗余协议(华为)
  • ​用户画像从0到100的构建思路
  • #QT(一种朴素的计算器实现方法)
  • #职场发展#其他
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (28)oracle数据迁移(容器)-部署包资源