我为电阻计算器制作了这个代码,我不断收到像(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;
}
答案 0 :(得分:2)
在
int tol(...)
...
return 0.1
int mult (...)
...
return 0.01
从编译器向上/向下舍入的函数返回const float
- 在您的情况下,导致某些情况的返回值为0
的乘数。
在我阅读评论时,@ BLUEPIXY
已经注意到了这一点