2016-07-11 3 views
-3

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; 
} 
+4

코드에 문제가없는 경우 http://codereview.stackexchange.com/에 게시해야합니다. – ifma

+1

변환 된 온도와 같은 변환 함수에서 의미있는 것을 반환하고 변환 함수에서 인쇄 문을 이동합니다. – Galik

+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

그리고 _do_를 사용하면 지역 변수는 초기화되지 않은 상태로 두지 마십시오. 'float f;/* ... 다른 코드 ... */f = 1;'실수로 할당 전에'f '를 사용하면'float f = 1;'을 사용하는 것이 더 안전합니다. – CompuChip

+0

그 점을 지적 해 주셔서 감사합니다! – NonCreature0714

1

나는 이미 그 일을하고있다 @ NonCreature0714 같은 코드의 벽을 게시하지 않습니다.

나는 convert이라는 클래스를 가지지 않을 것이다. volt_ampereswatts도 붙이시겠습니까?

섭씨를 사용하는 기능이 많고 화씨를 사용하는 다른 부하가 같은 클래스에 함께 살면 어떻게됩니까? 개인적으로 코드 단위는 celsius이고 다른 코드 단위는 fahrenheit이고 두 번째 단위는 celsius_fahrenheit이며이 두 단위 간의 변환을 처리합니다. 즉 화씨를 모두 사용하지 않고 섭씨가 필요한 코드를 만들 수 있습니다.

관련 문제