在 C 中按颜色解码电阻的程序的问题

Problems with a program that decodes resistance by color in C

我在这方面完全遇到了障碍,我对指针和文件处理 atm 和 uni 不是很擅长。走得太快,我跟不上...

文件的预期输入是数组中的任何颜色:
input.txt
红、绿、蓝
黑色、白色、灰色
...

然后输出到文件:
output.txt
红、绿、蓝
电阻(欧姆):7000000.000000

黑色、白色、灰色
电阻(欧姆):1000000.000000
...

我现在的输出:
红、绿、蓝
电阻(欧姆):0.000000

黑色、白色、灰色
电阻(欧姆):0.000000

到目前为止,这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define size 100

double DecodeString(char *inputString){
  const char *kColorTable[10] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
  int i;

  for(i=0; i<10; i++){
    if(strcmp(inputString, kColorTable[i]) == 0){
      return (double)i;
    }
  }
  return -999.0;
}

int main(){
  char color[size], *token, *inputString;
  double resistance, color1, color2, color3;
  double value;


  FILE *fptrin, *fptrout;
  if(((fptrin = fopen("input.txt", "r"))==NULL) || ((fptrout = fopen("output.txt", "w")) == NULL)){
    printf("Error 404: File not found");
    exit(1);
  }

  while(fgets(color, size, fptrin)){
    token = strtok(color, ",");
    DecodeString(token);
    puts("");
    while(token != NULL){
      value = DecodeString(token);
      printf("%s",token);
      token = strtok(NULL, ",");
    }
  }
  //value = DecodeString(color);

  if (color1 == -999.0 || color2 == -999.0 || color3 == -999.0){
    printf("\n\nBad code -- cannot compute resistance\n");
  }

  else{
    resistance = (10.0 * color1 + color2) *pow(10.0, color3);

    if(resistance > 1000){
      printf("\n\nResistance in Kilo-ohms: %f\n",resistance);
    }
    else{
      printf( "\n\nResistance in ohms: %f\n",resistance);
    }
  }

  fclose(fptrin);
  fclose(fptrout);

  getchar();
  return 0;
}

我卡在了 strtok 循环中,我不完全确定如何将这些值传递给函数并将函数中的值分配给 color1color2color3 用于计算欧姆。

我不明白当我将 token 传递给 DecodeString 时,它只是 returns 第一个字符串的值而不是每个单独的字符串(也许我缺少一个 for循环)

我有另一个代码,我的代码基于某人的函数:

#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<math.h>
#define size 100

double DecodeString(char *inputString)
{
     const char *kColorTable[11] =
     {"", "black", "brown", "red", "orange", "yellow",
      "green", "blue", "violet", "gray", "white"};
     int i;

     for(i=0; i<11; i++)
     {
         if(strcmp(inputString, kColorTable[i]) == 0)
         {
             return (double)i;
         }
     }
     return -999.0;

}

int main(void){
  char color[size], *token, *inputString;
  double value;

  FILE *fptrin, *fptrout;
  if(((fptrin = fopen("input.txt", "r")) == NULL) || ((fptrout = fopen("output.txt", "w")) == NULL)){
    printf("Error 404: File not found");
    exit(1);
  }

  while(fgets(color, size, fptrin)){
    token = strtok(color, ",");
    DecodeString(token);
    puts("");
    while(token != NULL){
      value = DecodeString(token);
      printf("%s",token);
      token = strtok(NULL, ",");
    }
    //value = DecodeString(token);
    printf("%f\n",value + value) *pow(10.0, value);
  }
    return 0;
}

我遇到了严重的障碍,希望能提供一些解释和示例以更好地理解正在发生的事情。

以下逻辑适合您,

1.Declare双数组

double colord[3];

2.Now修改while循环,

while(fgets(color, size, fptrin)){
    token = strtok(color, ",");
    colord[i] = DecodeString(token);
    puts("");
    while(token != NULL){
        i++;
        colord[i] = DecodeString(token);
        printf("%s",token);
        token = strtok(NULL, ",");
    }
}
if (colord[0] == -999.0 || colord[1] == -999.0 || colord[2] == -999.0){
    printf("\n\nBad code -- cannot compute resistance\n");
}
else{
    resistance = (10.0 * colord[0] + colord[1]) * pow(10.0, colord[2]);