我有以下程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void print_codes( void ); /* menu of codes */
double decode_char( char code );
main() {
char code1, code2, code3; /* one code per band */
double resistance;
double color1, color2, color3; /* decoded values */
/* Print codes and prompt for user input. */
print_codes();
printf( "\n\n\tEnter three codes. " );
/* Read three character codes. */
code1 = getchar();
code2 = getchar();
code3 = getchar();
/* Decode each character code. */
color1 = decode_char( code1 );
color2 = decode_char( code2 );
color3 = decode_char( code3 );
/* Check whether codes were legal. */
if ( color1 == -999.0 || color2 == -999.0 || color3 == -999.0 )
printf( "\n\n\tBad code -- cannot compute resistance\n" );
/* If codes were legal, compute and print resistance in ohms. */
else {
resistance = ( 10.0 * color1 + color2 ) * pow( 10.0, color3 );
printf( "\n\n\tResistance in ohms:\t%f\n", resistance );
}
return;
}
/* This function prints a menu of color codes to guide the user in
entering input. */
void print_codes( void ) {
printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
printf( "COLOR\t\t\tCODE\n\t" );
printf( "-----\t\t\t----\n\n" );
printf( "\tBlack-------------------> B\n" );
printf( "\tBrown-------------------> N\n" );
printf( "\tRed---------------------> R\n" );
printf( "\tOrange------------------> O\n" );
printf( "\tYellow------------------> Y\n" );
printf( "\tGreen-------------------> G\n" );
printf( "\tBlue--------------------> E\n" );
printf( "\tViolet------------------> V\n" );
printf( "\tGray--------------------> A\n" );
printf( "\tWhite-------------------> W\n" );
}
double decode_char( char code ) {
if (code == 0.0) {
return 'B';
}
else if (code == 1.0) {
return 'N';
}
else if (code == 'R') {
return 2.0;
}
else if (code == 'O') {
return 3.0;
}
else if (code == 'Y') {
return 4.0;
}
else if (code == 'G') {
return 5.0;
}
else if (code == 'E') {
return 6.0;
}
else if (code == 'V') {
return 7.0;
}
else if (code == 'A') {
return 8.0;
}
else if (code == 'W') {
return 9.0;
}
else {
return -990.0;
}
}
这是一个简单的程序,可根据以下颜色代码计算欧姆:
E.G。输入YVB会给我470欧姆。
我一直试图进行反向输出,用户可以输入欧姆并获得彩色输出。例如,如果用户输入470欧姆,他们就会得到这个:
我很难实现这一点,并想知道从哪里开始。我已经尝试过将char更改为double并切换值,但这根本不起作用。 同样是的,我知道我应该使用switch语句而不是if语句,但这不是我现在的问题。
答案 0 :(得分:0)
您可以使用mod运算符%
来查找数字并将其与颜色匹配。然后在每个步骤之后将ohm
除以10以获得下一个数字。例如:
int ohm = 470;
char map[11] = "BNROYGEVAW";
char color[10];
for (int i = 0; i < 10; i++)
color[i] = 0;
for (int i = 0; i < 10; i++)
{
int n = ohm % 10;
color[i] = map[n];
ohm /= 10;
if (ohm == 0)
break;
}
for (int i = 9; i >= 0; i--)
if (color[i])
printf("%c", color[i]);
答案 1 :(得分:0)
也许是这样的:
#include <stdio.h> // printf(), fprintf()
#include <errno.h> // EINVAL
#include <stdlib.h> // strtoull()
#define SUCCESS 0
/** ***********************************************************************
* Convert command-line string of resistor colors to ohms.
*/
int main(
int I__argC,
char *I__argV[]
)
{
int rCode = SUCCESS;
unsigned long long ohms;
unsigned long long ohmsTotal;
int power = 0;
int value10s;
int value1s;
char *color[] = {"Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Gray", "White"};
unsigned long long ohmsCheck;
int powerCheck = 0;
printf("\n");
if(I__argC < 2)
{
rCode=EINVAL;
fprintf(stderr, "Missing numeric resistor value parameter.\n");
printf("USAGE: %s {numeric resistor value in ohms}\n", I__argV[0]);
goto CLEANUP;
}
ohmsTotal = ohms = strtoull(I__argV[1], NULL, 10);
while(ohms > 99)
{
++power;
ohms /= 10;
}
value10s = ohms / 10;
value1s = ohms - (value10s * 10);
ohmsCheck = (value10s * 10) + value1s;
if(power)
for(powerCheck = 0; powerCheck < power; ++powerCheck)
ohmsCheck *= 10;
if(ohmsCheck != ohmsTotal)
{
fprintf(stderr, "Invalid resistor ohms value[%llu]. (Perhaps you intended: %llu ?)\n", ohmsTotal, ohmsCheck);
goto CLEANUP;
}
//RESULTS:
printf("%llu Ohms = %s %s %s\n\n", ohmsTotal, color[value10s], color[value1s], color[power]);
CLEANUP:
return(rCode);
}
BONUS :(我的颜色版本为欧姆)
#include <stdio.h> // printf(), fprintf()
#include <errno.h> // EINVAL, ERANGE
#include <string.h> // strerror()
#include <ctype.h> // toupper()
#define SUCCESS 0
/** ***********************************************************************
* Convert a resistor color to an integer value.
*/
int ResistorColorToInteger(
char I__color,
int *_O_integer
)
{
int rCode = SUCCESS;
char *colorsIndex = "BNROYGEVAW";
char *cp;
cp=strchr(colorsIndex, toupper(I__color));
if(!cp)
{
rCode=ERANGE;
goto CLEANUP;
}
//RESULTS:
if(_O_integer)
*_O_integer = cp - colorsIndex;
CLEANUP:
return(rCode);
}
/** ***********************************************************************
* Convert a string of three resistor colors to ohms.
*/
int ResistorColorsToOhms(
const char *I__colors,
unsigned long long *_O_ohms
)
{
int rCode = SUCCESS;
int value10s;
int value1s;
int valuePower;
unsigned long long power = 1;
if(3 != strlen(I__colors))
{
rCode=EINVAL;
goto CLEANUP;
}
rCode=ResistorColorToInteger(I__colors[0], &value10s);
if(rCode)
goto CLEANUP;
rCode=ResistorColorToInteger(I__colors[1], &value1s);
if(rCode)
goto CLEANUP;
rCode=ResistorColorToInteger(I__colors[2], &valuePower);
if(rCode)
goto CLEANUP;
while(valuePower--)
power *= 10;
//RESULTS:
if(_O_ohms)
*_O_ohms = ((value10s * 10) + value1s) * power;
CLEANUP:
return(rCode);
}
/** ***********************************************************************
* Display program help screen.
*/
int Usage(
const char *I__programName
)
{
int rCode=SUCCESS;
printf("USAGE: %s {3-color resister code}\n", I__programName);
printf("Color code key:\n");
printf( "\tBlack-------------------> B\n" );
printf( "\tBrown-------------------> N\n" );
printf( "\tRed---------------------> R\n" );
printf( "\tOrange------------------> O\n" );
printf( "\tYellow------------------> Y\n" );
printf( "\tGreen-------------------> G\n" );
printf( "\tBlue--------------------> E\n" );
printf( "\tViolet------------------> V\n" );
printf( "\tGray--------------------> A\n" );
printf( "\tWhite-------------------> W\n" );
printf("\n");
return(rCode);
}
/** ***********************************************************************
* Convert command-line string of resistor colors to ohms.
*/
int main(
int I__argC,
char *I__argV[]
)
{
int rCode = SUCCESS;
unsigned long long ohms;
printf("\n");
if(I__argC < 2)
{
rCode=EINVAL;
fprintf(stderr, "Missing 3-color code parameter.\n");
Usage(I__argV[0]);
goto CLEANUP;
}
rCode=ResistorColorsToOhms(I__argV[1], &ohms);
switch(rCode)
{
case SUCCESS:
break;
case EINVAL:
fprintf(stderr, "ResistorColorsToOhms() reports that \"%s\" an invalid color code sequence.\n", I__argV[1]);
Usage(I__argV[0]);
goto CLEANUP;
case ERANGE:
fprintf(stderr, "ResistorColorsToOhms() reports that the sequence \"%s\" contains a non-key character.\n", I__argV[1]);
Usage(I__argV[0]);
goto CLEANUP;
default:
fprintf(stderr, "ResistorColorsToOhms() reports: [%d]%s (Something bad happened)\n", rCode, strerror(rCode));
goto CLEANUP;
}
//RESULTS:
printf("\"%s\" = %llu Ohms\n", I__argV[1], (unsigned long long)ohms);
CLEANUP:
return(rCode);
}