โปรแกรมคำนวณหาค่าตัวต้านทานจากโค้ดสี

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น

Moderator:

User avatar
nokroo
Posts: 188
Joined: 01/01/1970 7:00 am
Location: วัดตะคร้ำเอน ต.ตะคร้ำเอน อ.ท่ามะกา จ.กาญจนบุรี

โปรแกรมคำนวณหาค่าตัวต้านทานจากโค้ดสี

Postby nokroo » 05/10/2013 8:00 am

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <iostream>
#include <cctype>

using namespace std;

void getColorCode(char colorCode[]);
long getResistorValue(char colorCode[]);

char colorCode[3];

int main (void) {
long r_value = 0;
printf("This program will calculate the value of a resistor.\n\n");
printf("B = Black\nN = Brown\nR= Red\nO = Orange\nY = Yellow\n");
printf("G = Green\nL = Blue\nV = Violet\nE = Gray\nW = White\n\n");
printf("Enter the 4-character color description of the resistor: ");
getColorCode(colorCode);

r_value = getResistorValue(colorCode);
printf("\n\nThe resistor with color code %c %c %c has a value of %d Ohms.\n", colorCode[0], colorCode[1], colorCode[2], r_value);
getch();
}

void getColorCode(char colorCode[]) {
int i;
for(i = 0; i < 4; i++)
colorCode[i] = getchar();
}

long getResistorValue( char colorCode[] ) {

int i;
double multiplier;
char band_color;
long value;
int colorValue[3];

for(i = 0; i < 3; i++) {
band_color = toupper(colorCode[i]);
switch (band_color) {
case('B'):
colorValue[i] = 0;
break;
case('N'):
colorValue[i] = 1;
break;
case('R'):
colorValue[i] = 2;
break;
case('O'):
colorValue[i] = 3;
break;
case('Y'):
colorValue[i] = 4;
break;
case('G'):
colorValue[i] = 5;
break;
case('L'):
colorValue[i] = 6;
break;
case('V'):
colorValue[i] = 7;
break;
case('E'):
colorValue[i] = 8;
break;
case('W'):
colorValue[i] = 9;
break;
default:
printf("\nERROR: Color Code %c is not a valid choice\n", colorCode[i]);
break;
}
}
multiplier = pow( 10, (double) colorValue[2]);
value = (((colorValue[0]*10) + colorValue[1]) * multiplier);
return value;
}


ที่มา : http://th.answers.yahoo.com/question/in ... 630AAzTcOn


ผลรันโปรแกรม

Image

Return to “Programming - C/C<sup>++</sup>& java”

Who is online

Users browsing this forum: No registered users and 2 guests