计算电阻值的色带并输出

时间:2016-05-23 04:06:14

标签: c

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.output必须分开{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

真的很感激,如果有人可以提供帮助

1 个答案:

答案 0 :(得分:0)

  1. 您的color_code功能设计不当。您应该将颜色字符串作为输入并返回代码字符串。
  2. 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;
    }
    
    1. 在主要内容中,您需要在scanf的
    2. 之后调用此color_code函数

      codeOne = color_code(colorOne);
      
      1. 您还需要为其他三个功能重复此操作。

      2. 变量colorOne colorTwo colorthree codeOne codeTwo codeThree可以在本地生成,而不是全局。< / p>