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

时间:2016-07-11 05:14:03

标签: c++

我在用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;
}

2 个答案:

答案 0 :(得分:2)

我确信其他人有更好的建议,但一些非常基本的改进是:

#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;
}

答案 1 :(得分:1)

我不会发布一段代码,因为@ NonCreature0714已经这样做了。

我不会有一个名为convert的课程。你会在那里贴上volt_ampereswatts吗?

如果你有很多使用Celsius的函数,并且使用Fahrenheit的另一个加载它们都在同一个类中共存,会发生什么?就个人而言,我会有一个名为celsius的代码单元和另一个名为fahrenheit的代码单元,然后有一个名为celsius_fahrenheit的第三个单元,用于处理两者之间的转换。这意味着您可以拥有仅需要摄氏温度的代码而无需拉入所有华氏温度。