【问题标题】:How to fill a string with chars in a for (or while) loop, c++ [duplicate]如何在for(或while)循环中用字符填充字符串,c ++ [重复]
【发布时间】:2020-03-12 10:38:48
【问题描述】:

所以,我一直在编写一个程序,将两个罗马数字相加并输出罗马数字的总和。我遇到的问题是,检查结果的东西只是读取第一个输出字母,而不是整个输出。

这让我想到了如何用字符(或长度为 1 的字符串)填充字符串。我会这样做:

#include <iostream>

int main() {
    std::string str{" "};

    for (int i = 0; i < 5; ++i) {
        str[i] = 'A';
    }

    std::cout << str;
}

运行此程序后,当我希望它为“AAAAA”时,您会得到输出“A”。如果说技术性的话,我真的不知道如何制作一个动态字符列表。


关于上下文,这里是罗马数字的代码。我尝试在 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] = '\0';
        }
    }
    //..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);
}

这是相当多的代码,我知道,我添加了一些 cmets 来解释我在这里和那里做什么。

【问题讨论】:

  • 你不能只通过索引访问字符串的任何部分,除非那里有东西。一个空字符串什么都没有,一个带空格的字符串只有一个元素。访问未定义行为之外的任何内容。例如,您可以使用s += 'I';
  • 您在写的内容中隐藏了一个很好的问题。我将其移至您问题的顶部,以便更容易找到。实际上,您可以删除水平线之后的所有内容,因为您的实际问题不需要该上下文。我会把它留给你。 (不过,您确实有一个重点问题要强调,这很好。很多人没有付出那么多努力。)

标签: c++ function dev-c++


【解决方案1】:

以下代码可能有助于输出 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];
    }
}

【讨论】:

  • std::cout &lt;&lt; std::string s(5, 'A'); 更加简洁易读。
猜你喜欢
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
  • 2022-12-10
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多