SLB二面
Q1
有一个文件有如下内容
using System.Reflection; #if DEBUG [assembly: AssemblyConfiguration("Debug")] #else [assembly: AssemblyConfiguration("Release")] #endif [assembly: AssemblyVersion("1.1")] [assembly: AssemblyFileVersion("1.4.10.0")] [assembly: AssemblyInformationalVersion("1.0.5.0")]
希望写一个C++函数,每次调用将该文件的AssemblyFileVersion和AssemblyInformationalVersion的倒数第二位加一。
void IncrementVersion(const std::string& filepath) { std::ifstream inFile(filepath); if (!inFile.is_open()) { std::cerr << "Failed to open the file." << std::endl; return; } std::ostringstream contents; std::string line; while (std::getline(inFile, line)) { if (line.find("AssemblyFileVersion") != std::string::npos || line.find("AssemblyInformationalVersion") != std::string::npos) { size_t lastDot = line.find_last_of('.'); size_t secondLastDot = line.find_last_of('.', lastDot - 1); int number = std::stoi(line.substr(secondLastDot + 1, lastDot - secondLastDot - 1)); number += 1; line = line.substr(0, secondLastDot + 1) + std::to_string(number) + line.substr(lastDot); } contents << line << "\n"; } inFile.close(); std::ofstream outFile(filepath, std::ios::trunc); if (!outFile.is_open()) { std::cerr << "Failed to open the file for writing." << std::endl; return; } outFile << contents.str(); outFile.close(); }
或者用正则:
void IncrementVersionWithRegex(const std::string& filepath) { std::ifstream inFile(filepath); if (!inFile.is_open()) { std::cerr << "Failed to open the file." << std::endl; return; } std::ostringstream contents; std::string line; // Modified pattern to target the second to last number section of the version std::regex versionPattern( R"delimiter((AssemblyFileVersion|AssemblyInformationalVersion)\("([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)"\))delimiter" ); while (std::getline(inFile, line)) { std::smatch match; if (std::regex_search(line, match, versionPattern)) { int number = std::stoi(match[3].str()); number += 1; // Construct the new version string std::string newVersion = match[1].str() + "(\"" + match[2].str() + "." + match[3].str() + "." + std::to_string(number) + "." + match[4].str() + "\")"; line.replace(match.position(), match.length(), newVersion); } contents << line << "\n"; } inFile.close(); std::ofstream outFile(filepath, std::ios::trunc); if (!outFile.is_open()) { std::cerr << "Failed to open the file for writing." << std::endl; return; } outFile << contents.str(); outFile.close(); } int main() { IncrementVersionWithRegex("your_file_path_here.cs"); return 0; }
Q2
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { int carryOn = 0; public: ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) { if (l1 == nullptr && l2 == nullptr && carryOn == 0) { return nullptr; } int n1 = l1 == nullptr ? 0 : l1->val; int n2 = l2 == nullptr ? 0 : l2->val; int val = n1 + n2 + carryOn; carryOn = val / 10; val = val % 10; ListNode *cur = new ListNode(); cur->val = val; cur->next = addTwoNumbers( l1 == nullptr? nullptr : l1->next, l2 == nullptr? nullptr : l2->next ); return cur; } };