C if语句问题?

标签 c

我正在尝试练习制作 if 语句,但运气不佳。现在我正在尝试制作一个三角函数计算器,一个非常简单的,使用 if 语句,但我无法让它工作。实际问题出现在输入三角函数(正弦、余弦、正切)之后。这就是发生的事情。
1.我自己编译
2. 输出用户提示
3. 我输入函数并按回车键
4.程序跳转到一个新的空行
5. 如果我按 enter 没有任何反应并且程序关闭

这是代码本身。如果我做了一些非常愚蠢的事情,请善待,我是 C 的新手。

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

int main(void)
{
    float x;
    float a, o, h;
    float sine, cosine, tangent;

    printf("Enter the trig function you wish to calculate: ");
    scanf("%f", &x);

    if (x == sine)
    {    printf("Enter the value of the opposite leg: ");
         scanf("%f", &o);
         printf("Enter the value of the hypotenuse: ");
         scanf("%f", &h);

         sine = o / h;
         printf("The sine is equal to %f", sine);
    }

    if (x == cosine)
    {    printf("Enter the value of the adjacent leg: ");
         scanf("%f", &a);
         printf("Enter the value of the hypotenuse: ");
         scanf("%f", &h);

         cosine = a / h;
         printf("The cosine is equal to %f", cosine);
    }

    if (x == tangent)
    {    printf("Enter the value of the opposite leg: ");
         scanf("%f", &o);
         printf("Enter the value of the adjacent leg: ");
         scanf("%f", &a);

         tangent = o / a;
         printf("The tangent is equal to %f", tangent);
    }

    getch();
}

感谢所有真正提供帮助并且没有因为我的不理解而粗鲁的人,我没有意识到我必须添加一个数字字符而不仅仅是一个字符。

最佳答案

简单(最少)修复

是的,您即将做出各种被经验丰富的程序员称为愚蠢的事情,但它们是新手会犯的错误(您既不是第一个也不是最后一个犯错误的人)。

int main(void)
{
    float x;
    float a, o, h;
    float sine, cosine, tangent;

    printf("Enter the trig function you wish to calculate: ");
    scanf("%f", &x);

    if (x == sine)

主要问题是您没有给出sinecosinetangent 值,因此您不知道要输入什么使平等发挥作用。

第二个问题是比较 float 是否相等并不是一个好主意。

你可能会用这样的东西做得最好:

int main(void)
{
    int x;
    float a, o, h;
    enum { sine, cosine, tangent };

    printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
    scanf("%d", &x);

    if (x == sine)

这或多或少是正统的,读取和比较整数的相等性是可靠的。您必须更改操作,因为我已经预先将名称 sinecosinetangent 作为枚举(整数)常量。您可以通过为常量使用大写名称(这很正统),或为名称使用前缀,或 ...

int main(void)
{
    int x;
    float a, o, h;
    float sine, cosine, tangent;
    enum { SINE, COSINE, TANGENT };

    printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
    scanf("%d", &x);

    if (x == SINE)

更友好的输入

您可能会从下面的评论中了解到,最好是允许用户输入他们想要输入的功能的名称,而不是让他们输入编码数字。可靠地编写代码有点棘手,这就是我在上面使用数字留下答案的主要原因。

#include <stdio.h>
#include <string.h>

int main(void)
{
    char line[4096];

    printf("Enter trig function you wish to calculate: ");
    if (fgets(line, sizeof(line), stdin) != 0)
    {
        char *nl = strchr(line, '\n');
        if (nl != 0)
            *nl = '\0';
        if (strcmp(line, "sine") == 0)
        {
            /* Process sine */
        }
        else if (strcmp(line, "cosine") == 0)
        {
            /* Process cosine */
        }
        else if (strcmp(line, "tangent") == 0)
        {
            /* Process tangent */
        }
        else
        {
            fprintf(stderr, "Unrecognized trig function (%s)\n", line);
        }
    }
}

4096 只是一个很大的整数,它太长了以至于任何人都不太可能输入比这更长的行。如果他们确实输入了这样一行,那么 GIGO 和他们得到他们应得的(这将是一个礼貌的错误消息,表示他们输入的名字未被识别)。

这仍然不是美妙的代码。去除前导和尾随空格可能是合理的,并且可能将输入大小写转换为小写,并且其中一条消息可能应该标识有效的函数名称。代码可能会重复循环,但随后您需要一个函数来提示和读取响应等。所有这些都以增加可用性为代价增加了可用性,这对新手程序员来说不必要地使事情复杂化。

关于C if语句问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14087862/

相关文章:

C 二维数组内存分配

c - 通用链表库中的列表创建功能出现问题

c - 如何根据库中的地址查找函数名称

c - 如何使用共享内存传输FILE结构(POSIX SKIN)

c - fflush 和 'no disk space left'

c++ - 头文件礼仪

c - 使用scanf()时如何区分整数和字符

c++ - 如何从 wchar_t 解码 char

字符常量对其类型而言太长

从程序集调用 C printf