如何实现C++字符串每次显示时#N数值从18递减至1循环替换?
实现字符串中数值每次显示自动递减循环(18→1→18)
我正在寻找一种方法,实现字符串每次显示时其中的数值自动递减。现有C++代码仅能将文本文件第一行中的
#N固定替换为18,需求为:#N需被替换为从18递减到1的数值,当数值到0时重新回到18循环。请帮忙修改代码实现该功能。
原代码如下:
#include <algorithm> #include <cstdlib> #include <fstream> #include <iostream> #include <string> using namespace std; void find_and_replace(string & source, string const & find, string const & replace) { for (string::size_type i = 0; (i = source.find(find, i)) != string::npos;) { source.replace(i, find.length(), replace); i += replace.length(); } } int main(int argc, char * argv[]) { std::ifstream fileIn("Answers.txt", std::ios::in | std::ios::binary); string question; string line; if (!fileIn) { cout << "Cannot open input file!" << endl; return 1; } while (getline(fileIn, line)) { if (line == "The answer can be found in a secret place in the woods.") { fileIn.clear(); fileIn.seekg(0, ios::beg); } cout << "Ask a question followed by the Enter key. Or type 'exit' to Exit program.\n"; getline(cin, question); system("CLS"); find_and_replace(line, "#N", "18"); if (question == "") { cout << "Your input cannot be blank. Please try again.\n\n"; } else if (question == "exit") exit(0); else { cout << "Q: " + question << "\nA: " + line + "\n\n"; } } }
解决方案
要实现这个需求,核心是添加一个能持续跟踪数值的计数器,每次显示后自动递减,到0时重置回18。同时要动态生成对应数值的字符串来替换#N,而不是固定用"18"。
修改后的代码如下:
#include <algorithm> #include <cstdlib> #include <fstream> #include <iostream> #include <string> #include <sstream> // 用于整数转字符串,兼容旧C++标准 using namespace std; // 全局计数器,初始值设为18,确保程序运行期间保留数值状态 int counter = 18; void find_and_replace(string & source, string const & find, string const & replace) { for (string::size_type i = 0; (i = source.find(find, i)) != string::npos;) { source.replace(i, find.length(), replace); i += replace.length(); } } // 工具函数:将整数转为字符串 string int_to_string(int num) { stringstream ss; ss << num; return ss.str(); } int main(int argc, char * argv[]) { std::ifstream fileIn("Answers.txt", std::ios::in | std::ios::binary); string question; string line; if (!fileIn) { cout << "Cannot open input file!" << endl; return 1; } while (getline(fileIn, line)) { if (line == "The answer can be found in a secret place in the woods.") { fileIn.clear(); fileIn.seekg(0, ios::beg); } cout << "Ask a question followed by the Enter key. Or type 'exit' to Exit program.\n"; getline(cin, question); system("CLS"); // 用当前计数器值替换#N string current_num = int_to_string(counter); find_and_replace(line, "#N", current_num); // 更新计数器:递减到0时重置为18 counter--; if (counter == 0) { counter = 18; } if (question == "") { cout << "Your input cannot be blank. Please try again.\n\n"; } else if (question == "exit") exit(0); else { cout << "Q: " + question << "\nA: " + line + "\n\n"; } } }
关键改动说明
- 全局计数器:用全局变量
counter来跟踪当前数值,避免局部变量每次循环重置的问题,确保数值能持续递减。 - 整数转字符串:新增
int_to_string函数(如果用C++11及以上版本,也可以直接用标准库的std::to_string()),把计数器的整数值转换成字符串,才能用于替换操作。 - 动态替换与循环逻辑:每次用户提问后,先用当前计数器值替换
#N,再更新计数器;当计数器降到0时,立刻重置为18,实现循环递减的效果。 - 兼容性:用
stringstream做类型转换是为了兼容旧版C标准,如果你的环境支持C11及以上,可以直接替换成std::to_string(counter)简化代码。
内容的提问来源于stack exchange,提问作者DogFoxX
相关产品推荐
相关产品推荐

