我的C程序的改进。 [英] Improvements in my C program.

查看:91
本文介绍了我的C程序的改进。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习编程。最近我写了一个转换温度的C程序。我听说总会有更多的方法让你的程序简短而有效。我想要一些改进程序的建议。



I am still learning to program. Recently I wrote a C program to convert temperature. I heard that there can are always more ways to make your program short and efficient. I want some suggestions to improve the program.

#include<stdio.h>
#include<conio.h>

celsiustofahrenheit(float celsius)
{
	float fahrenheit;
	
	fahrenheit = celsius * (9.0/5.0) + 32;
	
	return fahrenheit;
}

fahrenheittocelsius(float fahrenheit)
{
	float celsius;
	
	celsius = ( fahrenheit - 32 )  * 5.0/9.0;
	
	return celsius;
}

main()
{
	char option,option2;
	float input,output;

	do
	{
	printf("--WELCOME--\nThis is a temperature converter\n");
	printf("'C' for Fahrenheit to Celsius\n'F' for Celsius to Fahrenhiet\n'E' to exit the program\n");
	
	option = getch();
	
	if(option == 'c' || option == 'C')
	{
		printf("Enter temperature in Fahrenheit : ");
		scanf("%f",&input);
		
		output = fahrenheittocelsius(input);
		
	    printf("\n%.2f degrees Fahrenheit is %.2f degrees Celsius\n",input,output);
	}
	
	if(option == 'f' || option == 'F')
	{
		printf("\nEnter the Temperature in Celsius : ");
		scanf("%f",&input);
		
		output = celsiustofahrenheit(input);
		
		printf("\n%.2f degrees Celsius is %.2f degrees Fahrenheit\n",input,output);
	}
	
	if(option == 'e' || option == 'E')
	{
		return 0 ;
	}
	
	printf("To use the program again press 'Y' or else press any other key\n");
	option2 = getch();
    }
    while(option2 == 'Y'  || option2 == 'y');
	
	getch();
}





我的尝试:



如何在此程序中进行改进?



What I have tried:

How can I make improvements in this program?

推荐答案

为您的函数编写正确的签名:例如

Write the correct signature for your functions: e.g.
float celsiustofahrenheit(float celsius)





使您的 C 符合标准:

  • 不要'使用 conio header
  • main 函数必须返回 int


  • Make your C to comply with the standard:

    • dont' use conio header
    • The main function must return an int.
    • Quote:

      celsiustofahrenheit(float celsius)

      {

      浮动华氏度;



      fahrenheit =摄氏*(9.0 / 5.0)+32;



      返回华氏度;

      }

      celsiustofahrenheit(float celsius)
      {
      float fahrenheit;

      fahrenheit = celsius * (9.0/5.0) + 32;

      return fahrenheit;
      }



      这样:


      This way:

      double celsiustofahrenheit(double celsius)
      {
              return celsius * (9.0 / 5.0) + 32.0;	
      }


      1。您的转换函数默认返回int。您应该始终指定返回类型。

      2. C程序中的main必须返回一个int值。另外,指定返回类型。

      3.您可以在两个转换函数中删除局部变量。

      4.将输入字符转换为大写将为您节省几个测试用例。

      5.使用switch语句或使用if ... else if语句模式。使用多个if语句测试输入值是没有意义的,因为只有一个语句是真的。



      只是为了好玩 - 我快速重写了你的代码。 br $>


      1. Your conversion functions are returning int by default. You should always specify the return type.
      2. The main in a C program must return an int value. Also, specify the return type.
      3. You can get rid of the local variable in both conversion functions.
      4. Converting the input character to upper case will save you several test cases.
      5. Either use a switch statement or use the if...else if pattern of statements. Testing the input value with multiple if statements makes no sense, because only one statement will be true.

      Just for fun - I did a quick rewrite of your code.

      #include <stdio.h>
      #include <conio.h>
      #include <ctype.h>
      
      // Returns farentheit
      float CelsiusToFahrenheit(float celsius)
      {
      	// converting from double to float to stop warnings
      	return (float)(celsius * (9.0/5.0) + 32);
      }
      
      // Returns celsius
      float FahrenheitToCelsius(float fahrenheit)
      {
      	// converting from double to float to stop warnings
      	return (float)((fahrenheit - 32) * 5.0/9.0);
      }
      
      int main()
      {
      	char option;
      	float input,output;
      	
      	for(;;)
      	{
      		printf("\n--WELCOME--\nThis is a temperature converter\n");
      		printf("'C' for Fahrenheit to Celsius\n'F' for Celsius to Fahrenhiet\n'E' to exit the program\n");
      		
      		option = getch();
      		switch (toupper(option))
      		{
      		case 'C':
      			printf("\nEnter temperature in Fahrenheit : ");
      			scanf("%f",&input);
      			
      			output = FahrenheitToCelsius(input);
      			
      			printf("%.2f degrees Fahrenheit is %.2f degrees Celsius\n",input,output);
      			break;
      		case 'F':
      			printf("\nEnter the Temperature in Celsius : ");
      			scanf("%f",&input);
      			
      			output = CelsiusToFahrenheit(input);
      			
      			printf("%.2f degrees Celsius is %.2f degrees Fahrenheit\n",input,output);
      			break;
      		case 'E':
      			return 0;
      		default:
      			printf("\nInvalid input \'%c\': Please try again.\n", option);
      		}
          }
      
      	return 0;
      }


      这篇关于我的C程序的改进。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆