Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我为电阻计算器编写了此代码,并且不断收到语法错误,如(11db)或输出值为0。这里是6,所以也许我忽略了某些内容。我想我也应该提到我正在使用xCode。

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

#define MAX_COLOR_STR_LEN 10

int valueFormColor(char const * const color)
{

    if(strcmp("black", color)==0)
        return 0;
    if(strcmp("brown", color)==0)
        return 1;
    if(strcmp("red", color)==0)
        return 2;
    if(strcmp("orange", color)==0)
        return 3;
    if(strcmp("yellow", color)==0)
        return 4;
    if(strcmp("green", color)==0)
        return 5;
    if(strcmp("blue", color)==0)
        return 6;
    if(strcmp("violet", color)==0)
        return 7;
    if(strcmp("gray", color)==0)
        return 8;
    if(strcmp("white", color)==0)
        return 9;
    return 0;
}

int mult(char const * const multiplier)

{
    if(strcmp("black", multiplier)==0)
        return 1;
    if(strcmp("brown", multiplier)==0)
        return 10;
    if(strcmp("red", multiplier)==0)
        return 100;
    if(strcmp("orange", multiplier)==0)
        return 1000;
    if(strcmp("yellow", multiplier)==0)
        return 10000;
    if(strcmp("green", multiplier)==0)
        return 100000;
    if(strcmp("blue", multiplier)==0)
        return 1000000;
    if(strcmp("violet", multiplier)==0)
        return 10000000;
    if(strcmp("gold", multiplier)==0)
        return 0.1;
    if(strcmp("silver", multiplier)==0)
        return 0.01;
    return 0;

}

int tol(char const * const tolerance)

{
    if(strcmp("brown", tolerance)==0)
        return 1;
    if(strcmp("red", tolerance)==0)
        return 2;
    if(strcmp("green", tolerance)==0)
        return 0.5;
    if(strcmp("blue", tolerance)==0)
        return 0.25;
    if(strcmp("violet", tolerance)==0)
        return 0.10;
    if(strcmp("gray", tolerance)==0)
        return 0.05;
    if(strcmp("gold", tolerance)==0)
        return 5;
    if(strcmp("silver", tolerance)==0)
        return 10;
    return 0;
}

int main()

{
    char bands;
    char color1[MAX_COLOR_STR_LEN];
    char color2[MAX_COLOR_STR_LEN];
    char color3[MAX_COLOR_STR_LEN];
    char color4[MAX_COLOR_STR_LEN];
    char color5[MAX_COLOR_STR_LEN];
    char color6[MAX_COLOR_STR_LEN];
    float value;


    printf("Select how many bands the resistor has");
    printf("\nA. 4 bands");
    printf("\nB. 5 bands");
    printf("\nC. 6 bands\n");
    scanf("%c", &bands);

switch(bands)

    {

case 'a':
case 'A':
    printf("Enter the colors in the proper order\n");
    scanf("%s %s %s %s", color1, color2, color3, color4);

    value = (10 * valueFormColor(color1) + valueFormColor(color2)) * mult(color3);
    printf("The total resistance is %.2f with %d%% tolerance\n", value, tol(color4));
    break;

case 'b':
case 'B':
    printf("Enter the colors in the proper order\n");
    scanf("%s %s %s %s %s", color1, color2, color3, color4, color5);

    value = (100 * valueFormColor(color1) + 10 * valueFormColor(color2) + valueFormColor(color3)) * mult(color4);
    printf("The total resistance is %.2f with %d%% tolerance\n", value, tol(color5));
    break;

case 'c':
case 'C':
    printf("Enter the colors in the proper order\n");
    scanf("%s %s %s %s %s %s", color1, color2, color3, color4, color5, color6);

    value = (1000 * valueFormColor(color1) + 100 * valueFormColor(color2) + 10 * valueFormColor(color3) + valueFormColor(color4)) * mult(color5);
    printf("The total resistance is %.2f with %d%% tolerance\n", value, tol(color6));
    break;

    }

    getchar();
    return 0;
}

最佳答案



int tol(...)
   ...
   return 0.1


int mult (...)
   ...
   return 0.01


您从编译器向上/向下取整的函数返回const float-在您的情况下,导致某些情况下的返回值给出乘数0



当我阅读评论时,@ BLUEPIXY已注意到这一点

关于c - (11db)并输出0语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22892822/

10-11 21:25