если вкратце, то такая задачка: не понимаю, почему в итоге результат получается 473, когда верный ответ 445 #include <iostream> #include <fstream> #include <string> #include <cstdlib> bool isInteger(char symbol) { if (symbol >= '0' && symbol <= '9') return true; else return false; } int symbolToInteger(char symbol) { if (symbol >= '0' && symbol <= '9') return static_cast<int>(symbol) - '0'; else return static_cast<int>(symbol); } int main() { const std::string inputFilename = "paroli.txt"; const std::string outputFilename = "output.txt"; std::ifstream inputFile(inputFilename); if (inputFile.is_open()) { int passwordsCount = 0; std::string input; while (std::getline(inputFile, input)) { int repeatMin = -1; int repeatMax = -1; char checkSymbol; std::string password; bool flagRepeatMin = false; bool flagRepeatMax = false; int symbolPosition; for (int i = 0; i < input.length(); i++) { if (repeatMin < 0 && isInteger(input[i])) { flagRepeatMin = true; symbolPosition = i; } else if (flagRepeatMin && !isInteger(input[i])) { std::string temp = input.substr(symbolPosition, i - 1); repeatMin = std::atoi(temp.c_str()); flagRepeatMin = false; } else if (repeatMax < 0 && isInteger(input[i]) && !flagRepeatMin) { flagRepeatMax = true; symbolPosition = i; } else if (flagRepeatMax && !isInteger(input[i])) { std::string temp = input.substr(symbolPosition, input.length() - i); repeatMax = std::atoi(temp.c_str()); flagRepeatMax = false; } else if (input[i] == ':') { checkSymbol = input[i - 1]; symbolPosition = i + 1; break; } } password = input.substr(symbolPosition, input.length() - symbolPosition + 1); int count = 0; for (int i = 0; i < password.length(); i++) { if (password[i] == checkSymbol) { count++; } } if (count >= repeatMin && count <= repeatMax) { passwordsCount++; } } inputFile.close(); std::ofstream outputFile(outputFilename); if (outputFile.is_open()) { outputFile << "Passwords: " << passwordsCount << std::endl; } else { std::cout << "Could not open the output file" << std::endl; } } else { std::cout << "Could not open the input file" << std::endl; } return 0; } C #include <iostream> #include <fstream> #include <string> #include <cstdlib> bool isInteger(char symbol) { if (symbol >= '0' && symbol <= '9') return true; else return false; } int symbolToInteger(char symbol) { if (symbol >= '0' && symbol <= '9') return static_cast<int>(symbol) - '0'; else return static_cast<int>(symbol); } int main() { const std::string inputFilename = "paroli.txt"; const std::string outputFilename = "output.txt"; std::ifstream inputFile(inputFilename); if (inputFile.is_open()) { int passwordsCount = 0; std::string input; while (std::getline(inputFile, input)) { int repeatMin = -1; int repeatMax = -1; char checkSymbol; std::string password; bool flagRepeatMin = false; bool flagRepeatMax = false; int symbolPosition; for (int i = 0; i < input.length(); i++) { if (repeatMin < 0 && isInteger(input[i])) { flagRepeatMin = true; symbolPosition = i; } else if (flagRepeatMin && !isInteger(input[i])) { std::string temp = input.substr(symbolPosition, i - 1); repeatMin = std::atoi(temp.c_str()); flagRepeatMin = false; } else if (repeatMax < 0 && isInteger(input[i]) && !flagRepeatMin) { flagRepeatMax = true; symbolPosition = i; } else if (flagRepeatMax && !isInteger(input[i])) { std::string temp = input.substr(symbolPosition, input.length() - i); repeatMax = std::atoi(temp.c_str()); flagRepeatMax = false; } else if (input[i] == ':') { checkSymbol = input[i - 1]; symbolPosition = i + 1; break; } } password = input.substr(symbolPosition, input.length() - symbolPosition + 1); int count = 0; for (int i = 0; i < password.length(); i++) { if (password[i] == checkSymbol) { count++; } } if (count >= repeatMin && count <= repeatMax) { passwordsCount++; } } inputFile.close(); std::ofstream outputFile(outputFilename); if (outputFile.is_open()) { outputFile << "Passwords: " << passwordsCount << std::endl; } else { std::cout << "Could not open the output file" << std::endl; } } else { std::cout << "Could not open the input file" << std::endl; } return 0; }
Результат неправильный потому что ты неправильно парсишь входные значения в строчках - весь этот цикл с условиями в 46 строке очень тяжело читать и в нём точно есть ошибки потому что если ты оставишь во входном файле только один пароль например ("3-8 a: abcdefg") и запустишь в дебаге то увидишь что repeatMin равно 0, ходя должно быть равно 3. Обычно для парсинга заранее известных форматов строк используют регулярные выражения: #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <regex> #include <assert.h> int main() { const std::string inputFilename = "paroli.txt"; const std::string outputFilename = "output.txt"; std::ifstream inputFile(inputFilename); if (inputFile.is_open()) { int passwordsCount = 0; const std::regex data_regex("(\\d+)-(\\d+) ([a-z]): (.+)"); std::smatch data_match; std::string input; while (std::getline(inputFile, input)) { assert (std::regex_match(input, data_match, data_regex)); int repeatMin = std::stoi(data_match[1].str()); int repeatMax = std::stoi(data_match[2].str()); char checkSymbol = data_match[3].str()[0]; std::string password = data_match[4].str(); int count = 0; for (int i = 0; i < password.length(); i++) { if (password[i] == checkSymbol) { count++; } } if (count >= repeatMin && count <= repeatMax) { passwordsCount++; } } inputFile.close(); std::ofstream outputFile(outputFilename); if (outputFile.is_open()) { outputFile << "Passwords: " << passwordsCount << std::endl; } else { std::cout << "Could not open the output file" << std::endl; } } else { std::cout << "Could not open the input file" << std::endl; } return 0; } C #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <regex> #include <assert.h> int main() { const std::string inputFilename = "paroli.txt"; const std::string outputFilename = "output.txt"; std::ifstream inputFile(inputFilename); if (inputFile.is_open()) { int passwordsCount = 0; const std::regex data_regex("(\\d+)-(\\d+) ([a-z]): (.+)"); std::smatch data_match; std::string input; while (std::getline(inputFile, input)) { assert (std::regex_match(input, data_match, data_regex)); int repeatMin = std::stoi(data_match[1].str()); int repeatMax = std::stoi(data_match[2].str()); char checkSymbol = data_match[3].str()[0]; std::string password = data_match[4].str(); int count = 0; for (int i = 0; i < password.length(); i++) { if (password[i] == checkSymbol) { count++; } } if (count >= repeatMin && count <= repeatMax) { passwordsCount++; } } inputFile.close(); std::ofstream outputFile(outputFilename); if (outputFile.is_open()) { outputFile << "Passwords: " << passwordsCount << std::endl; } else { std::cout << "Could not open the output file" << std::endl; } } else { std::cout << "Could not open the input file" << std::endl; } return 0; }