如何使这段代码更好?(C++温度转换器)

How can I make this code better? (C++ temperature converter)

本文关键字:转换器 C++ 更好 温度 代码 何使这 段代码      更新时间:2023-10-16

我在练习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_ampereswatts也插在那里吗?

另外,如果你有很多使用摄氏度的函数,而另一个使用华氏度的负载会一起生活在同一个类中吗? 就我个人而言,我会有一个名为celsius的代码单元和另一个称为fahrenheit的代码单元,然后有一个名为celsius_fahrenheit的第三个单元来处理两者之间的转换。 这意味着您可以拥有只需要摄氏度的代码,而无需拉入所有华氏度。