gpt4 book ai didi

c++ - C++输出对齐

转载 作者:行者123 更新时间:2023-12-02 10:18:59 25 4
gpt4 key购买 nike

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

double start_temp, end_temp, incr, fahrenheit, celsius, kelvin;
do
{
cin >> start_temp >> end_temp >> incr;
if (start_temp > end_temp || incr <= 0)
{
cout << "Starting temperature must be <= ending temperature and increment must be > 0.0\n";
}
} while (start_temp > end_temp || incr <= 0);
cout << fixed << setprecision(4);

cout << "\n"
<< left << setw(18) << "Fahrenheit" << right << setw(18) << "Celsius" << setw(18) << "kelvin";

for (fahrenheit = start_temp; fahrenheit <= end_temp; fahrenheit = fahrenheit + incr)

{
celsius = (fahrenheit - 32) / 1.8;
kelvin = celsius + 273.15;

cout << "\n"
<< left << setw(18) << fahrenheit << right << setw(18) << celsius << right << setw(18) << kelvin;
}

return 0;
}


该数字应为18个字符宽,精度为4位数字,并且必须采用固定格式。不要使用制表符(\ t)输出值.......
这是我要输出的内容:
-30 100 20

Fahrenheit Celsius kelvin
-30.0000 -34.4444 238.7056
-10.0000 -23.3333 249.8167
10.0000 -12.2222 260.9278
30.0000 -1.1111 272.0389
50.0000 10.0000 283.1500
70.0000 21.1111 294.2611
90.0000 32.2222 305.3722

华氏温度列中的数字未正确对齐........
我希望我的输出看起来像这样
这是带有有效输入的示例运行:

-30 100 20
输出为:
    Fahrenheit           Celsius            Kelvin
-30.0000 -34.4444 238.7056
-10.0000 -23.3333 249.8167
10.0000 -12.2222 260.9278
30.0000 -1.1111 272.0389
50.0000 10.0000 283.1500
70.0000 21.1111 294.2611
90.0000 32.2222 305.3722

考虑以下输入:
100.5 110.4 5
有效输出为:
    Fahrenheit           Celsius            Kelvin
100.5000 38.0556 311.2056
105.5000 40.8333 313.9833

我被困住了,请帮忙

最佳答案

当它应该正确时,您已向左对齐。

cout << "\n"
<< right << setw(18) << "Fahrenheit" << right << setw(18) << "Celsius" << setw(18) << "kelvin";

...
cout << "\n"
<< right << setw(18) << fahrenheit << right << setw(18) << celsius << right << setw(18) << kelvin;
}

Running sample

输出:

        Fahrenheit           Celsius            kelvin
-30.0000 -34.4444 238.7056
-10.0000 -23.3333 249.8167
10.0000 -12.2222 260.9278
30.0000 -1.1111 272.0389
50.0000 10.0000 283.1500
70.0000 21.1111 294.2611
90.0000 32.2222 305.3722

关于c++ - C++输出对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61042821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com