I'm trying to develop a C program that calculates the resistor values by inputting the colour bands marked on the resistor.
忽略电阻公差这是我的代码 输出必须类似于 Calculating the resistor value with its color bands as input
我下面的代码没有给我想要的东西。我希望它能够循环返回 y 和 no。输出必须分开 {3}{3}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int bandChoice;
char colorOne[10];
char colorTwo[10];
char colorThree[10];
float codeOne;
float codeTwo;
float codeThree;
float x;
void colorCodes();
void multiplier();
void color_code(char codeOne);
int main(void)
{ printf("Enter the colors of the resistor three bands, beginning with the band nearest the end. Type the colors in lowercase letters only, NO CAPS\n");
puts(" ");
printf("Band 1 = >");
scanf("%s",colorOne);
puts(" ");
printf("Band 2 = >");
scanf("%s",colorTwo);
puts(" ");
printf("Band 3 = >");
scanf("%s",colorThree);
puts(" ");
printf("%f %f %f\n",colorOne,colorTwo,colorThree);
x=(codeOne*10)+codeTwo;
printf("Resistance value %d");
}
void color_code(char codeOne )
{
if(strcmp(colorOne, "black") == 0)
{
codeOne=0;
}
else
if(strcmp(colorOne, "brown") == 0)
{
codeOne=1;
}
else
if(strcmp(colorOne, "red") == 0)
{
codeOne=2;
}
else
if (strcmp(colorOne, "orange") == 0)
{
codeOne=3;
}
else
if (strcmp(colorOne, "yellow") == 0)
{
codeOne=4;
}
else
if (strcmp(colorOne, "green") == 0)
{enter code here
codeOne=5;
} else
if (strcmp(colorOne, "blue") == 0)
{
codeOne=6;
} else
if (strcmp(colorOne, "violet") == 0)
{
codeOne=7;
} else
if (strcmp(colorOne, "gray") == 0)
{
codeOne=8;
} else
if (strcmp(colorOne, "white") == 0)
{
codeOne=9;
} else
{
printf("Invalid colors\n");
}
}
void multiplier()
{
if(strcmp(colorThree, "black") == 0)
{
codeThree=1;
} else
if(strcmp(colorThree, "brown") == 0)
{
codeThree=10;
} else
if(strcmp(colorThree, "red") == 0)
{
codeThree=pow(10.0,2);
} else
if (strcmp(colorThree, "orange") == 0)
{
codeThree=pow(10.0,3);
} else
if (strcmp(colorThree, "yellow") == 0)
{
codeThree=pow(10.0,4);
} else
if (strcmp(colorThree, "green") == 0)
{
codeThree=pow(10.0,5);
} else
if (strcmp(colorThree, "blue") == 0)
{
codeThree=pow(10.0,6);
} else
if (strcmp(colorThree, "violet") == 0)
{
codeThree=pow(10.0,7);
} else
if (strcmp(colorThree, "gray") == 0)
{
codeThree=pow(10.0,8);
} else
if (strcmp(colorThree, "white") == 0)
{
codeThree=pow(10.0,9);
} else
{
printf("Invalid colors\n");
}
}
http://imgur.com/rya9egk
如果有人能提供帮助,我将不胜感激
- 您的
color_code
函数设计不当。您应该将颜色字符串作为输入并返回代码字符串。
.
char color_code(char *colorOne )
{
char codeOne;
if(strcmp(colorOne, "black") == 0)
{
codeOne=0;
}
// and so on for other colors
// at the end
return codeOne;
}
- 主要是,您需要在 scanf 之后调用此
color_code
函数
.
codeOne = color_code(colorOne);
您还需要对其他三个函数重复此操作。
变量 colorOne
colorTwo
colorthree
codeOne
codeTwo
codeThree
可以在 main 本地,而不是全局。
我是一名优秀的程序员,十分优秀!