0

我正在尝试编写一个程序,将摄氏度转换为华氏度,反之亦然。我的程序正在编译,但它给了我错误的结果。我一直在尝试通过更改指针来更改它,但它不起作用。谁能指出我的问题是什么?在我看来,它在声明和指针中,但我不确定。谢谢!

#include<stdio.h>

float f2c(float f);
float c2f(float c);

int main(void)
{
 float cel;
 float celcius;
 float fahren;
 float fah;
 char ch;
 float number;

scanf("%c %f", &ch, &number);

if(&ch == "-f"){
  f2c(number);
  celcius=f2c(number);
  printf("%f", celcius);
  }

 else{
  c2f(number);
  fahren = c2f(number);
  printf("%f", fahren);
  }

return 0;
}

float f2c(float f)
{
 float cel = (f - 32) * 5/9;
 return cel;
} 

float c2f(float c)
{
 float fah = (9 * c/5 + 32);
 return fah;
}
4

4 回答 4

3

if(&ch == "-f")这就是问题。它应该是 :

if(ch == 'f')

于 2012-07-05T12:01:38.370 回答
1

您正在将 char 与 C-string 进行比较"-f"。用于strcmp比较 C 字符串。

您可能还ch需要在此处将多个字符声明为 char 数组。因为,您不能像现在那样将“-f”存储在单个字符中。

随着变化:

#include<stdio.h>

float f2c(float f);
float c2f(float c);

int main(void)
{
 float cel;
 float celcius;
 float fahren;
 float fah;
 char ch[25];
 float number;

scanf("%s %f", ch, &number);

if(strcmp(ch,"-f")==0){
  f2c(number);
  celcius=f2c(number);
  printf("%f", celcius);
  }

 else{
  c2f(number);
  fahren = c2f(number);
  printf("%f", fahren);
  }

return 0;
}

float f2c(float f)
{
 float cel = (f - 32) * 5/9.0;
 return cel;
} 

float c2f(float c)
{
 float fah = (9 * c/5.0 + 32);
 return fah;
}

这本质上是您的代码,但将其修改为将输入读取为 C 字符串。

于 2012-07-05T12:01:26.847 回答
1

除了其他答案:

您正在对转换函数进行不必要的调用并丢弃结果,就在再次执行相同的调用但正确处理返回值之前:

if(&ch == "-f"){
  f2c(number);  /* This line does absolutely nothing! */
  celcius=f2c(number);
  printf("%f", celcius);
  }
于 2012-07-05T12:04:55.747 回答
-1

使用浮点算术,而不是整数算术,因为在整数算术中,5/9 为 0。所以您可以对代码进行此更改(尽管float参数不常见):

float f2c(float f)
{
 return  (f - 32.0) * 5.0/9.0;
} 

float c2f(float c)
{
 return 9.0 * c/5.0 + 32.0;
}

同样作为其他答案之一,条件(&ch == "-f")永远不会成立。

这是一个完整的工作示例,带有错误检查。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <assert.h>

static double f2c(double f)
{
 return (f - 32.0) * 5.0/9.0;
} 

static double c2f(double c)
{
 return 9.0 * c/5.0 + 32.0;
}

int main (int argc, char *argv[])
{
 int i;
 const char *label_f = "Fahrenheit";
 const char *label_c = "Celcius";
 const char *from, *to;
 double (*conversion)(double);

 from = label_c;
 to = label_f;
 conversion = c2f;

 for (i=1; i<argc; ++i)
   {
     if (0 == strcmp(argv[i], "-f"))
       {
         from = label_f;
         to = label_c;
         conversion = f2c;
       }
     else if (0 == strcmp(argv[i], "-c"))
       {
         from = label_c;
         to = label_f;
         conversion = c2f;
       }
     else
       {
         char *suffix = NULL;
         double temp_in;

         errno = 0;
         temp_in = strtod(argv[i], &suffix);

         if (ERANGE == errno)
           {
             if (HUGE_VAL == temp_in) 
               {
                 fprintf(stderr, "%s is too big", argv[i]);
                 return 1;
               }
             else
               {
                 assert(-HUGE_VAL == temp_in);
                 fprintf(stderr, "%s is too small", argv[i]);
                 return 1;
               }
           }
         else if (errno != 0 && (suffix == argv[i]))
           {
             /* no conversion was performed. */
             fprintf(stderr, "%s is not a number", argv[i]);
             return 1;
           }
         else if (*suffix) 
           {
             fprintf(stderr, "Expected a number but saw '%s'\n", argv[i]);
             return 1;
           }
         else
           {
             const double temp_out = (*conversion)(temp_in);
             printf("%f %s is %f %s.\n", temp_in, from, temp_out, to);
           }
       }
   }
 return 0;
}
于 2012-07-05T12:03:02.617 回答