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

1. 文件名修改

在一个文件目录下,存在相同扩展名 ".stp"的多个文件,对这样的文件名,请修改文件名称,在文件 名称后增加排序标识 "-01" , "-02" , "-03"...

cpp 复制代码
#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 的连续节点组成的序列, 直到不存在这样的序列为止,需 考虑各种特殊情况。

cpp 复制代码
#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 源码

相关推荐
风清扬_jd4 分钟前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
liu_chunhai12 分钟前
设计模式(3)builder
java·开发语言·设计模式
姜学迁20 分钟前
Rust-枚举
开发语言·后端·rust
冷白白22 分钟前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
凌云行者26 分钟前
rust的迭代器方法——collect
开发语言·rust
It'sMyGo29 分钟前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式
睡觉然后上课40 分钟前
c基础面试题
c语言·开发语言·c++·面试
qing_0406031 小时前
C++——继承
开发语言·c++·继承
武昌库里写JAVA1 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g1 小时前
GESP C++四级样题卷
java·c++·算法