C++摄氏度至华氏3度

C++ celsius to fahrenheit 3 times

本文关键字:3度 摄氏度 C++      更新时间:2023-10-16

我正在学习C++,为此我给自己制造了一个问题,那就是在控制台中将摄氏度转换为华氏度三次。用户将输入摄氏度。我还希望输出显示如下:

Celsius: Fahrenheit: cel1 fahr1 cel2 fahr2 cel3 fahr3

到目前为止,我尝试过的代码是:

double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;
cout << "Celsius degree one: ";
cin >> cel1;
cout << "Celsius degree two: ";
cin >> cel2;
cout << "Celsius degree three: ";
cin >> cel3;
fahr1 = (cel1 * 9) / 5 + 32;
fahr2 = (cel2 * 9) / 5 + 32;
fahr3 = (cel3 * 9) / 5 + 32;
// messy like this to display like I want to 
cout << endl <<
    "Celsius:  " << "Fahrenheit:" << endl <<
    cel1 << "         " << fahr1 << endl <<
    cel2 << "         " << fahr2 << endl <<
    cel3 << "         " << fahr3 << endl << endl;

它会像我想的那样显示,但我觉得这本可以用一种更简单的方式实现,所以我尝试了这样的循环,但我不知道如何正确地做到:

double celsius;
for (int times = 0; times != 3; ++times){
    cout << "Celsius degree: ";
    cin >> celsius;
    double fahrenheit = (celsius * 9) / 5 + 32;
    cout << "Fahrenheit degree: " << fahrenheit << endl;
    cin.clear();
}

这个代码比上一个代码少,给出了正确的答案,并且会转换三次,但我不知道如何像我想的那样显示它。

我的问题是,最好的方法是什么?

我建议将代码拆分为更小的函数:

计算转换的

double celsius_to_fahrenheit(double celsius)
{
    return (celsius * 9.0) / 5.0 + 32.0;
}

要获得输入,我选择使用std::vector作为容器。您可以选择CCD_ 3,因为阵列具有固定的大小,但CCD_ 4是一个很好的默认选择。

std::vector<double> get_input_celsius(std::size_t size)
{
    std::vector<double> celsius(size);
    for (std::size_t i = 0; i != celsius.size(); ++i) {
        std::cout << "Celsius degree " << (i + 1) << ": ";
        std::cin >> celsius[i];
    }
    return celsius;
}

显示结果的方法。我选择不将转换存储在新的std::vector中,因为之后不会使用:

void display_celsius_and_fahrenheit(const std::vector<double>& celsius)
{
    std::cout << std::endl << "Celsius:  " << "Fahrenheit:" << std::endl;
    for (auto c : celsius) { // for range since C++11
        std::cout << c << "         " << celsius_to_fahrenheit(c) << std::endl;
    }
}

最后是main函数:

int main()
{
    std::vector<double> celsius = get_input_celsius(3);
    display_celsius_and_fahrenheit(celsius);
    return 0;
}

实例

  1. 创建数组来存储温度。

    代替

    double cel1, cel2, cel3;
    double fahr1, fahr2, fahr3;
    

使用

    double celsius[3];
    double fahrenheit[3];
  1. 更改输入收集循环以使用数组。

    for (int times = 0; times != 3; ++times){
        cout << "Celsius degree: ";
        cin >> celsius[times];
        fahrenheit[times] = (celsius[times] * 9) / 5 + 32;
        cin.clear();
    }
    
  2. 使用循环创建输出:

    cout << endl << "Celsius:  " << "Fahrenheit:" << endl <<
    for (int times = 0; times != 3; ++times){
      cout << celsius[times] << "         " << fahrenheit[times] << endl;
    }
    

如果只在输出期间计算数组fahrenheit,那么也可以不创建它。

在这种情况下,将输入收集循环更改为:

    for (int times = 0; times != 3; ++times){
        cout << "Celsius degree: ";
        cin >> celsius[times];
        cin.clear();
    }

将输出循环更改为:

    cout << endl << "Celsius:  " << "Fahrenheit:" << endl <<
    for (int times = 0; times != 3; ++times){
      double fahrenheit = (celsius[times] * 9) / 5 + 32;
      cout << celsius[times] << "         " << fahrenheit << endl;
    }