如何在 for(或 while)循环中用字符填充字符串,C++
How to fill a string with chars in a for (or while) loop, c++
所以,我一直在编写一个程序,将两个罗马数字相加并以罗马数字输出总和。我 运行 的问题过去是,现在也是,检查结果的东西只读取第一个输出字母,而不是整个输出。
这让我想到了如何用字符(或长度为 1 的字符串)填充字符串。我做了类似的事情:
#include <iostream>
int main() {
std::string str{" "};
for (int i = 0; i < 5; ++i) {
str[i] = 'A';
}
std::cout << str;
}
在 运行 之后,你得到输出 "A",而我希望它是 "AAAAA"。我真的不知道如何制作一个动态的字符列表,如果说技术的话。
对于上下文,这里是罗马数字的代码。我尝试在 Python 中执行此操作,但效果也不佳,并且由于我最初是从 C++ 开始的,所以我想知道如何在那里执行此类操作。
#include <iostream>
using namespace std;
// simple shit
// a func str gets a sum, then with while loops 'fills' the " " in the string s with some letters
string str(int sum) {
string s = " ";
int i = 0;
while (sum >= 1000) {
s[i] = 'M';
sum -= 1000;
++i;
}
while (sum >= 500) {
s[i] = 'D';
sum -= 500;
++i;
}
while (sum >= 100) {
s[i] = 'C';
sum -= 100;
++i;
}
while (sum >= 50) {
s[i] = 'L';
sum -= 50;
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
while (sum >= 10) {
s[i] = 'X';
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
if (sum == 9) {
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 9;
}
while (sum >= 5) {
s[i] = 'V';
++i;
}
if (sum == 4) {
s[i] = 'I';
++i;
s[i] = 'V';
++i;
sum -= 4;
}
while (sum >= 1) {
s[i] = 'I';
++i;
}
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ' ') {
s[i] = '[=11=]';
}
}
//..and returns the string, full of " ", when I don't need those
return s;
}
//uh, that's the func to check if IX == 19, not 21. don't care about this one
int a(string prev) {
if (prev == "M") {
return 1000;
}
if (prev == "D") {
return 500;
}
if (prev == "C") {
return 100;
}
if (prev == "L") {
return 50;
}
if (prev == "X") {
return 10;
}
if (prev == "V") {
return 5;
}
if (prev == "I") {
return 1;
}
else {
return 0;
}
}
int main() {
//all the strings and other cool stuff
string s, letter, prev;
cin >> s;
int sum;
int prev_check;
int l = s.length();
// a loop to find a letter in a string we just input, and 'parse' it to the normal-digits (arabic) form
for (int i = 0; i < l; i++) {
letter = s[i];
if (letter == "M") {
sum += 1000;
//checking if nothing less than current Rome digit is behind it
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 1000) {
sum -= prev_check * 2;
//you may be wondering, why times two?
//because I've already added the letter behind to my sum,
//so I have to substract it from sum twice
}
}
}
// fun fact - there cant exist numbers such as IM (999) in Rome letter, only C, X and V are "substractable"
//yet, I've still added this kinda "substraction" to all letters, just because I'm lazy to input specific ones for debugging my "substraction" func
if (letter == "D") {
sum += 500;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 500) {
sum -= prev_check * 2;
}
}
}
if (letter == "C") {
sum += 100;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 100) {
sum -= prev_check * 2;
}
}
}
if (letter == "L") {
sum += 50;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 50) {
sum -= prev_check * 2;
}
}
}
if (letter == "X") {
sum += 10;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 10) {
sum -= prev_check * 2;
}
}
}
if (letter == "V") {
sum += 5;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 5) {
sum -= prev_check * 2;
}
}
}
if (letter == "I") {
sum += 1;
}
}
//and... out
cout << str(sum);
}
这是相当多的代码,我知道,我添加了一些注释来解释我在这里和那里做了什么。
下面的代码可能有助于输出 5 'A'
#include <iostream>
#include <vector>
int main() {
std::vector<char> val;
for (int i = 0; i < 5; ++i) {
val.push_back('A');
}
for (int a = 0; a < val.size(); ++a)
{
std::cout << val[a];
}
}
所以,我一直在编写一个程序,将两个罗马数字相加并以罗马数字输出总和。我 运行 的问题过去是,现在也是,检查结果的东西只读取第一个输出字母,而不是整个输出。
这让我想到了如何用字符(或长度为 1 的字符串)填充字符串。我做了类似的事情:
#include <iostream>
int main() {
std::string str{" "};
for (int i = 0; i < 5; ++i) {
str[i] = 'A';
}
std::cout << str;
}
在 运行 之后,你得到输出 "A",而我希望它是 "AAAAA"。我真的不知道如何制作一个动态的字符列表,如果说技术的话。
对于上下文,这里是罗马数字的代码。我尝试在 Python 中执行此操作,但效果也不佳,并且由于我最初是从 C++ 开始的,所以我想知道如何在那里执行此类操作。
#include <iostream>
using namespace std;
// simple shit
// a func str gets a sum, then with while loops 'fills' the " " in the string s with some letters
string str(int sum) {
string s = " ";
int i = 0;
while (sum >= 1000) {
s[i] = 'M';
sum -= 1000;
++i;
}
while (sum >= 500) {
s[i] = 'D';
sum -= 500;
++i;
}
while (sum >= 100) {
s[i] = 'C';
sum -= 100;
++i;
}
while (sum >= 50) {
s[i] = 'L';
sum -= 50;
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
while (sum >= 10) {
s[i] = 'X';
++i;
}
if (sum == 19) {
s[i] = 'X';
++i;
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 19;
}
if (sum == 9) {
s[i] = 'I';
++i;
s[i] = 'X';
++i;
sum -= 9;
}
while (sum >= 5) {
s[i] = 'V';
++i;
}
if (sum == 4) {
s[i] = 'I';
++i;
s[i] = 'V';
++i;
sum -= 4;
}
while (sum >= 1) {
s[i] = 'I';
++i;
}
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ' ') {
s[i] = '[=11=]';
}
}
//..and returns the string, full of " ", when I don't need those
return s;
}
//uh, that's the func to check if IX == 19, not 21. don't care about this one
int a(string prev) {
if (prev == "M") {
return 1000;
}
if (prev == "D") {
return 500;
}
if (prev == "C") {
return 100;
}
if (prev == "L") {
return 50;
}
if (prev == "X") {
return 10;
}
if (prev == "V") {
return 5;
}
if (prev == "I") {
return 1;
}
else {
return 0;
}
}
int main() {
//all the strings and other cool stuff
string s, letter, prev;
cin >> s;
int sum;
int prev_check;
int l = s.length();
// a loop to find a letter in a string we just input, and 'parse' it to the normal-digits (arabic) form
for (int i = 0; i < l; i++) {
letter = s[i];
if (letter == "M") {
sum += 1000;
//checking if nothing less than current Rome digit is behind it
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 1000) {
sum -= prev_check * 2;
//you may be wondering, why times two?
//because I've already added the letter behind to my sum,
//so I have to substract it from sum twice
}
}
}
// fun fact - there cant exist numbers such as IM (999) in Rome letter, only C, X and V are "substractable"
//yet, I've still added this kinda "substraction" to all letters, just because I'm lazy to input specific ones for debugging my "substraction" func
if (letter == "D") {
sum += 500;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 500) {
sum -= prev_check * 2;
}
}
}
if (letter == "C") {
sum += 100;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 100) {
sum -= prev_check * 2;
}
}
}
if (letter == "L") {
sum += 50;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 50) {
sum -= prev_check * 2;
}
}
}
if (letter == "X") {
sum += 10;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 10) {
sum -= prev_check * 2;
}
}
}
if (letter == "V") {
sum += 5;
if (i > 0) {
prev = s[i - 1];
prev_check = a(prev);
if (prev_check < 5) {
sum -= prev_check * 2;
}
}
}
if (letter == "I") {
sum += 1;
}
}
//and... out
cout << str(sum);
}
这是相当多的代码,我知道,我添加了一些注释来解释我在这里和那里做了什么。
下面的代码可能有助于输出 5 'A'
#include <iostream>
#include <vector>
int main() {
std::vector<char> val;
for (int i = 0; i < 5; ++i) {
val.push_back('A');
}
for (int a = 0; a < val.size(); ++a)
{
std::cout << val[a];
}
}