华氏温度为摄氏改进方案,并选择输出 [英] Fahrenheit to Celsius program improvement and choosing the output

查看:193
本文介绍了华氏温度为摄氏改进方案,并选择输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始是从事C编程前几天,我想提高我的计划,但不知道该怎么做。

I just started programming in C a few days ago and I want to improve my program but not sure how to do it.

这程序是华氏温度为摄氏温度,反之亦然转换器。我做到了尽可能以最简单的方式。但现在我想这样做,让我有2个功能C2F和F2C内搭温度作为参数,当我跑我想你选择我是否要隐蔽从F到C或从C到F(类似程序TempConverter -f 32这应该只转换为32摄氏度和TempConverter -c 100应该隐蔽到100华氏度)。

This program is a Fahrenheit to Celsius and vice versa converter. I did it in the simplest way possible. But now I want to do it so that I have 2 functions c2f and f2c which take the temperature as a parameter and when I run the program I want do choose whether I want to covert from F to C or from C to F (something like TempConverter -f 32 this should only convert 32 into celsius and TempConverter -c 100 should covert 100 into Fahrenheit).

我觉得我的职能应该是这样的:浮动C2F(浮点C)浮动F2C(浮动六)

I think my functions should be something like this : float c2f (float c) and float f2c (float f)

但是,究竟如何它做到这一点,所以当我运行像> TempConverter -f 50.0 我得到这样的事情,让我们说什么?
10.00°C = 50.00°F

But how exactly do it do it so when I run something like > TempConverter -f 50.0 I get something like this let's say? 10.00°C = 50.00°F

#include<stdio.h>

int main(void)
{
    // Local Declarations
    float Celsius, Fahrenheit, Fahrenheit_1, Celsius_2;

    // Statements
    printf("Enter the temperature in Fahrenheit: ");
    scanf("%f", &Fahrenheit);
    printf("Fahrenheit temperature is: %5.1f F\n\a", Fahrenheit);

    Celsius = (100.0 / 180.0) * (Fahrenheit - 32);

    printf("Celsius temperature is: %8.1f C\n\n\a", Celsius);

    printf("Enter the temperature in Celsius: ");
    scanf("%f", &Celsius_2);
    printf("Celsius temperature is: %8.1f C\n\a", Celsius_2);

    Fahrenheit_1 = 32 + (Celsius_2 * (180.0 / 100.0));

    printf("Fahrenheit temperature is: %5.1f F\a", Fahrenheit_1);

    return 0;

}

电流输出:

*华氏输入温度:20

*Enter the temperature in Fahrenheit: 20

华氏温度为:20.0˚F

Fahrenheit temperature is: 20.0 F

摄氏温度为:-6.7ç

Celsius temperature is: -6.7 C

在摄氏输入温度:40

Enter the temperature in Celsius: 40

摄氏温度为:40.0ç

Celsius temperature is: 40.0 C

华氏温度为:104.0 F *

Fahrenheit temperature is: 104.0 F*

所需的输出

TempConverter -f 50.0

TempConverter -f 50.0

10.00°C = 50.00°F

10.00°C = 50.00°F

推荐答案

你需要做的第一件事就是添加命令行参数到code,像这样:

The first thing you'll need to do is add command line arguments to your code, like so:

int main( int argc, char **argv )

ARGC 包含命令行中输入参数的数量;它总是> = 1

argc contains the number of parameters entered on the command line; it is always >= 1.

的argv 包含指向每一个命令行参数字符串。 的argv [0] 总是指向用于启动该程序的字符串。 ARGV [ARGC] 始终是NULL。

argv contains pointers to each of the command line argument strings. argv[0] always points to the string you used to start the program. argv[argc] is always NULL.

所以,如果你叫你的程序为

So, if you called your program as

TempConverter -f 50.00

然后

argc == 3
argv[0] == "TempConverter"
argv[1] == "-f"
argv[2] == "50.00"
argv[3] == NULL

请注意,要利用最后的参数转换功能,你需要它从一个字符串转换为浮点值。 的strtod 是首选的库函数本:

Note that to use the last parameter in your conversion functions, you'll need to convert it from a string to a floating-point value. strtod is the library function of choice for this:

char *check;
double intemp = strtod(argv[2], &check);

转换后,检查将指向字符串中的第一个字符,这是的的转换为浮点值。如果该字符不是空格或0以外的东西,那么输入值不是有效的浮点字符串。

After the conversion, check will point to the first character in the string that was not converted to a floating point value. If this character is something other than whitespace or 0, then the input value was not a valid floating-point string.

所以,操作的一般顺序:

So, the general order of operations:


  1. 检查 ARGC 的价值。你的目的,它需要3。

  2. 转换的argv [2] 来一个浮点值,使用的strtod 。确保选中字符为0或空格。

  3. 检查华氏/摄氏开关;它需要为 - F。 - C(或任何你决定)

  4. 基于呼叫华氏/摄氏开关上的相应转换功能,并显示结果。

  1. Check the value of argc. For your purposes, it needs to be 3.
  2. Convert argv[2] to a floating-point value using strtod. Make sure the check character is either 0 or whitespace.
  3. Check your fahrenheit/celcius switch; it needs to be either "-f" or "-c" (or whatever you decide on).
  4. Call the appropriate conversion function based on the fahrenheit/celcius switch, and display the result.

这篇关于华氏温度为摄氏改进方案,并选择输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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