Obviously I am new to programming, and I realize that there is most likely better ways to go about what I did here. Seeking advice.
Code:
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<ctype.h>
void intro (void);
double getCode(char code);
double engVal(double );
void cont(void);
int main (void)
{
char c1,c2,c3;
double num1, num2, num3, ohm;
intro ();
printf("\n\n\tEnter your three band code\t=> ");
scanf("%c %c %c", &c1, &c2, &c3);
num1 = getCode(c1); /* Runs the 3 user inputs (c1, c2, c3) through the */
num2 = getCode(c2); /* function "getCode(char code);" and brings back*/
num3 = getCode(c3); /* an interger value. */
_clrscr();
if ( num1 == -999.0 || num2 == -999.0 || num3 == -999.0 ) /* Checks that the number is valid. */
printf( "\n\n\tBad code -- cannot compute resistance\n" );
else {
ohm = ( 10 * num1 + num2 ) * pow(10, num3);}
/////////////////////////////////////////////////////////////////////////////////////////////////////////// Part of main that provides
c1 = toupper(c1);c2 = toupper(c2);c3 = toupper(c3); //// the user the code that they typed
printf("\n\n\t\t *********"); //// in upper case (easier on the eyes)
printf("\n\tColor code * %c%c%c *",c1,c2,c3); //// along with the value that the resistor
printf("\n\t\t *********\n\t\t\t"); //// code returned in engineering notation.
ohm = engVal(ohm); ////
printf(" ohms\n\n\n"); ////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
cont();
return (0);
} /* An introduction function giving direction for the user input*/
void intro (void){
printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
printf( "\t COLOR\t\t CODE\n\t" );
printf( "\t---------\t----------\n" );
printf( "\t\t| Black |\t| B or b |\n" );
printf( "\t\t| Brown |\t| N or n |\n" );
printf( "\t\t| Red |\t| R or r |\n" );
printf( "\t\t|Orange |\t| O or o |\n" );
printf( "\t\t|Yellow |\t| Y or y |\n" );
printf( "\t\t| Green |\t| G or g |\n" );
printf( "\t\t| Blue |\t| E or e |\n" );
printf( "\t\t|Violet |\t| V or v |\n" );
printf( "\t\t| Gray |\t| A or a |\n" );
printf( "\t\t| White |\t| W or w |\n" );
printf( "\t\t---------\t----------\n" );
} /* Function to convert the input color code to a numeric value*/
double getCode(char code){
switch (code ) {
case 'B': case'b': return 0.0;
case 'N': case'n': return 1.0;
case 'R': case'r': return 2.0;
case 'O': case'o': return 3.0;
case 'Y': case'y': return 4.0;
case 'G': case'g': return 5.0;
case 'E': case'e': return 6.0;
case 'V': case'v': return 7.0;
case 'A': case'a': return 8.0;
case 'W': case'w': return 9.0;
default:
return 0;
}
} /* Function to convert resistor value to engineering notation*/
double engVal(double val){
if (val >= 1.0&&val < 1.0e3){
(val); printf("= :\t%.1f ", val);}
if (val >= 1e3&& val < 1e6){
(val = val / 1e3); printf("\t= %.1f k", val);}
if (val >=1e6&&val<1e9){
(val = val / 1e6 ); printf("\t= %.1f M", val);}
if(val >=1e9&&val<1e12){
(val = val / 1e9); printf("\t= %.1f G", val);}
if(val >=1e12&&val<1e15){
(val = val / 1e12); printf("\t= %.1f T", val);}
if(val < 0.9&&val>=1e-3){
(val = val *1e3); printf("\t= %.1f m", val);}
if (val < 1e-3&&val>=1e-6){
(val = val *1e6 ); printf("\t= %.1f u", val);}
if (val < 1e-6&&val>=1e-9){
(val = val *1e9); printf("\t= %.1f p", val);}
if (val < 1e-9&&val>=1e-12){
(val = val *1e12); printf("\t= %.1f p", val);}
return(0);
} /* Continue function, gives the option to abort or enter another code*/
void cont(void){
int another;
printf("\n\tEnter another code?\n\n\t");
printf("Type 1 to continue,\n\t");
printf(" or any key to abort\n\n\t");
another = 0;
scanf("%d",&another);
_clrscr();
if ( another == 1)
{ fflush ( stdin ) ;
main();
}else if ( another !=1 );
exit(1);
}
Also am wanting to make it more universal, possibly accept 4,5,6 band codes as well.
Just having fun with it, its not a class project or anything like that but still would appreciate
any input.
Thanks!