如何使这段代码更好?(C++温度转换器)
How can I make this code better? (C++ temperature converter)
我在练习C++类和函数时编写了这个温度转换程序。虽然代码有效,但我并不完全满意。有没有办法使这段代码更有效率?有没有我的代码中持续存在错误?如果你能批评我的代码,我会很高兴的。谢谢。
#include<iostream>
#include<string>
class convert{
public:
int c_con(float y){
float f;
std::cout << "Converting to Fahrenheit: ";
f=y*9/5+32;
std::cout << f << std::endl;
return 0;
}
int f_con(float x){
float c;
std::cout << "Converting to Celsius:";
c=(x-32)*5/9;
std::cout << c << std::endl;
return 0;
}
};
int main(){
char a;
int b;
convert temp;
std::cout << "__________Temp Converter-----------" << std::endl;
std::cout << "What would like to convert? (c/f): ";
std::cin >> a;
switch(a)
{
case 'c' : std::cout << "Input Celsius: ";
std::cin >> b;
temp.c_con(b);
break;
case 'f' :std::cout << "Input Fahrenheit: ";
std::cin >> b;
temp.f_con(b);
break;
default: std::cout << "Wrong input.";
}
return 0;
}
我相信
其他人有更好的建议,但一些非常基本的改进是:
#include<iostream>
// Don't import libraries you don't use.
class Convert //Classes typically have the leading character Capitalized.
{
public:
/*Give meaningful function names.*/
float celsius_To_Fahrenheit(const float &y) /*Placing "const" in your parameters is good practice if you don't need/want to change the parameter inside the function.*/
{
//Try not to use local variables in classes, use member variables if you do need a variable.
//I'm not sure either member function *needs* a local variable.
//And I don't think this very simple classes needs local variables, yet.
return y*9/5+32; /*Use "return" to return *something* otherwise, use "void" functions.*/
}
float fahrenheit_To_Celsius(const float &x)/*And using "&" to pass by reference is good in combination with "const", this makes your code more efficient so multiple copies don't exist of the same variable.*/
{
//Avoid putting std::cout statements inside classes as a habit.
return (x-32)*5/9;
}
};
int main()
{
char choice = 'z'; //Give meaningful variable names.
float temperature = 1; // Initialize local variables!
Convert temp_converter;
std::cout << "__________Temp Converter-----------" << std::endl;
std::cout << "What would like to convert? (c/f): ";
std::cin >> choice;
switch(choice)
{
case 'c' : std::cout << "Input Celsius: ";
std::cin >> temperature;
std::cout << temperature << " converted to Fahrenheit is " << temp_converter. celsius_To_Fahrenheit(temperature) << std::endl;
break;
case 'f' :std::cout << "Input Fahrenheit: ";
std::cin >> temperature;
std::cout << temperature << " converted to Celcius is " << temp_converter. fahrenheit_To_Celsius(temperature) << std::endl;
break;
default:
std::cout << "Wrong input.";
}
return 0;
}
我不会发布代码墙,因为@NonCreature0714已经这样做了。
我不会有一个名为convert
的类。你会把volt_amperes
和watts
也插在那里吗?
另外,如果你有很多使用摄氏度的函数,而另一个使用华氏度的负载会一起生活在同一个类中吗? 就我个人而言,我会有一个名为celsius
的代码单元和另一个称为fahrenheit
的代码单元,然后有一个名为celsius_fahrenheit
的第三个单元来处理两者之间的转换。 这意味着您可以拥有只需要摄氏度的代码,而无需拉入所有华氏度。
相关文章:
- 转换器练习:跳过 if 语句和 if 语句
- 使用转换器提升 Python 问题 - 静态链接
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 用于 boost::p roperty_tree 的通用枚举转换器
- 为什么我的C++基转换器中出现分段错误?
- 将字节数组转换为对象C++ |C++中的 C# 位转换器
- 英语到摩尔斯电码转换器
- 我用c++做了一个转换器,把英里转换成公里,把公里转换成英里,把赛尔修斯转换成法赫伦海特,把法赫伦海特转换成赛尔修斯
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 十进制,包括负到 32 位二进制转换器
- 标准::映射转换器模板
- 英里到千克转换器练习的小语法错误
- 句子转换器在存在新行的情况下不起作用
- 在C Windows程序中获取A/D转换器示例程序
- ROS中未定义的参考误差到OpenCV图像转换器
- 表达式到C++类类型转换器/类型转换
- 这里怎么了?单位转换器
- 华氏度到摄氏转换器C
- 如何理解 c++ 中的 MNIST 二进制转换器?