Public feed #446517

shibby

public unlisted text v1 · immutable
Raw Edit
·published 2007-04-18 15:21 UTC
rendered paste body
#include <ctype.h>
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
 
 
int	comp;	
 
#define DEBUG_ON   1
 
//Functions
int     fncSelectionInput();
int     fncHandInput();
int     fncFileInput();
int     fncGetBand(int intRes);
int		fncWorkout();
int		fncTempLoad();
int		fncBandLoad();
 
//Global Vars
int 	counter;
int     intSelectionInput;
int     intResSeelction;
int     intResBand;
int     intRVValue = 0;


//File load up parts
int	ResisA[22];
int	ResisB;
int	TempsA[22];
int	TempsB;

//bands load up parts
FILE *FileBandsLocation = NULL;
int maxBandLines = 7;
int maxBandStringSize = 6;
char arrBandsFile[7][6];

int	intLocation;
int intDiff;
 
//Arrays
char    *arrRes1[3];
char    *arrRes2[3];
 
//consts for setting arrays
char *BLACK = "black";
char *BROWN = "brown";
char *RED = "red";
char *ORANGE = "orange";
char *YELLOW = "yellow";
char *GREEN = "green";
char *BLUE = "blue";
char *VIOLET = "violet";
char *GRAY = "gray";
char *WHITE = "white";
char *GOLD = "gold";
char *SILVER = "silver";
 
int main(void)
{		
		fncTempLoad();
        fncSelectionInput();
        return 0; 
}
 
int fncSelectionInput(void)
{
        printf("\nWhat would you like to do?\n");
        printf("\n1\tInput by hand\n");
        printf("\n2\tGrab from file\n");
        printf("\n3\tWorkout\n");
        printf("\n4\tExit\n");
        printf("\n\nSelect your choice (1-4):");
        scanf("%d", &intSelectionInput);
        
        if (intSelectionInput >= 1 && intSelectionInput <= 4 ) 
        {
                        switch (intSelectionInput)
                        {
                                case 1:
                                        system("cls");
                                        fncHandInput();
                                        break;
                                                
                                case 2:
                                        system("cls");
                                        fncFileInput();
                                        break;
                                case 3:
                                        fncWorkout();
                                        break;
                                case 4:
                                        
                                        exit(0);
                                        break;     
                        }
        }
else
        {
                system("cls");
                printf("\nWrong!!! Please select again\n");
                fncSelectionInput();    
        }
        
}
 
int     fncHandInput()
{
        printf("\nWhat would you like to do?\n");
        printf("\n1\tInput resistor 1\n");
        printf("\n2\tInput resistor 2\n");
        printf("\n3\tInput variable resistor\n");
        printf("\n4\tReturn to menu\n");
        
        printf("\n\nSelect your choice (1-4):");
        scanf("%d", &intResSeelction);
        
        if (intResSeelction >= 1 && intResSeelction <= 4 ) 
        {
                        switch (intResSeelction)
                        {
                                case 1:
                                        system("cls");
                                        fncGetBand(intResSeelction);
                                        break;
                                                
                                case 2:
                                        system("cls");
                                        fncGetBand(intResSeelction);
                                        break;
                                case 3:
                                        system("cls");
                                        printf("\nPlease enter in your variable resistor value:");
                                        scanf("%d", &intRVValue);
                                        fncSelectionInput();
                                        break;     
                                case 4:
                                        system("cls");
                                        fncSelectionInput();
                                        break;     
              
                        }
        }
else
        {
                system("cls");
                printf("\nWrong!!! Please select again\n");
                fncSelectionInput();    
        }
 
 
}
 

 
int fncGetBand(int intRes)
{
                
        printf("\nPlease enter in your bands\n");
 
        for ( counter = 1; counter < 4; counter++ )
        {
                        printf("\nPlease select the colour band %d\n", counter);
                        printf("\n1\tBlack\n");
                        printf("2\tBrown\n");
                        printf("3\tRed\n");
                        printf("4\tOrange\n");
                        printf("5\tYellow\n");
                        printf("6\tGreen\n");
                        printf("7\tBlue\n");
                        printf("8\tViolet\n");
                        printf("9\tGray\n");
                        printf("10\tWhite\n");
                        printf("11\tGold\n");
                        printf("12\tSilver\n");     
                                                                      
                        printf("\n\nSelect your choice (1-12):");
                        scanf("%d", &intResBand);
                        
                        
        if (intRes == 1)
        {          
                        if (intResBand >= 1 && intResBand <= 12 ) 
                        {
                                switch (intResBand)
                                {
                                        case 1:
                                                system("cls");
                                                arrRes1[counter] = BLACK;
                                                break;
                                                        
                                        case 2:
                                                system("cls");
                                                arrRes1[counter] = BROWN;
                                                break;
                                        case 3:
                                                system("cls");
                                                arrRes1[counter] = RED;
                                                break;
                                        case 4:
                                                system("cls");
                                                arrRes1[counter] = ORANGE;
                                                break;
                                        case 5:
                                                system("cls");
                                                arrRes1[counter] = YELLOW;
                                                break;
                                        case 6:
                                                system("cls");
                                                arrRes1[counter] = GREEN;
                                                break;
                                        case 7:
                                                system("cls");
                                                arrRes1[counter] = BLUE;
                                                break;
                                        case 8:
                                                system("cls");
                                                arrRes1[counter] = VIOLET;
                                                break;
                                        case 9:
                                                system("cls");
                                                arrRes1[counter] = GRAY;
                                                break;
                                        case 10:
                                                system("cls");
                                                arrRes1[counter] = WHITE;
                                                break;
                                        case 11:
                                                system("cls");
                                                arrRes1[counter] = GOLD;
                                                break;
                                        case 12:
                                                system("cls");
                                                arrRes1[counter] = SILVER;
                                                break;
                                } //end switch
                                
        } //end if
else
        {
                system("cls");
                printf("\nWrong!!! Please select again\n");
                fncGetBand(intResSeelction);          
        }       
} //end if for if intRes = 1
else
{
if (intResBand >= 1 && intResBand <= 12 ) 
                        {
                                switch (intResBand)
                                {
                                        case 1:
                                                system("cls");
                                                arrRes2[counter] = BLACK;
                                                break;
                                                        
                                        case 2:
                                                system("cls");
                                                arrRes2[counter] = BROWN;
                                                break;
                                        case 3:
                                                system("cls");
                                                arrRes2[counter] = RED;
                                                break;
                                        case 4:
                                                system("cls");
                                                arrRes2[counter] = ORANGE;
                                                break;
                                        case 5:
                                                system("cls");
                                                arrRes2[counter] = YELLOW;
                                                break;
                                        case 6:
                                                system("cls");
                                                arrRes2[counter] = GREEN;
                                                break;
                                        case 7:
                                                system("cls");
                                                arrRes2[counter] = BLUE;
                                                break;
                                        case 8:
                                                system("cls");
                                                arrRes2[counter] = VIOLET;
                                                break;
                                        case 9:
                                                system("cls");
                                                arrRes2[counter] = GRAY;
                                                break;
                                        case 10:
                                                system("cls");
                                                arrRes2[counter] = WHITE;
                                                break;
                                        case 11:
                                                system("cls");
                                                arrRes2[counter] = GOLD;
                                                break;
                                        case 12:
                                                system("cls");
                                                arrRes2[counter] = SILVER;
                                                break;
                                } //end switch
                                
        } //end if
else
        {
                system("cls");
                printf("\nWrong!!! Please select again\n");
                fncGetBand(intResSeelction);          
        }       
        
}//end of if int res = 2
           
 
} //end for
 
fncHandInput();
}
 


int	fncWorkout(void)
{

int	R1B1, R1B2, R2B1, R2B2;
int	R1P1, R1P2;
int	R2P1, R2P2;
int	intStep1;
int	intFinalWork;
	
	system("cls");
	if (arrRes1[1] == BLACK)
	R1B1 = 0 * 10;
	if (arrRes1[1] == BROWN)
	R1B1 = 1 * 10;
	if (arrRes1[1] == RED)
	R1B1 = 2 * 10;
	if (arrRes1[1] == ORANGE)
	R1B1 = 3 * 10;
	if (arrRes1[1] == YELLOW)
	R1B1 = 4 * 10;	
	if (arrRes1[1] == GREEN)
	R1B1 = 5 * 10;
	if (arrRes1[1] == BLUE)
	R1B1 = 6 * 10;
	if (arrRes1[1] == VIOLET)
	R1B1 = 7 * 10;
	if (arrRes1[1] == GRAY)
	R1B1 = 8 * 10 ;
	if (arrRes1[1] == WHITE)
	R1B1 = 9 * 10;
	if (arrRes1[1] == GOLD)
	R1B1 = 0 * 10;	  
	if (arrRes1[1] == SILVER)
	R1B1 = 0 * 10;	  	   
	if (arrRes1[2] == BLACK)
	R1B2 = 0;
	if (arrRes1[2] == BROWN)
	R1B2 = 1;
	if (arrRes1[2] == RED)
	R1B2 = 2;
	if (arrRes1[2] == ORANGE)
	R1B2 = 3;
	if (arrRes1[2] == YELLOW)
	R1B2 = 4;	
	if (arrRes1[2] == GREEN)
	R1B2 = 5;
	if (arrRes1[2] == BLUE)
	R1B2 = 6;
	if (arrRes1[2] == VIOLET)
	R1B2 = 7;
	if (arrRes1[2] == GRAY)
	R1B2 = 8;	
	if (arrRes1[2] == WHITE)
	R1B2 = 9;
	if (arrRes1[2] == GOLD)
	R1B2 = 0;	 
	if (arrRes1[2] == SILVER)
	R1B2 = 0;	 		  	  
// Part calculation	 for R1  
	R1P1 = R1B1 + R1B2; 
// End of part calculation for R1
	if (arrRes1[3] == BLACK)
	R1P2 = R1P1 * 1;
	if (arrRes1[3] == BROWN)
	R1P2 = R1P1 * 10;
	if (arrRes1[3] == RED)
	R1P2 = R1P1 * 100;
	if (arrRes1[3] == ORANGE)
	R1P2 = R1P1 * 1000;
	if (arrRes1[3] == YELLOW)
	R1P2 = R1P1 * 10000;	
	if (arrRes1[3] == GREEN)
	R1P2 = R1P1 * 100000;
	if (arrRes1[3] == BLUE)
	R1P2 = R1P1 * 1000000;
	if (arrRes1[3] == VIOLET)
	R1P2 = R1P1 * 0;
	if (arrRes1[3] == GRAY)
	R1P2 = R1P1 * 0;	
	if (arrRes1[3] == WHITE)
	R1P2 = R1P1 *01;
	if (arrRes1[3] == GOLD)
	R1P2 = R1P1 / 10;	 
	if (arrRes1[3] == SILVER)
	R1P2 = R1P1 / 100;	
		   
// R1 sorted 

// R2 Started
	if (arrRes2[1] == BLACK)
	R2B1 = 0 * 10 ;
	if (arrRes2[1] == BROWN)
	R2B1 = 1 * 10;
	if (arrRes2[1] == RED)
	R2B1 = 2 * 10;
	if (arrRes2[1] == ORANGE)
	R2B1 = 3 * 10;
	if (arrRes2[1] == YELLOW)
	R2B1 = 4 * 10 ;	
	if (arrRes2[1] == GREEN)
	R2B1 = 5 * 10;
	if (arrRes2[1] == BLUE)
	R2B1 = 6 * 10;
	if (arrRes2[1] == VIOLET)
	R2B1 = 7 * 10;
	if (arrRes2[1] == GRAY)
	R2B1 = 8 * 10 ;	
	if (arrRes2[1] == WHITE)
	R2B1 = 9 * 10;
	if (arrRes2[1] == GOLD)
	R2B1 = 0 * 10;	  
	if (arrRes2[1] == SILVER)
	R2B1 = 0 * 10;	  	   
	if (arrRes2[2] == BLACK)
	R2B2 = 0;
	if (arrRes2[2] == BROWN)
	R2B2 = 1;
	if (arrRes2[2] == RED)
	R2B2 = 2;
	if (arrRes2[2] == ORANGE)
	R2B2 = 3;
	if (arrRes2[2] == YELLOW)
	R2B2 = 4 ;	
	if (arrRes2[2] == GREEN)
	R2B2 = 5;
	if (arrRes2[2] == BLUE)
	R2B2 = 6;
	if (arrRes2[2] == VIOLET)
	R2B2 = 7;
	if (arrRes2[2] == GRAY)
	R2B2 = 8;	
	if (arrRes2[2] == WHITE)
	R2B2 = 9;
	if (arrRes2[2] == GOLD)
	R2B2 = 0;	 
	if (arrRes2[2] == SILVER)
	R2B2 = 0;	 		  	  
// Part calculation	 for R2 
	R2P1 = R2B1 + R2B2; 
// End of part calculation for R2
	if (arrRes2[3] == BLACK)
	R2P2 = R2P1 * 1;
	if (arrRes2[3] == BROWN)
	R2P2 = R2P1 * 10;
	if (arrRes2[3] == RED)
	R2P2 = R2P1 * 100;
	if (arrRes2[3] == ORANGE)
	R2P2 = R2P1 * 1000;
	if (arrRes2[3] == YELLOW)
	R2P2 = R2P1 * 10000;	
	if (arrRes2[3] == GREEN)
	R2P2 = R2P1 * 100000;
	if (arrRes2[3] == BLUE)
	R2P2 = R2P1 * 1000000;
	if (arrRes2[3] == VIOLET)
	R2P2 = R2P1 * 0;
	if (arrRes2[3] == GRAY)
	R2P2 = R2P1 * 0;	
	if (arrRes2[3] == WHITE)
	R2P2 = R2P1 *01;
	if (arrRes2[3] == GOLD)
	R2P2 = R2P1 / 10;	 
	if (arrRes2[3] == SILVER)
	R2P2 = R2P1 / 100;	 	
	
	intStep1 = R1P2 * intRVValue;
	intFinalWork = intStep1 / R2P2;
	 
		
		intDiff = abs(ResisA[0] - intFinalWork);
		
	
	for ( counter = 0; counter < 22; counter++) 
	{


		if (abs(ResisA[counter] - intFinalWork) < intDiff)
	   	   	   { 
				intDiff = abs(ResisA[counter] - intFinalWork); 
				intLocation = counter;
				}
	} 

#ifdef	DEBUG_ON	
	for ( counter = 1; counter < 4; counter++ )
	{
		printf("\n\nwhats in arrRes1 %s",arrRes1[counter] );
	}
	for ( counter = 1; counter < 4; counter++ )
	{
		printf("\n\nwhats in arrRes2 %s",arrRes2[counter] );
	}	   	   
#endif
	
	printf("\nResistor 1 = %d",R1P2 );
	printf("\nResistor 2 = %d",R2P2 );
	
	printf("\nResistor 1 * variable resistor = %d",intStep1 );
	printf("\n%d divided by Resistor 2 = %d",intStep1, intFinalWork );
	
	printf("\nFinal Working out is = %d",intFinalWork );
	
	
	printf("\nThe Res is = %d",ResisA[intLocation] );
	printf("\nThe Temp is = %d",TempsA[intLocation] );
	

	
        printf("\nTo exit program press 1?\n");
        printf("\n1\tExit\n");
        printf("\n\nTo comfirm press 1:");
        scanf("%d", &intSelectionInput);
        
        if (intSelectionInput >= 1 && intSelectionInput <= 1 ) 
        {
                        switch (intSelectionInput)
                        {
                                case 1:
                                        system("cls");
                                        exit(0);
                                        break;
                         }                       
		}
else
        {
                system("cls");
                printf("\nYou will now be taken back to the start\n");
                fncSelectionInput();  
        }       	   	   

}


int		fncTempLoad( void )
{
FILE *FileLocation = NULL;

FileLocation = fopen( "table.txt", "r" );
   if ( FileLocation == NULL )
     return 0;
 
   for ( counter = 0; counter < 22; counter++ )
   {
     if ( fscanf( FileLocation, "%d",&ResisB ) == EOF )
        break;
 
     if ( fscanf( FileLocation, "%d", &TempsB ) == EOF )
        break;
 
 		ResisA[ counter ] = ResisB; 
		TempsA[ counter ] = TempsB;
   }

}


int fncFileInput(void)
{


        system("cls");
	
		FileBandsLocation = fopen( "bands.txt", "r" );
		
		int i = 0;
		
		while(fgets(arrBandsFile[i], maxBandStringSize, FileBandsLocation) != NULL && i < maxBandLines)
		{
		   
		    i++;
			

			
			
		}
		

		
		arrRes1[1] = arrBandsFile[0];
		arrRes1[2] = arrBandsFile[1];
		arrRes1[3] = arrBandsFile[2];
		arrRes2[1] = arrBandsFile[3];
		arrRes2[2] = arrBandsFile[4];
		arrRes2[3] = arrBandsFile[5]; 




		//convert string to integer   	  
		intRVValue = atoi(arrBandsFile[6]);	      	  

		
		printf("\nYou will now proceed to working out" );

#ifdef DEBUG_ON			
		for ( counter = 0; counter < 7; counter++ )
		{
		
			printf("\nwhats in arrBandsFile %s",arrBandsFile[counter] );
			
		}
		for ( counter = 1; counter < 4; counter++ )
		{
			printf("\nwhats in arrRes1 %s",arrRes1[counter] );
		}
		for ( counter = 1; counter < 4; counter++ )
		{
			printf("\nwhats in arrRes2 %s",arrRes2[counter] );
		}	   	   
 	 
	
		

#endif
comp = strcmp( arrBandsFile[0], RED );
	printf("\ncomp %d",comp );

//fncWorkout();
}