basic C code programming
#include <stdio.h> #include <stdlib.h> #include <math.h> int col_to_num(char color, int choice) { //decodes color if(color == 'B') { //this is according to question if(choice == 1) return 0; if(choice == 2) return 1; if(choice == 3) return 6; } if(color == 'G') { //this is according to question if(choice == 1) return 5; if(choice == 2) return 8; if(choice == 3) return 10; } if(color == 'R') //for remaining questions, only first character is needed return 2; if(color == 'O') return 3; if(color == 'Y') return 4; if(color == 'V') return 7; if(color == 'W') return 9; if(color == 'S') return 11; } int main() { FILE *fp; //declare file pointers FILE* outfp; fp = fopen("resistorcolor.txt", "r"); //open input file outfp = fopen("resistorvalue.txt", "w"); //open output file if(fp == NULL || outfp == NULL) { //if cannot open file, abort printf("Cannot open file\n"); return 0; } int val1, val2, val3; //declare variables char col1, col2, col3; while(fscanf(fp, "%c %d %c %d %c %d\n", &col1, &val1, &col2, &val2, &col3, &val3) != EOF) { //fscanf returns number of variables read //while we are able to read 6 characters in each line int d1 = col_to_num(col1, val1); //get mapping for each color and choice int d2 = col_to_num(col2, val2); float d3 = col_to_num(col3, val3); //since d3 can also have gold and silver, we need to convert them tocoressponding values //since col_to_num cannot return double, we need to do it h re if(d3 == 10) { //if color is gold d3 = 0.1; } else if(d3 == 11) { //if color is silver d3 = 0.01; } double ans = (d1*10 + d2)*pow(10, d3); fprintf(outfp, "%lf\n", ans); } return 0; }