Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <regex>
- #include <unordered_set>
- void analyzeVariables(const std::string& inputFileName, const std::string& outputFileName) {
- std::ifstream inputFile(inputFileName);
- std::ofstream outputFile(outputFileName);
- if (!inputFile.is_open()) {
- std::cerr << "Ошибка открытия файла: " << inputFileName << std::endl;
- return;
- }
- std::string inputData((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
- // Регулярное выражение для анализа описания переменных (пример для языка C++)
- std::regex variablePattern(R"(\b(?:int|double|long|short|char|float)\s+([a-zA-Z_]\w*)\s*(?:=\s*(?:\"[^\"]*\"|-?\d+(?:\.\d+)?)\s*)?;)");
- std::smatch match;
- std::regex_iterator<std::string::iterator> it(inputData.begin(), inputData.end(), variablePattern);
- std::regex_iterator<std::string::iterator> end;
- // Множество для отслежшвания дублирования имен переменных
- std::unordered_set<std::string> variableNames;
- while (it != end) {
- std::string variableName = (*it)[1].str();
- // Проверка на дублирование имен переменных
- if (variableNames.find(variableName) != variableNames.end()) {
- outputFile << "Ошибка: Дублирование имени переменной '" << variableName
- << "' в строке " << std::count(inputData.begin(), it->suffix().first, '\n') + 1
- << ", позиция " << it->position(1) + 1 << "." << std::endl;
- return;
- }
- // Запись имени переменной в множество
- variableNames.insert(variableName);
- ++it;
- }
- // Если программа дошла сюда, значит описание переменных корректное
- outputFile << "Описание переменных корректное." << std::endl;
- }
- int main() {
- analyzeVariables("input.txt", "output.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment