C++摄氏度至华氏3度
C++ celsius to fahrenheit 3 times
我正在学习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;
}
实例
-
创建数组来存储温度。
代替
double cel1, cel2, cel3; double fahr1, fahr2, fahr3;
使用
double celsius[3];
double fahrenheit[3];
更改输入收集循环以使用数组。
for (int times = 0; times != 3; ++times){ cout << "Celsius degree: "; cin >> celsius[times]; fahrenheit[times] = (celsius[times] * 9) / 5 + 32; cin.clear(); }
使用循环创建输出:
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;
}
相关文章:
- 函数复杂度分析
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 函数的时间复杂度是多少?
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 在 C++ 中将 RGB 更改为灰度
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 如何计算此排序函数的时间复杂度?
- 计算两个代码块的时间复杂度
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 具有嵌套 if-else 的循环的时间复杂度
- C++中 std::map 的运行时复杂度是多少?
- OpenCV - 如何在灰度图像上应用 Kmeans?
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 如何计算函数的时间复杂度?
- 华氏度到摄氏度的错误结果
- 输入摄氏度/华氏度,然后进行转换
- C++摄氏度至华氏3度