c++ - 定义比例因子以图形化三个量,其总和为常数

标签 c++ stdout stdstring

我正在尝试在标准输出中编写图,程序返回“#”行,在另一个函数更改它们后,这些行应加到初始数量上。我保证修改数字的功能不会出错。这是我的代码:

struct mystruct {
    long long int s;
    long long int i;
    long long int r;
}

mystruct initial_;

void draw_row(mystruct P)
{
    long long int Ntotal = initial_.s + initial_.i;
    int scale = round(Ntotal / 10);
    std::string a(Ntotal / scale, '#');
    std::string b(round(P.s / scale), '#');
    std::string c(round(P.i / scale), '#');
    std::string d(round(P.r / scale), '#');
    std::cout << P.s << " " << P.i << " " << P.r << '\n';
    std::cout << scale << '\n';
    std::cout << a << '\n' << b << c << d << '\n';
}
这些是其某些输出的示例:
499 1 0
##########
#########

0 450 50
##########
##########

0 249 251
##########
#########

最佳答案

您正在行中进行整数除法

int scale = round(Ntotal/10) ; 
std::string a(Ntotal/scale,'#') ;
std::string b(round(P.s/scale),'#') ;
std::string c(round(P.i/scale),'#') ;
std::string d(round(P.r/scale),'#') ;
并且其余部分被截断,因此此处使用的round()无法正常工作。
您可以执行(A + B/2) / B来取整两个正整数A / B的除法结果,因此行应为
int scale = (Ntotal+5)/10 ; 
std::string a(Ntotal/scale,'#') ;
std::string b((P.s+scale/2)/scale,'#') ;
std::string c((P.i+scale/2)/scale,'#') ;
std::string d((P.r+scale/2)/scale,'#') ;

关于c++ - 定义比例因子以图形化三个量,其总和为常数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63560386/

相关文章:

c++ - CUDA 将 GpuMat 的 c 数组传递给内核

c++ - 如何用 C++ 编写简短的文字?

testing - 打印功能的 Spock 测试输出

c++ - C++ 字符串比较失败

c++ - boost ASIO 并发度

c++ - 将变量初始化为包含派生类的 vector 中的对象

perl - 如何在 Log4perl 中抑制输出到 stdout 和 stderr?

Python:如何写入 fd 3?

运行我的随机 ID 生成时出现 C++ 段错误

c++ - Google Protocol Buffer 序列化字符串可以包含嵌入的 NULL 字符吗?