根据C+中生成的随机数的值生成Html RGB值+;

根据C+中生成的随机数的值生成Html RGB值+;,html,c++,Html,C++,我目前有一个程序,它可以创建一个充满随机数的表格(我使用“for”循环来实现这一点),并让这些数字根据其值改变颜色背景(例如:如果随机值为1到2500,则这些数字的颜色背景将为黄色,2500到5000将为红色)。这个表是成功的,但是现在我需要用HTML生成RGB值,而不是硬编码它们。(我不能对HTML使用任何类型的逻辑…)。我应该采取什么样的策略来解决这个问题?如果有人能够帮助(提示、示例或策略),我将非常感激:),而且,我对HTML有点新的见解,在C++上略微有些中间。 void makeGa

我目前有一个程序,它可以创建一个充满随机数的表格(我使用“for”循环来实现这一点),并让这些数字根据其值改变颜色背景(例如:如果随机值为1到2500,则这些数字的颜色背景将为黄色,2500到5000将为红色)。这个表是成功的,但是现在我需要用HTML生成RGB值,而不是硬编码它们。(我不能对HTML使用任何类型的逻辑…)。我应该采取什么样的策略来解决这个问题?如果有人能够帮助(提示、示例或策略),我将非常感激:),而且,我对HTML有点新的见解,在C++上略微有些中间。
void makeGap(int gap, std::string & text) {
    for (int i = 0; i < gap; i++)
        text.append(" ");
}

int main() 
{
    ofstream htmltable("MyTask.html", ios::out | ios::trunc);

    array<string, 10> chars = { " Monthly " , " Balance " , " Savings ", " Income ", " Tax Inc. ", " Salary ", " Federal ", " Employee ", " Number ", " Account " };
    int headgap = 3;
    int bodygap = 3;
    int tablegap = 6;
    int rowgap = 9;
    string tabletext("<html>\n");

    makeGap(headgap, tabletext);
    tabletext += "<head>\n";

    makeGap(headgap, tabletext);
    tabletext += "<style>\n";

    makeGap(headgap, tabletext);
    tabletext += "table, th, td { border: 1px solid black;}\n";

    makeGap(headgap, tabletext);
    tabletext += "#p1 {background-color:rgb(255,0,0);}\n"; //red

    makeGap(headgap, tabletext);
    tabletext += "#p2 {background-color:rgb(240,0,0);}\n"; //green

    makeGap(headgap, tabletext);
    tabletext += "#p3 {background-color:rgb(220,0,0);}\n"; //blue

    makeGap(headgap, tabletext);
    tabletext += "#p4 {background-color:rgb(198,0,0);}\n"; //yellow

    makeGap(headgap, tabletext);
    tabletext += "#p5 {background-color:rgb(170,0,0);}\n"; //black

    makeGap(headgap, tabletext);
    tabletext += "#p6 {background-color:rgb(150,0,0);}\n";

    makeGap(headgap, tabletext);
    tabletext += "#p7 {background-color:rgb(120,0,0);}\n";

    makeGap(bodygap, tabletext);
    tabletext += "<H3><BR>Random Table Color Project</H3>\n";

    makeGap(bodygap, tabletext);
    tabletext += "<TR>\n";

    makeGap(bodygap, tabletext);
    tabletext += "<TH TABLE BORDER>\n";

    makeGap(bodygap, tabletext);
    tabletext += "</TH >\n";

    makeGap(bodygap, tabletext);
    tabletext += "</TR>\n";

    makeGap(headgap, tabletext);
    tabletext += "</style>\n";

    makeGap(headgap, tabletext);
    tabletext += "</head>\n";

    makeGap(bodygap, tabletext);
    tabletext += "<body>\n";

    makeGap(tablegap, tabletext);
    tabletext += "<table width = \"1155\">\n";

    makeGap(tablegap + 1, tabletext);
    tabletext += "<thead align=\"center\">\n";

    makeGap(tablegap, tabletext);
    tabletext += "<tr><th></th>";

    for (int i = 0; i < 10; i++) { //Table Names
        tabletext += "<td>";
        tabletext += *(chars.begin() + i);
        tabletext += "</td>";
    }

    tabletext += "</tr>\n";

    makeGap(tablegap + 1, tabletext);

    tabletext += "</thead>";

    makeGap(tablegap + 1, tabletext);

    tabletext += "<tbody align=\"center\">\n";

    srand(static_cast<unsigned int>(time(0))); 

    for (int row = 0; row < 38; row++) { //Rows
        makeGap(rowgap, tabletext);
        ostringstream oss;
        int randnumber = rand() % 10000; 
        if (randnumber > 0 && randnumber <= 2500)
        {
            tabletext += "<tr id=\"p1\"><td>"; //red
        }
        else if (randnumber > 2500 && randnumber <= 5000)
        {
            tabletext += "<tr id=\"p3\"><td>"; //blue
        }
        else if (randnumber > 5000 && randnumber <= 7500)
        {
            tabletext += "<tr id=\"p4\"><td>"; //yellow
        }
        else if (randnumber > 7500 && randnumber <= 9000)
        {
            tabletext += "<tr id=\"p2\"><td>"; //green
        }
        else
        {
            tabletext += "<tr id=\"p5\"><td>"; //black
        }
        oss << row;

        for (int col = 0; col < 10; col++) { //Columns
            oss.str("");
            int randnumber = rand() % 10000; //Random Option
            if (randnumber > 0 && randnumber <= 2500)
            {
                tabletext += "<td id=\"p1\">";
            }
            else if (randnumber > 2500 && randnumber <= 5000)
            {
                tabletext += "<td id=\"p3\">";
            }
            else if (randnumber > 5000 && randnumber <= 7500)
            {
                tabletext += "<td id=\"p4\">";
            }
            else if (randnumber > 7500 && randnumber <= 9000)
            {
                tabletext += "<td id=\"p2\">";
            }
            else
            {
                tabletext += "<td id=\"p5\">";
            }
            oss << randnumber;

            tabletext.append(oss.str());
            tabletext += "</td>";
        }
        tabletext += "</tr>\n";
    }

    makeGap(tablegap + 1, tabletext);
    tabletext += "</tbody>\n";

    makeGap(tablegap, tabletext);
    tabletext += "</table>\n";

    makeGap(bodygap, tabletext);
    tabletext += "</body>\n";

    tabletext += "</html>\n";

    htmltable << tabletext;
    htmltable.close();
    cout << "Writing Table..." << endl;
    system("pause");
    return 0;
}
void makeGap(int-gap,std::string和text){
对于(int i=0;i随机表格颜色项目\n”;
makeGap(bodygap,tabletext);
tabletext+=“\n”;
makeGap(bodygap,tabletext);
tabletext+=“\n”;
makeGap(bodygap,tabletext);
tabletext+=“\n”;
makeGap(bodygap,tabletext);
tabletext+=“\n”;
makeGap(headgap,tabletext);
tabletext+=“\n”;
makeGap(headgap,tabletext);
tabletext+=“\n”;
makeGap(bodygap,tabletext);
tabletext+=“\n”;
makeGap(tablegap,tabletext);
tabletext+=“\n”;
makeGap(tablegap+1,tabletext);
tabletext+=“\n”;
makeGap(tablegap,tabletext);
tabletext+=“”;
对于(inti=0;i<10;i++){//表名
tabletext+=“”;
tabletext+=*(chars.begin()+i);
tabletext+=“”;
}
tabletext+=“\n”;
makeGap(tablegap+1,tabletext);
tabletext+=“”;
makeGap(tablegap+1,tabletext);
tabletext+=“\n”;
静态施法(时间(0));
对于(int row=0;row<38;row++){//Rows
makeGap(行间距、表格文本);
ostringstream oss;
int randnumber=rand()%10000;
如果(randnumber>0&&randnumber 2500&&randnumber 5000&&randnumber 7500&&randnumber 2500&&randnumber 5000&&randnumber 7500&&randnumber2种可选方式(肯定不止2种):

解决方案1

创建颜色的结构向量:

struct Color {
    int r, g, b;
};
vector<Color> colors;

void init_colors() {
    colors.push_back({255, 0, 0});
    colors.push_back({0, 255, 0});
}
解决方案2 每次随机RGB参数:

struct Color {
    int r, g, b;
};
Color get_rand_color() {
    return {rand() % 255, rand() % 255, rand() % 255};
}
还有更多的解决方案,但如果我需要,这两个解决方案将出现在我的代码中


希望能有所帮助。

请修改您的代码帖子,使其仅显示“随机颜色生成器”你已经有了一个策略-基于
randNumber
指定你的
td
id
。只需在你的
td
中添加一个内联样式属性。感谢codekaizer的帮助,你能再解释一下吗?我该怎么做呢?我只是从硬编码的背景色中获取颜色n HTML,(EX:P1是红色)。我需要基于RANDTENT产生的数字的颜色。谢谢:谢谢KOELK!!!但是我如何将输出应用到HTML?它在C++中工作,我尝试使用TD和I,但是我只是生成了一堆随机文本,而不是:在这里,我们可以编辑内容并在你的例子中插入一个例子,如果它仍然不清楚。我尝试了你的例子KorelK,但是我基本上需要的是C++中产生的颜色,但是在HTML中显示,我基本上不能硬编码任何颜色,它们必须随机地或逻辑地基于RAN来产生。“2 for循环”上生成的dom编号。非常感谢您抽出时间帮助我。:)我想你要找的是我主要评论中的解决方案1。你要找的是逻辑方法,每次都产生随机颜色,这就是正确的方法。如果你有一个包含10种颜色的列表,并且要使用新的颜色,你可以简单地随机选择一个介于0和9之间的数字,然后返回特定索引中的颜色e列表。如果我错了,请修复我。顺便说一句,帮助没有问题:)
struct Color {
    int r, g, b;
};
Color get_rand_color() {
    return {rand() % 255, rand() % 255, rand() % 255};
}