0

我正在尝试制作一个在 Windows 控制台中启动的程序,该程序会在电阻器中询问“第一个波段的颜色是什么”,因此当您键入诸如“红色”之类的内容时,它会将 2 输入到浮点数中。我遇到的问题是,我不仅不知道如何将“红色”变成“2”,而且每当我尝试在控制台上输入文本时,它都会显示其余的 printf 并跳到最后。

这就是我到目前为止所做的,我注释掉了第三和第四个输入,以便我可以更轻松地进行故障排除。

//Color bands

#include <stdio.h>

int
main(void)
{
    double first;
    double second;
    //double third;
    //double fourth;
    double total_resist;
    double black, brown, red = 2, orange, yellow, green, blue, violet, gray, white;

    black = 0;
    brown = 1;
    red = 2;
    orange = 3;
    yellow = 4;
    green = 5;
    blue = 6;
    violet = 7;
    gray = 8;
    white = 9;

    printf("Input first color band> ");
    scanf("%lf", &first);

    printf("Input second color band> ");
    scanf("%lf", &second);

    total_resist = first + second;// + second + third + fourth;

    printf("\nTotal resistance is %.lf\n", total_resist);

    return(0);
}
4

4 回答 4

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

static int colour_value(char const *colour)
{
    const char *band[] =
    {
        "black", "brown", "red", "orange", "yellow",
        "green", "blue", "violet", "grey", "white"
    };
    enum { NUM_BANDS = sizeof(band) / sizeof(band[0]) };
    for (int i = 0; i < NUM_BANDS; i++)
    {
        if (strcmp(colour, band[i]) == 0)
            return i;
    }
    return -1;
}

int main(void)
{
    double total_resistance = 0.0;
    const char *ordinals[] = { "zeroth", "first", "second", "third", "fourth" };
    char colour[32];
    char band[4][8];
    int i;

    for (i = 0; i < 4; i++)
    {
        int value;
        printf("Input %-6s color band> ", ordinals[i + 1]);
        if (scanf("%s", colour) != 1)
            break;
        if ((value = colour_value(colour)) == -1)
            break;
        total_resistance = total_resistance * 10.0 + value;
        strcpy(band[i], colour);
    }

    if (i == 4)
    {
        printf("%s", "Bands: ");
        for (i = 0; i < 4; i++)
            printf(" %s", band[i]);
        printf("\nTotal resistance is %.lf\n", total_resistance);
    }
    else
        puts("Oops!");

    return(0);
}

示例运行:

Input first  color band> orange
Input second color band> green
Input third  color band> violet 
Input fourth color band> brown
Bands:  orange green violet brown
Total resistance is 3571


Input first  color band> red
Input second color band> yellow
Input third  color band> black
Input fourth color band> blue
Bands:  red yellow black blue
Total resistance is 2406
于 2014-03-03T01:43:23.823 回答
0

怎么样:

char color[20];

printf("Input first color band> ");
scanf("%19s", color);

if (!strcmp(color, "red"))
    first = 2.0;
else if (...)

显然下一步是制作一个函数:

double color2number(const char *name)
于 2013-04-08T10:10:37.157 回答
0

这应该做的工作:

#include <stdio.h>



int color2nr(char* color){
if(color=="red")
   return red;
else if(color=="orange")
   return orange;
...  
}

对所有其他颜色做同样的事情......

main(void)
{
double first;
double second;
double third;
double fourth;
double total_resist;
double black, brown, red = 2, orange, yellow, green, blue, violet, gray, white;

black=0;
brown=1;
red = 2;
orange = 3;
yellow = 4;
green = 5;
blue = 6;
violet = 7;
gray = 8;
white = 9;

char[10] buf;
printf("Input first color band> ");
scanf("%s", buf);
double first=color2nr(buf);

printf("Input second color band> ");
scanf("%s", buf);
double second=color2nr(buf);


total_resist = first + second;// + second + third + fourth;


printf("\nTotal resist is %.lf\n", total_resist);



return(0);
}
于 2013-04-08T10:21:22.680 回答
0
1) try using **enum**  

例如:
typedef enum {black,brown,red...}colorcodes;

像这样声明变量..

colorcodes FstColor, SndColor;

2) `scanf("%s\n",FstColorStr)` and not `scanf("%lf",bSndColorStr);`

3)  if(strcmp(FstColorStr,"red")) FstColor = red and so on for all other colors.

4) tot_resist = FstColor * pow(10,SndColor); //include math.h
于 2013-04-08T10:45:54.447 回答