使用循环 c++ 的温度表
Temperature table using for loop c++
编码
的超级新手,我已经在这个问题上停留了一段时间。我需要制作一个程序,以摄氏度为单位输入温度,并输入总共 20 行的增量。在这个过程中,它转化为华氏度、开尔文度和兰金度。这一切都很好,但我无法通过我的输入来增加值。
例如,它应该看起来:
输入温度(摄氏度(:50
输入增量:5
Cel Far Kel Rank
1 - 50
2 - 55
3 - 60
....
20 - 150
我根本无法让值递增。正在阅读我的笔记并在线查看以了解问题,但没有运气。
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int CELS; // celsius entry
int x; // x = input value for increment
double FAH = (CELS * (9.0 / 5) + 32); // farenheit conversion
double KEL = (CELS + 273.15); // kelvin conversion
double RANK = ((CELS + 273.15) * (9.0 / 5)); // rankine conversion
cout << "Enter the temperature in celsius: ";
cin >> CELS;
while ((CELS < -273.15) || (CELS > 5000))
{
cout << "ERROR: out of range." << endl;
cout << "Enter the temperature in celsius: ";
cin >> CELS;
}
cout << "Enter the increment value: ";
cin >> x;
cout << endl;
cout << " # Cels Fahr Kel Rank" << endl;
cout << right;
for (int i = 1; i <= 20; i++)
{ //
for (double j = CELS; j <= CELS; x++)
{ //
for (double k = (CELS * (9.0 / 5) + 32);
k <= (CELS * (9.0 / 5) + 32); x++)
{
for (double m = (CELS + 273.15); m <= (CELS + 273.15); x++)
{
for (double n = ((CELS + 273.15) * (9.0 / 5));
n <= ((CELS + 273.15) * (9.0 / 5)); x++)
{
cout << setw(10) << i << setw(10) << j << setw(10) << k
<< setw(10) << m << setw(10) << n << endl;
}
}
}
}
}
}
并忽略为什么我将公式放在 for 循环中。公式无法使用声明的变量,因此同时执行此操作。
您可以使用一些用户定义的函数来计算从摄氏度到其他单位的转换。
那么你只需要一个循环:
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::setw;
// define some constant
const double zero_point = 273.15;
constexpr double factor = 9.0 / 5.0;
const double max_cels = 5000;
// farenheit conversion
double cels_to_far( double cels ) {
return cels * factor + 32.0;
}
// kelvin conversion
double cels_to_kel( double cels ) {
return cels + zero_point;
}
// rankine conversion
double cels_to_ran( double cels ) {
return ( cels + zero_point ) * factor;
}
int main()
{
double temp_cels;
double delta_t;
cout << "Enter the temperature in celsius: ";
cin >> temp_cels;
while ((temp_cels < -zero_point) || (temp_cels > 5000)) {
cout << "ERROR: out of range.n";
cout << "Enter the temperature in celsius: ";
cin >> temp_cels;
}
cout << "Enter the increment value: ";
cin >> delta_t;
cout << "n # Cels Fahr Kel Rankn";
// output loop
for ( int i = 0; i < 20; ) {
cout << setw(10) << ++i << std::fixed << std::setprecision(2)
<< setw(10) << temp_cels
<< setw(10) << cels_to_far(temp_cels)
<< setw(10) << cels_to_kel(temp_cels)
<< setw(10) << cels_to_ran(temp_cels) << std::endl;
temp_cels += delta_t;
}
}
输出符合预期(50°C,增量为5°(:
# Cels Fahr Kel Rank
1 50.00 122.00 323.15 581.67
2 55.00 131.00 328.15 590.67
3 60.00 140.00 333.15 599.67
4 65.00 149.00 338.15 608.67
5 70.00 158.00 343.15 617.67
6 75.00 167.00 348.15 626.67
7 80.00 176.00 353.15 635.67
8 85.00 185.00 358.15 644.67
9 90.00 194.00 363.15 653.67
10 95.00 203.00 368.15 662.67
11 100.00 212.00 373.15 671.67
12 105.00 221.00 378.15 680.67
13 110.00 230.00 383.15 689.67
14 115.00 239.00 388.15 698.67
15 120.00 248.00 393.15 707.67
16 125.00 257.00 398.15 716.67
17 130.00 266.00 403.15 725.67
18 135.00 275.00 408.15 734.67
19 140.00 284.00 413.15 743.67
20 145.00 293.00 418.15 752.67
你只需要一个for循环,递增x.(嵌套的for循环是一个错误(
从您需要的单 for 循环开始:通过输出表的行进行计数的循环。
在循环的主体中,计算该行所需的所有内容,并将其输出。
像这样:
for( int line_count = 0;
line_count < 20;
line_count++ )
{
double line_temp_celsius =
start_celsius + line_count * celsius_increment;
// calculate the other values based line_temp_celsius
...
}
计算必须在更改 CELS
值的循环中。应该有一个循环。结果如下所示。观看现场演示
for (int i = 1; i <= 20; i++, CELS+=x)
{
double FAR = (CELS * (9.0 / 5) + 32); // farenheit conversion
double KEL = (CELS + 273.15); // kelvin conversion
double RANK = ((CELS + 273.15) * (9.0 / 5)); // rankine conversion
cout << setw(10) << i << setw(10) << CELS << setw(10) << FAR
<< setw(10) << KEL << setw(10) << RANK << endl;
}
相关文章: